【问题标题】:Expect script problems预期脚本问题
【发布时间】:2012-05-18 10:48:59
【问题描述】:

我是新手,我正在使用一种逻辑来自动断开用户登录的 telnet 会话并保持其他行不变:

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

它断开所有线路

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

【问题讨论】:

    标签: tcl telnet expect cisco


    【解决方案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 "*#" }
        }
    }
    

    如果一行以! 开头,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
    

    【讨论】:

    • 我无法理解期望 {} 在做什么??它是否带来了一堆期望??
    • 这是一种告诉期望寻找几个响应之一的方式
    • 好的,还有一件事...我正在运行一个期望脚本来运行一个命令来检查在实验室开始 10 分钟后占用的 tty 行,使用“sh line tty 1 sum”然后匹配字符串...是否有可能如果他们没有人在线我可以以某种方式在我的 php 脚本中返回一个值,以便我可以删除该用户凭据???
    • 我希望你知道我在用这一切做什么 :) 它是一个免费的 cisco + 基于云的实验室网络监控工具 :) 我会在它建成后告诉你。你是一个真正的帮助
    • 不客气。如果这解决了您的问题,请不要忘记接受答案。
    猜你喜欢
    • 2016-02-03
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    • 2011-03-12
    相关资源
    最近更新 更多