期待脚本问题

时间:2012-05-09 21:20:48

标签: tcl telnet expect cisco

我是新手,我正在使用逻辑来自动执行用户登录的远程登录会话,并保留其他行:

for {set i 0} {$i ==4} {incr i} {  
send "clear line vty $i\r"  
"confirm]" {send "\r"}  
"% Not allowed" {send "quit\r"; exit}  
}

它会断开所有行

现在我使用" show user"命令,但我很难围绕它写一个脚本,输出对我来说不够友好。所以我在这个for循环中写了IF语句,但它不好,甚至不值得包括在这里。请有人指导我,以便如何匹配输出结果中的字符串,并根据字符串决定是否清除行

1 个答案:

答案 0 :(得分:2)

这是对算法的一个有趣的旋转......你目前在迭代所有行时遇到的问题是,如果它只是在其中一条线上看到你自己的脚本,那么你的脚本将会退出...

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 "*#" }
    }
}

如果以!

开头,Cisco IOS会将该行视为注释

以下脚本登录并清除除脚本运行行之外的所有行...这在我的实验室中运行良好。

#!/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