Spawning an interactive telnet session from a shell script

怎甘沉沦 提交于 2019-12-03 11:51:29

You need to redirect the Terminal input to the telnet process. This should be /dev/tty. So your script will look something like:

#!/bin/bash

for HOST in `cat`
do
  echo Connecting to $HOST...
  telnet $HOST </dev/tty
done

I think you should look at expect program. It`s present in all modern linux distros. Here is some exmaple script:

#!/usr/bin/expect -f
spawn telnet $host_name
expect {
   "T0>"                {}
   -re "Connection refused|No route to host|Invalid argument|lookup failure"
                        {send_user "\r******* connection error, bye.\n";exit}
   default              {send_user "\r******* connection error (telnet timeout),
 bye.\n";exit}
}
send "command\n"
expect -timeout 1 "something"

spawn command start remote login program (telnet, ssh, netcat etc)

expext command used to... hm.. expect something from remote session

send - sending commands

send_user - to print comments to stdout

Thanks Dave - it was the TTY redirection that I was missing.

The complete solution I used, for those who are interested:

#!/bin/bash

TTY=`tty` # Find out what tty we have been invoked from.
for i in `cat hostnames.csv` # List of hosts/ports
do
        # Separate port/host into separate variables
        host=`echo $i | awk -F, '{ print $1 }'`
        port=`echo $i | awk -F, '{ print $2 }'`
        telnet $host $port < $TTY # Connect to the current device
done
Prashant Ghodke

Telnet to Server using Shell Script Example:

Test3.sh File:

#!/bin/sh

#SSG_details is file from which script will read ip adress and uname/password
#to telnet.

SSG_detail=/opt/Telnet/SSG_detail.txt

cat $SSG_detail | while read ssg_det ; do

   ssg_ip=`echo $ssg_det|awk '{print $1}'`
   ssg_user=`echo $ssg_det|awk '{print $2}'`
   ssg_pwd=`echo $ssg_det|awk '{print $3}'`


   echo " IP to telnet:" $ssg_ip
   echo " ssg_user:" $ssg_user
   echo " ssg_pwd:" $ssg_pwd

   sh /opt/Telnet/Call_Telenet.sh $ssg_ip $ssg_user $ssg_pwd 

done


exit 0

The Call_Telenet.sh script is as follows:

#!/bin/sh

DELAY=1 
COMM1='config t'                 #/* 1st commands to be run*/
COMM2='show run'
COMM3=''
COMM4=''
COMM5='exit'
COMM6='wr'
COMM7='ssg service-cache refresh all'
COMM8='exit'                     #/* 8th command to be run */


telnet $1 >> $logfile 2>> $logfile |&
sleep $DELAY
echo -p $2 >> $logfile 2>> $logfile
sleep $DELAY
echo -p $3 >> $logfile 2>> $logfile
sleep $DELAY
echo -p $4 >> $logfile 2>> $logfile
sleep $DELAY
echo -p $5 >> $logfile 2>> $logfile
sleep $DELAY

sleep $DELAY
sleep $DELAY
sleep $DELAY
echo -p $COMM7 >> $logfile 2>> $logfile
sleep $DELAY
echo -p $COMM8 >> $logfile 2>> $logfile
sleep $DELAY

exit 0

Run the above file as follows:

$> ./test3.sh 

Perhaps you could try bash -i to force the session to be in interactive mode.

The problem in your example is that you link the input of your script (and indirectly of telnet) to the output of the echo. So after echo is done and telnet is started, there is no more input to read. A simple fix could be to replace echo "testhost" by { echo "testhost"; cat; }.

Edit: telnet doesn't seem to like taking input from a pipe. However, netcat does and is probably just suitable in this case.

@muz I have a setting with ssh, no telnet, so i can't test if your problem is telnet related, but running the following script logs me successively to the different machines asking for a password.

for i in adele betty
do
ssh all@$i
done

If your environment is X11-based, a possibility is to open an xterm running telnet:

xterm -e telnet $host $port

Operations in xterm are interactive and shell script is halted until xterm termination.

Try these links.

http://planetozh.com/blog/2004/02/telnet-script/

http://www.unix.com/unix-dummies-questions-answers/193-telnet-script.html

#!/bin/sh
( echo open hostname
sleep 5
echo username
sleep 1
echo password
sleep 1
echo some more output, etc. ) | telnet

They worked for me :D

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!