[SOLVED] Problem with expect script for telneting cisco switches
Linux - DesktopThis forum is for the discussion of all Linux Software used in a desktop context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
---------
#!/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.
---------
#!/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
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
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
")
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.