1

I would like to pipe a text file with commands to a telnet session I have open on a ssh client, Is this possible? an example: I have a text file cmds that contains commands like

FirstFunction(A,B,C,0x9001,123456,7);
SecondFunction(A,B,C1,2,4);
ThirdFunction(A,B,C,D,E,F);

and I can copy 7 paste each command into the telnet session I have open in a client ssh shell. can I automate this to directly stream each line into the telnet session?

EDIT1
On my ssh host, I open the telnet session (where the commands ha to go to ) like telnet MyHost

EDIT2
I tried:

$ ssh root@cc1 telnet MyHost < cmds                                                                                       
Welcome to MyHost                                                                                                              
Trying 10.254.0.2...                                                                                                                            
Connected to MyHost.                                                                                                                            
Escape character is '^]'.                                                                                                                       
Connection closed by foreign host.
ron@jpax-build07:~/tmp$ 

it doesn't seem to pipe the cmds file into the telnet session at all.
What I tried as well is:

$ ssh root@cc1 cmds - | telnet MyHost
Welcome to MyHost
cat: cmds: No such file or directory
telnet: could not resolve MyHost/telnet: Name or service not known

EDIT 3
I installed expect and tried the following:

#!/usr/bin/expect
spawn ssh root@ccTest telnet -l MyHost
#expect Password:
#send "mypassword\r"
expect {-> }
send "some_cmd(20)\r"
expect {$ }
send "exit\r"
expect eof

but this is what I got:

$ ./jpax_txcmd.sh 
spawn ssh root@ccTest telnet -l MyHost
bad flag "-> ": must be -glob, -regexp, -exact, -notransfer, -nocase, -i, -indices, -iread, -timestamp, -timeout, -nobrace, or --
    while executing
"expect {-> }"
    (file "./jpax_txcmd.sh" line 5)
$
4
  • 1
    Have you tried? ssh … < cmds should work. If the commands require the presence of a TTY add the -t flag to ssh Jul 12, 2016 at 21:53
  • @DylanFrese Thanks for your reply, please see EDIT 1 above, the commands have to be forwarded into the telnet session somehow...
    – stdcerr
    Jul 12, 2016 at 21:58
  • 1
    telnet MyHost < cmds should do the trick, then, assuming you don't have to wait for output to come back through after all the commands are sent. If you do, try cat cmds - | telnet MyHost. Jul 12, 2016 at 22:02
  • @DylanFrese Please see EDIT2 above, doesn't seem to work this way...
    – stdcerr
    Jul 12, 2016 at 23:21

1 Answer 1

4

The problem with telnet is that once it reads end-of-file it will close the connection and you will not see any output. A simple hack is to hold the input open for some extra time, eg:

(cat cmds; sleep 15) | ssh root@cc1 telnet MyHost 

Obviously, this is not a robust solution, so you should use something that is able to detect when the remote has finished, for example expect. The following example expect script implements a typical telnet over ssh conversation involving a login for myusername with password mypassword and sending the command ls -l to a $<space> shell prompt:

#!/usr/bin/expect
spawn ssh root@cc1 telnet -l myusername myhost
expect Password:
send "mypassword\r"
expect {$ }
send "ls -l\r"
expect {$ }
send "exit\r"
expect eof
3
  • Thanks for your suggestions, I gave it a try, please see EDIT 3 above
    – stdcerr
    Jul 19, 2016 at 19:58
  • @cerr There is a minor problem because the quoted string {-> } (i.e. -> ) has been interpreted as a flag to the expect command. oops. You simply need to use -- to signal the end of the flags, so for that line use expect -- {-> }
    – meuh
    Jul 19, 2016 at 20:06
  • Yes, I just realized or escaping woerks too: expect {\-> }!
    – stdcerr
    Jul 19, 2016 at 20:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .