BashScripts.org
http://bashscripts.org/forum/

Telnet using expect
http://bashscripts.org/forum/viewtopic.php?f=16&t=1665
Page 1 of 1

Author:  Ravendark [ Sun Oct 28, 2012 7:30 pm ]
Post subject:  Telnet using expect

Hi,

I have a scipt that telnets to a machine (DSLAM) and then telnet again to a local IP of that machine (aDSL module) and retrieves its uptime.

Here it is:

Code:
#!/bin/bash

MSAN_IP=$1
SLOT_IP=$2
ARGS=2

if [ $# -ne $ARGS ]; then
   echo "`basename $0`: Wrong number or parameter supplied"
   echo "`basename $0`: Usage: `basename $0` <MSAN_IP> <SLOT_LOCAL_IP>"
   exit 0;
else
   expect << EOF

set timeout 3

spawn telnet $MSAN_IP 2323

expect "IPCP login: "

send "root\r"

expect "Password: "

send "E/SOu53r\r"

expect "~ # "

send "telnet 10.1.3.$SLOT_IP\r" # HERE LIES THE PROBLEM

expect "60xADSL login: "

send "root\r"

expect "Password: "

send "weblin1\r"

expect "60xADSL# "

send "uptime\r"

expect "60xADSL# "

send "exit\r"

expect “~ # ”

send "exit\r"

exit

EOF
fi


There is a problem with it...

if the second telnet to the aDSL module is not successful (e.g. unreachable module), the script will continue although the phrase it expects "60xADSL login: ", will never appear. So instead of the aDSL module uptime, the script will give me the uptime of the DSLAM machine.

To give you a visual aid these are the steps (when everything is ok):

Code:
[[email protected] ~]$ telnet 10.193.2.19 2323
Trying 10.193.2.19...
Connected to 10.193.2.19.
Escape character is '^]'.

You are logging into Marconi IP Common Part "IPCP"

IPCP login: root
Password:
~ # telnet 10.1.3.12

Entering character mode
Escape character is '^]'.


Linux 2.4.20_mvl31-wds-mips_fp_be (60xADSL) (0)

60xADSL login: root
Password:

this is motd file to inform any information to user


BusyBox v1.00-rc2 (2004.08.03-10:23+0000) Built-in shell (ash)
Enter 'help' for a list of built-in commands.

60xADSL# uptime
01:14:33 up 145 days,  2:53, load average: 0.00, 0.00, 0.00


when the aDSL module is unreachable:

Code:
[[email protected] ~]$ telnet 10.193.2.19 2323
Trying 10.193.2.19...
Connected to 10.193.2.19.
Escape character is '^]'.

You are logging into Marconi IP Common Part "IPCP"

IPCP login: root
Password:
~ # telnet 10.1.3.10
telnet: Unable to connect to remote host (10.1.3.10): No route to host
~ # uptime
01:22:17 up 343 days, 15:50, load average: 0.02, 0.20, 0.17


Well, what I need is to know when the second telnet is successful or not.
Any ideas please?

thank you in advance.

Author:  Watael [ Mon Oct 29, 2012 12:07 am ]
Post subject:  Re: Telnet using expect

hi,

I don't use expect at all, so I won't give you a solid answer. :(

you should do some search about: «conditional statements in expect script»

Author:  Ravendark [ Tue Oct 30, 2012 7:05 pm ]
Post subject:  Re: Telnet using expect

ok thank you, will do that.
Only though cause I am new to bash, I am not sure how to use conditions with expect.

Any other solutions maybe?

cheers

Author:  Watael [ Wed Oct 31, 2012 2:25 am ]
Post subject:  Re: Telnet using expect

expect is not related to bash.
they are totally different.
expect would be more like tcl (from what I read).

you can call expect inside a bash script, just like you'd call grep, sed, or any other external program.

see http://expect.sourceforge.net/ for examples, and more.

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com