tags:

views:

5021

answers:

9

I'm trying to write a script to allow me to log in to a console servers 48 ports so that I can quickly determine what devices are connected to each serial line.

Essentially I want to be able to have a script that, given a list of hosts/ports, telnets to the first device in the list and leaves me in interactive mode so that I can log in and confirm the device, then when I close the telnet session, connects to the next session in the list.

The problem I'm facing is that if I start a telnet session from within an executable bash script, the session terminates immediately, rather than waiting for input.

For example, given the following code:

$ cat ./telnetTest.sh
#!/bin/bash

while read line
do
        telnet $line
done
$

When I run the command 'echo "hostname" | testscript.sh' I receive the following output:

$ echo "testhost" | ./telnetTest.sh
Trying 192.168.1.1...
Connected to testhost (192.168.1.1).
Escape character is '^]'.
Connection closed by foreign host.
$

Does anyone know of a way to stop the telnet session being closed automatically?

A: 

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

stephanea
A: 

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

A: 

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.

mweerden
A: 

@stephenea: I thought of that, as when I run:

$ for i in `cat hostnames.txt`; do telnet $i; done

I get a succession of telnet prompts that accept input. However, when I use the -i switch in the #! line of telnetTest.sh, I am connected to the first host in the list, but am unable to send any input to the remote device, nor to the local telnet session. I am forced to Ctrl-C out of the telnet session, and every subsequent one until the end of the list. Is there some additional STDIN/STDOUT redirection I need to do?

@dagnir: Unfortunately the machine I will be using is a Solaris machine being provided by a third party; there's no easy way of getting additional packages installed on it.

Murali Suriar
+3  A: 

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
Dave Webb
A: 

@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
stephanea
+1  A: 

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
Murali Suriar
A: 

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
Prashant Ghodke
A: 

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.

mouviciel