8

I would like to make a shell script that executes commands on a device that I telnet to or in programs such as FTP or OpenSSL. I have already found a method in FTP, which would look something like this:

#!/bin/sh
HOST='0.0.0.0'
USER='User'
PASSWD='Pass'
FILE='~/Desktop/file.txt'
RFILE='file.txt'

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
binary
passive
put $FILE $RFILE
quit
END_SCRIPT
exit 0

How can I do something similar with devices I telnet to or in different programs?

John K
  • 514
  • 2
  • 4
  • 13
  • use `expect` or write a perl script that uses `Expect.pm` (or one of the perl `Net::*` modules like `Net::Telnet` or `Net:SSH` etc) or a python script that uses `pexpect`. – cas Dec 04 '15 at 11:41
  • @cas I would like to stick to shell script, as it does not need installation of anything and is portable across other UNIX like systems. – John K Dec 04 '15 at 11:51
  • Then it's too bad that sh (in any of its variations) isn't really up to the task. there's too many things that can go wrong (or are just unexpected) that a simple redirection sh script can't cope with. There's a reason why tools like `expect` and the others i mentioned exist - and that's because shell scripts aren't capable of doing the job reliably. – cas Dec 04 '15 at 12:36
  • @cas If there isn't a way then, you can make an answer on making that perl script – John K Dec 04 '15 at 12:37
  • you haven't provided anywhere near enough details to write any kind of script. it's not needed, though - there are many examples of `expect` etc scripts on this site (and on stack overflow and serverfault). Start with some of the links under the heading Related to the right. and also search the SE sites for expect and/or the perl and python libraries i mentioned. – cas Dec 04 '15 at 12:42
  • your most portable option is to use `expect` itself. it's an old, common tool installed on many systems already. the perl / python options not only require their interpreters to be installed (which is not uncommon) but also the relevant libraries (which is not so common)....they are much easier to work with and more flexible than `expect`itself, though. – cas Dec 04 '15 at 12:46

1 Answers1

19

some time ago I needed something similar. You can try with something like this:

#!/bin/sh

HOST='0.0.0.0'
USER='User'
PASSWD='Pass'
CMD=''

(
echo open "$HOST"
sleep 2
echo "$USER"
sleep 2
echo "$PASSWD"
sleep 2
echo "$CMD"
sleep 2
echo "exit"
) | telnet

PS1: I user "sleep 2" because I am accessing through satellite. If it's LAN, maybe you don't need "sleep" at all.

PS2: just enter what you need in the CMD variable

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
lester289
  • 316
  • 2
  • 3
  • 1
    Thank you so much! I have been going crazy trying to make expect and autoexpect work when I could have just done that! :) – NaturalBornCamper Sep 07 '17 at 11:39
  • Not having `expect` available to work with, this solution saves me so much headache. I just needed the immediate output of `telnet localhost 25` to check what it's printing, and simply piping `usleep 10000` to it works great (get it to stagger and then close the connection). Thank you. –  Jun 15 '18 at 14:25