Reputation: 25
I am having an issue. I have a simple expect script that i call in a php script to login to a cisco terminal server to clear vty lines after a lab time is over. The logic is simple that if a user is present then it only occupies line vty 0.
The problem occurs if a user is not present... the cisco router just displays an error stating %connection to clear or something and expect run the rest of the script which is not necessary because i am managing a lab of 10 routers and loading configuration files which takes a lot of time.
Please tell me how to read the error from log and end script if i get that message.
#! /usr/bin/expect -f
spawn telnet 192.168.2.1
set user [lindex $argv 0]
set pass [lindex $argv 1]
expect "Username:"
send "$user\r"
expect "Password:"
send "$pass\r"
expect "*>"
send "enable\r"
expect "*#"
send "clear line vty 0\r"
send "\r"
expect "*#"
send "copy tftp://192.168.2.3 running-config\r"
send "config\r"
send "\r"
expect "*#\r\n"
Upvotes: 1
Views: 710
Reputation: 43077
The error handling code you are asking about is here...
send "clear line vty 0\r"
expect {
"confirm]" { send "\r" }
"% Not allowed" { send "quit\r"; exit }
}
expect "#"
This looks for either [confirm]
or % Not allowed to clear current line
and makes an appropriate go / no-go decision.
I made a few modifications to your script so I could run it against machines in my lab. I'm not sure how you disabled the router from asking for an enable password, but that may be something to add to your script.
If you wanted to make the script more robust, you could issue a show user
before clearing lines, and only clear lines that your script isn't on. I will leave that as an exercise for you to experiment with.
#!/usr/bin/expect -f
spawn telnet 172.16.1.5
set user [lindex $argv 0]
set pass [lindex $argv 1]
expect "Username: "
send "$user\r"
expect "assword: "
send "$pass\r"
expect ">"
send "enable\r"
expect "assword: "
send "$pass\r"
expect "#"
send "clear line vty 0\r"
expect {
"confirm]" { send "\r" }
"% Not allowed" { send "quit\r"; exit }
}
expect "#"
send "copy tftp://192.168.2.3 running-config\r"
expect "Source filename *]? "
send "config\r"
expect "Destination filename *]? "
send "running\r"
expect "#"
exit
Upvotes: 3