Operating System - Linux
1828344 Members
3001 Online
109976 Solutions
New Discussion

Re: How to send commands to some ports using script?

 
Shashikiran_1
Advisor

How to send commands to some ports using script?

Hi ,


I need to telnet to a Unix box using some ports. And send some commands.
Eg :
telnet
send ctrl+A
Now I will get a prompt.

Now send some commends.

send brk
send chkstatus

I tried using label . It didn't work.

e.g. : telnet 192.168.10.10 4006 <
4 REPLIES 4
Dave Falloon
Trusted Contributor

Re: How to send commands to some ports using script?

I would use netcat and perl to do this, something like this:

perl -e 'print \cA' |nc

I can't remember if thats the right syntax for ctrl+A, you may have to change that to the decimal equivalent, I think its 001?

--Dave
Clothes make the man, Naked people have little to no effect on society
Stuart Browne
Honored Contributor

Re: How to send commands to some ports using script?

The reason why the 'here-document' didn't work as an input into telnet is because it doesn't wait.

To use a pipe and telnet, you have to force sleeps in to wait for the output, i.e.:

(
echo ^A
sleep 1
echo brk
sleep 1
echo chkstatus
sleep 1
) | telnet 192.168.10.10 4006

But the better option would be to use 'expect', if there is some sort of output expected after each command.
One long-haired git at your service...
Gopi Sekar
Honored Contributor

Re: How to send commands to some ports using script?


'expect' is the right method to use for this.
check man expect to find out how to achieve this.

you can also do through pipe like this,

mkfifo pipe # this will create the pipe
cat > pipe # this will open the pipe and wait for input, then go to another session

telnet host < pipe # this will open telnet session with input from pipe

now whatever sent in pipe will be piped to telnet. you can use this method to control it from script.

Regards,
Gopi
Never Never Never Giveup
Dave Falloon
Trusted Contributor

Re: How to send commands to some ports using script?

I played with this a little more, here is another useful way to do this.

printf \\001\\nbrk\\nchkstatus|nc 192.168.10.10 4006

the nice thing about netcat is you don't have to wait for responses, that is built in to the program.

check out the man page for more info:

man nc

--Dave
Clothes make the man, Naked people have little to no effect on society