If I open two bash shells and telnet from Shell 2 to a remote server (on the Net), is there a way to direct input from Shell 1 to the telnet shell?
The telnet shell is a limited environment with a specific command set.
I want to direct commands from Shell 1 and, if possible, put 1-second delays between them because the remote telnet shell server can't handle rapid input.
It would be ideal to redirect a file with the commands (with carriage returns and pauses between them) from Shell 1.
I tried with Netcat, but I don't know how to make Shell 1 and Shell 2 talk to each other, especially when Shell 2 is engaged in a telnet session.
Here's a small script that will do what I think you need. It works by starting a telnet process and piping its stdin/out back to the script. There are some hoops to be jumpped through to deal with the login/password prompts and then goes into a loop reading 'command-file' and sending the commands with a sleep in between. You will see the user name echoed back to your tty before the prompt for password; I didn't fuss with disabling echo when starting telnet.
It makes a BIG assumption that the PS1 prompt at the other end has a known trailing character ($ is coded in the script), and that this character isn't in the MOTD or other output. I don't think it will matter if it is in other output, other than possibly clogging the TCP input buffer on the local end a bit.
All output from the remote end to the tty is ignored -- probably a good idea if each command in the input file redirects both stdin and stdout.
#!/usr/bin/env ksh
host=${1:-localhost}
port=${2} # default to nothing to let telnet default
remote_prompt='$' # assume user prompt at remote end has a trailing $
telnet $host $port |& # start telnet in a separate process with stdin/out piped to this shell
# -p causes read/print to use the pipe to telnet
read -d : -p prompt # read login goo up to the first colon ... login:
printf "$prompt: " # prompt user, read answer (login name) and send back to telnet
read answer
print -p $answer
read -d : -p prompt # read next goo up to : (will be an echo of the user name)
printf "$prompt: " # show prompt and then read and send password
stty -echo # dont echo passwd back onto tty
read answer
stty echo # reset terminal to echo mode
print -p $answer
printf "\n"
read -d $remote_prompt -p answer # assume last character of prompt is $ -- read up to and discard
#printf "$discard: $answer\n" # uncomment to debug
while read command
do
printf "sending: $command\n"
print -p $command # send command; command should probably redirect stdout/stderr
read -d $remote_prompt -p answer # assume to be everything up through the prompt
#echo "$answer" # uncomment for debug
sleep 2
done <command-file
I've tested this under Kshell; no clue as to whether or not bash supports reading/writing to a child process. If you need more info about the Kshell read and print commands have a look at: Korn's Kshell man page at AT&T
You may know this, but I do feel the need to say: telnet is not secure and there are a good many users and admins that avoid it like the plague. This sort of thing can easily be accomplished via ssh with a lot less complexity in the local script and about 5 minutes worth of work to install your key on the remote host.