这是算法的一个有趣的旋转......您目前在遍历所有行时遇到的问题是,如果您的脚本仅在其中一行上看到自己,那么您的脚本将退出......
for {set i 0} {$i == 4} {incr i} {
send "clear line vty $i\r"
"confirm]" {send "\r"}
"% Not allowed" {send "quit\r"; exit}
}
如果您看到% Not allowed
,只需绕过此行并移至下一行,而不是退出脚本...
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 "*#" }
}
}
如果一行以!
以下脚本登录并清除除脚本运行的行之外的所有行...这在我的实验室中运行良好。
#!/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