期待脚本问题 [英] Expect script problems

查看:87
本文介绍了期待脚本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,我正在使用一种逻辑来自动进行telnet会话的断开连接,在该会话中,用户已登录,并保留了其他行:

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}  
}

它断开所有线路

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

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

推荐答案

这是算法上的一个有趣的转折……您当前遍历所有行时遇到的问题是,编写的脚本如果退出则将退出只是看到其中一种...

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}
}

如果看到% Not allowed,只需跳过此行并移至下一行,而不是退出脚本...

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以!

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

这篇关于期待脚本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆