Register
It is currently Tue Jul 29, 2014 10:49 am

Telnet using expect


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Sun Oct 28, 2012 7:30 pm   

Joined: Fri May 07, 2010 4:04 am
Posts: 7
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:
[portal@preproduction ~]$ 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:
[portal@preproduction ~]$ 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.


Top
 Profile  
 PostPosted: Mon Oct 29, 2012 12:07 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 533
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»


Top
 Profile  
 PostPosted: Tue Oct 30, 2012 7:05 pm   

Joined: Fri May 07, 2010 4:04 am
Posts: 7
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


Top
 Profile  
 PostPosted: Wed Oct 31, 2012 2:25 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 533
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.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP