Sending commands to a Telnet program

Z

Zach Flynn

Ok, so I'm programming in Unix if that makes any difference. And I'm
doing:

system("telnet somehost.com 25");

But I can't figure out how to send a command to it. Like, for example,
I would like to send the helo command to the smpt port with this telnet
open. Does anyone here know how to? Thanks.

(kinda desired output)

# ./program

Telnetting...

helo
250 welcome.
mail from: (e-mail address removed)
250 ok.

etc...I think you get the idea that I would like to this inside my C
program.

-Zach
 
D

Donovan Rebbechi

Ok, so I'm programming in Unix if that makes any difference.

Yes, it does. It means that your question is topical in comp.unix.programmer
(but not here).
And I'm doing:

system("telnet somehost.com 25");

But I can't figure out how to send a command to it.

Perhaps it's because you're starting wrong.
Like, for example, I would like to send the helo command to the smpt port
with this telnet open. Does anyone here know how to? Thanks.

start with
man socket
and read the related pages.

Cheers,
 
A

Alan Johnson

Zach said:
Ok, so I'm programming in Unix if that makes any difference. And I'm
doing:

system("telnet somehost.com 25");

But I can't figure out how to send a command to it. Like, for example,
I would like to send the helo command to the smpt port with this telnet
open. Does anyone here know how to? Thanks.

(kinda desired output)

# ./program

Telnetting...

helo
250 welcome.
mail from: (e-mail address removed)
250 ok.

etc...I think you get the idea that I would like to this inside my C
program.

-Zach

First, wrong newsgroup for this.

Second, you'll want to do roughly the following:
1) Open two pipes. (man 2 pipe)
2) Fork. (man 2 fork)
3) Close the unneeded ends of the pipes in each process. (man 2 close)
4) Use dup2 to set stdin and stdout to your pipes. (man 2 dup2)
5) In your child process, do something like execl("/bin/sh", "/bin/sh",
"-c", "telnet host port"). (man 3 execl)

Then you could read from the program and write to its stdin via your
pipes. Or if you were more comfortable, use fdopen to get FILE*'s to
read/write with instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top