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)
$
ssh … < cmds
should work. If the commands require the presence of a TTY add the-t
flag tossh
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, trycat cmds - | telnet MyHost
.