问题
I am new to expect and i was using a logic to automate diconnecting telnet session where user is logged in and leave other lines as it is:
for {set i 0} {$i ==4} {incr i} {
send "clear line vty $i\r"
"confirm]" {send "\r"}
"% Not allowed" {send "quit\r"; exit}
}
it disconnects all the lines
now i use "show user" command but it's hard for me to write a script around it, the output is not friendly enough for me.so i wrote IF statements in this for loop but it was not good and not even deserving to include here. please can somebody guide me in order to how to match strings in a output result and on the basis of the strings decide to clear line or not
回答1:
That is an interesting spin on the algorithm... the problem you currently have with iterating through all the lines is that your script as-written will exit if it merely sees itself on one of the lines...
for {set i 0} {$i == 4} {incr i} {
send "clear line vty $i\r"
"confirm]" {send "\r"}
"% Not allowed" {send "quit\r"; exit}
}
If you see % Not allowed
, simply bypass this line and move to the next, instead of exiting the script...
for {set i 0} {$i < 4} {incr i} {
send "clear line vty $i\r"
expect {
"confirm]" { send "\r"; expect "*#" }
"% Not allowed" { send "! refusing to clear line vty $i\r"; expect "*#" }
}
}
Cisco IOS considers a line as a comment if it begins with !
The following script logs in and clears all lines except the line the script runs on... This runs fine in my lab.
#!/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 "*#"
for {set i 0} {$i < 5} {incr i} {
send "clear line vty $i\r"
expect {
"confirm]" { send "\r"; expect "*#" }
"% Not allowed" { send "! refusing to clear line vty $i\r"; expect "*#" }
}
}
exit
来源:https://stackoverflow.com/questions/10524261/expect-script-problems