LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices

Reply
 
LinkBack Search this Thread
Old 02-02-2012, 02:26 AM   #1
s.farhan
LQ Newbie
 
Registered: Dec 2011
Posts: 8

Rep: Reputation: Disabled
Lightbulb Problem with expect script for telneting cisco switches


Hello everyone,

I have created a expect script that will telnet to cisco switches. The script runs well until it reach to password prompt and it stuck. Here is my script -
Quote:
#!/bin/bash
#!/usr/bin/expect
# Start the expect script
(expect -c "
set timeout 20
# Start the session with the input variable and the rest of the hostname
spawn telnet 192.168.50.11
expect -nocase \"username:\"
send \"farhan\r\"
expect -nocase \"password:\"
send \"*********\\r\"
expect \"*#\"
# Allow us to interact with the switch ourselves
interact
# stop the expect script once the telnet session is closed
exit
")
output:
Quote:
spawn telnet 192.168.50.11
Trying 192.168.50.11...
Connected to 192.168.50.11.
Escape character is '^]'.


User Access Verification

Username: farhan
Password:

% Authentication failed.
I have googled a lot could not find any thing that help. At the end of my password there is a "\". If this is the issue then how can I use it in these script.

Please help me because I have to maintain over 100 switches.
 
Old 02-02-2012, 03:23 PM   #2
jefro
Guru
 
Registered: Mar 2008
Posts: 5,934

Rep: Reputation: 582Reputation: 582Reputation: 582Reputation: 582Reputation: 582Reputation: 582
Try stopping the script (edit it) at the point of password so that you enter it. See if it works then.
 
1 members found this post helpful.
Old 02-03-2012, 01:54 PM   #3
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 463

Rep: Reputation: 36
Also you can try running it with debug mode like
$expect -d filename
I hope you will get clear idea whats going on by looking at the debug output.
 
1 members found this post helpful.
Old 02-05-2012, 01:15 PM   #4
rajivk
LQ Newbie
 
Registered: Aug 2011
Posts: 7

Rep: Reputation: Disabled
why don't u try this below?

---------
#!/bin/bash
#!/usr/bin/expect -f
# Start the expect script
# Enable the below "exp_internal" option for debugging purpose only
# exp_internal 1
set timeout -1
set prompt "(%|#|\\\$) $"
# Start the session with the input variable and the rest of the hostname
spawn telnet 192.168.50.11
expect -nocase -re "username: "
send -- "farhan\r"
expect -nocase -re "password: "
send -- "********\r"
expect -re $prompt
send -- "\r"
# And Now start what you want to do within the telnet session
# Allow us to interact with the switch ourselves
interact
# stop the expect script once the telnet session is closed
send -- "exit\r"
--------------------------

My other suggestion is to please keep a copy of "Exploring Expect" by Don Libbes handy for reference and bettwr understanding of Expect as a starter.

Iam not sure of the exam name of the author...but the publication is O'Reilly. hope this helps.

cheers
-Rajiv
 
Old 02-05-2012, 10:11 PM   #5
s.farhan
LQ Newbie
 
Registered: Dec 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanx everyone to reply.


Quote:
Originally Posted by rajivk View Post
why don't u try this below?

---------
#!/bin/bash
#!/usr/bin/expect -f
# Start the expect script
# Enable the below "exp_internal" option for debugging purpose only
# exp_internal 1
set timeout -1
set prompt "(%|#|\\\$) $"
# Start the session with the input variable and the rest of the hostname
spawn telnet 192.168.50.11
expect -nocase -re "username: "
send -- "farhan\r"
expect -nocase -re "password: "
send -- "********\r"
expect -re $prompt
send -- "\r"
# And Now start what you want to do within the telnet session
# Allow us to interact with the switch ourselves
interact
# stop the expect script once the telnet session is closed
send -- "exit\r"
--------------------------

My other suggestion is to please keep a copy of "Exploring Expect" by Don Libbes handy for reference and bettwr understanding of Expect as a starter.

Iam not sure of the exam name of the author...but the publication is O'Reilly. hope this helps.

cheers
-Rajiv
@Rajiv I have used your code but it still stuck at PASSWORD.

I have also DEbug it and it give this result


Quote:
expect -d DM-ACC-SW1
expect version 5.44.1.14
argv[0] = expect argv[1] = -d argv[2] = DM-ACC-SW1
set argc 0
set argv0 "DM-ACC-SW1"
set argv ""
executing commands from command file DM-ACC-SW1
spawn telnet 192.168.50.11
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {12661}
Gate keeper glob pattern for 'username: ' is 'username: '. Activating booster.

expect: does "" (spawn_id exp6) match regular expression "username: "? Gate "username: "? gate=no
Trying 192.168.50.11...
Connected to 192.168.50.11.
Escape character is '^]'.

expect: does "Trying 192.168.50.11...\r\nConnected to 192.168.50.11.\r\nEscape character is '^]'.\r\n" (spawn_id exp6) match regular expression "username: "? Gate "username: "? gate=no


User Access Verification

Username:
expect: does "Trying 192.168.50.11...\r\nConnected to 192.168.50.11.\r\nEscape character is '^]'.\r\n\r\n\r\nUser Access Verification\r\n\r\nUsername: " (spawn_id exp6) match regular expression "username: "? Gate "username: "? gate=yes re=yes
expect: set expect_out(0,string) "Username: "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "Trying 192.168.50.11...\r\nConnected to 192.168.50.11.\r\nEscape character is '^]'.\r\n\r\n\r\nUser Access Verification\r\n\r\nUsername: "
send: sending "farhan\r" to { exp6 }
Gate keeper glob pattern for 'password: ' is 'password: '. Activating booster.

expect: does "" (spawn_id exp6) match regular expression "password: "? Gate "password: "? gate=no
farhan
expect: does "farhan" (spawn_id exp6) match regular expression "password: "? Gate "password: "? gate=no

Password:
expect: does "farhan\r\nPassword: " (spawn_id exp6) match regular expression "password: "? Gate "password: "? gate=yes re=yes
expect: set expect_out(0,string) "Password: "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "farhan\r\nPassword: "
send: sending "**********\r"" to { exp6 }
Gate keeper glob pattern for '(%|#|\$) $' is ''. Not usable, disabling the performance booster.

expect: does "" (spawn_id exp6) match regular expression "(%|#|\$) $"? (No Gate, RE only) gate=yes re=no


% Authentication failed.
I dont understand the debug so pls help me.
 
Old 02-06-2012, 02:14 PM   #6
rajivk
LQ Newbie
 
Registered: Aug 2011
Posts: 7

Rep: Reputation: Disabled
ok let's try this again as below. I might have written somethings wrongly previously. Consider the debug output above and assuming that your username and password is farhan and farhan.

Also if iam right, logging into cisco router/switch generates a prompt with ">" sign at the end. Next you would need to go into enable mode by typing "en" and giving the enable password. so the script may somewhat look like below:

----------
#!/bin/bash
#!/usr/bin/expect -f
# Start the expect script
# Enable the below "exp_internal" option for debugging purpose only
# exp_internal 1
set timeout -1
# Start the session with the input variable and the rest of the hostname
spawn telnet 192.168.50.11
expect -re "Username: "
send -- "farhan\r"
expect -re "Password: "
send -- "farhan\r"
expect -re "\.*>*"
send -- "\r"
expect -re "\.*>*"
# And Now start what you want to do within the telnet session
# Allow us to interact with the switch/router ourselves
interact
# stop the expect script once the telnet session is closed
send -- "exit\r"
--------------------------

what i understand from the debug output (here i too am a learner of expect/tcl and am not a master or skilled enough to be correct in my understanding...so take it with a pinch of salt and please do further studying/analysis on your own) is that:

1. the password prompt expected may be wrong and maybe the password id wrong
2. in case the password is correct and accepted, then the expected prompt pattern is NOT matching

but 2 may not be the case as the message says Authentication failed.

My another suggestion to you which would be great help to you is to do use "autoexpect" for one session of telnet to one of your router/switch. It records the steps you execute manually and then gives you a tentative expect script, which you can generalise to a all purpose script. So do the following steps:

1. On the machine running expect, type expect and get the expect prompt and start autoexpect in prompt mode (-p). All your steps executes is recorded in the script file "script.exp" which you can later edit and run again
--------
root# expect
expect1.1> autoexpect -p
....some mesg..autoexpect started...recording to script.exp file....or something similar...
root# telnet 192.168.50.11
....do the normal manual steps necessary to login to the router/switch.. including going into enable mode

...
type exit to get out of telnet
type one more exit to get out of autoexpect

expectx.x>

2. Now just go thru the script.exp file to understand what expect does

hope this helps

-rajiv
 
Old 02-07-2012, 12:35 AM   #7
s.farhan
LQ Newbie
 
Registered: Dec 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rajivk View Post
what i understand from the debug output (here i too am a learner of expect/tcl and am not a master or skilled enough to be correct in my understanding...so take it with a pinch of salt and please do further studying/analysis on your own) is that:

1. the password prompt expected may be wrong and maybe the password id wrong
2. in case the password is correct and accepted, then the expected prompt pattern is NOT matching

but 2 may not be the case as the message says Authentication failed.

My another suggestion to you which would be great help to you is to do use "autoexpect" for one session of telnet to one of your router/switch. It records the steps you execute manually and then gives you a tentative expect script, which you can generalise to a all purpose script. So do the following steps:

1. On the machine running expect, type expect and get the expect prompt and start autoexpect in prompt mode (-p). All your steps executes is recorded in the script file "script.exp" which you can later edit and run again
--------
root# expect
expect1.1> autoexpect -p
....some mesg..autoexpect started...recording to script.exp file....or something similar...
root# telnet 192.168.50.11
....do the normal manual steps necessary to login to the router/switch.. including going into enable mode

...
type exit to get out of telnet
type one more exit to get out of autoexpect

expectx.x>

2. Now just go thru the script.exp file to understand what expect does

hope this helps

-rajiv
Thanx for the effort rajiv. Now I use only This code and giving password manually .
Quote:
#!/bin/bash
#!/usr/bin/expect
# Start the expect script
(expect -c "
set timeout 20
# Start the session with the input variable and the rest of the hostname
spawn telnet 192.168.50.11
expect -nocase \"username:\"
send \"farhan\r\"
expect -nocase \"password:\"
# Allow us to interact with the switch ourselves
interact
# stop the expect script once the telnet session is closed
exit
")
Thnx everyone.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem running an Expect script within a Bash script mbeipi Programming 8 03-15-2011 04:07 PM
[SOLVED] /usr/bin/expect : Script to check server load using both expect and bash Soji Antony Programming 1 07-27-2010 11:27 PM
Expect script: how do i send function key F12 in an expect script alix123 Programming 1 11-20-2008 12:07 PM
how can i config. switches & cisco-routers. rahuldevalone Linux - Networking 2 02-19-2007 08:13 AM
cisco switches caching IP's to MAC's for 3 hours abefroman Linux - Networking 3 11-01-2006 09:41 PM


All times are GMT -5. The time now is 06:12 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration