-->

Expect脚本问题(Expect script problems)

2019-10-16 16:25发布

我是新来的期待和我使用的是逻辑自动diconnecting其中用户登录并留下其他行,因为它是telnet会话:

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

它断开所有行

现在我用“显示用户”命令,但它很难为我写它周围的脚本,输出不是me.so足够友好的我在这写IF语句for循环,但它并不好,甚至不值得,包括在这里。 请可有人指导我为了如何在输出结果和琴弦的基础上匹配字符串决定清除线或不

Answer 1:

这是该算法的有趣的旋转......你现在有通过所有行迭代问题是,如果它只是把自己的线路之一为编写脚本将退出...

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

思科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


文章来源: Expect script problems