" exp_internal 1"输出似乎与telnet输出不匹配

时间:2016-03-29 08:14:31

标签: expect

快速背景:我正在运行培训数据中心&我们通过调用Expect脚本的PowerScript自动执行开关出厂默认重置。这是我第一次在SOF发帖,所以请原谅我,如果我发布太多信息。

问题(1) 有没有办法提高" exp_internal 1"的可读性?输出?实际输出(telnet终端显示的信息)&诊断输出混乱起来。

问题(2) 不确定这是否是可读性问题,但下面的诊断输出与telnet输出不匹配。

我的预期剧本:

#!/bin/expect
exp_internal 1
# Set variables passed on by previous script
set acsip [lindex $argv 0]
set port [lindex $argv 1]
set name [lindex $argv 2]
set station [lindex $argv 3]
set timeout 240

# Connect to ACS and prepare N1234 switch for the beginning of class
send_user "\n\n"
send_user "==================================================================================\n"
send_user " Preparing switch $name in station $station for the beginning of class...         \n"
send_user "==================================================================================\n"
send_user "\n\n"
spawn telnet $acsip $port
expect *
send "\r"
expect {
    ">" {#code_snipped}
    "(config)#" {
        send "\r"
        expect {
            "Use ? to list commands." {send "end\r"}
            "% Unrecognized command" {send "end\r"}
            "(config)#" {send "end\r"}
        }
        expect "#"
        send "\r"
    }
    "#" {#code_snipped}
    "More-- or (q)uit" {#code_snipped}
}
expect "#"
send "delete startup-config\r"

exp_internal 1输出(完整版显示上面(1)中质疑的混乱):

Your group is currently "mkpasswd".  This indicates that your
gid is not in /etc/group and your uid is not in /etc/passwd.

The /etc/passwd (and possibly /etc/group) files should be rebuilt.
See the man pages for mkpasswd and mkgroup then, for example, run

mkpasswd -l [-d] > /etc/passwd
mkgroup  -l [-d] > /etc/group

Note that the -d switch is necessary for domain users.


==================================================================================
 Preparing switch N1234-2 in station 1 for the beginning of class...
==================================================================================


spawn telnet 192.168.1.1 3004
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {3008}

expect: does "" (spawn_id exp4) match glob pattern "*"? yes
expect: set expect_out(0,string) ""
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) ""
send: sending "\r" to { exp4 }

expect: does "" (spawn_id exp4) match glob pattern ">"? no
"(config)#"? no
"#"? no
"More-- or (q)uit"? no

expect: does "Trying 192.168.1.1...\r\n\r\nConnected to 192.168.1.1.\r\nEscape character is '^]'.\r\nTrying 192.168.
1.1..." (spawn_id exp4) match glob pattern
">"? no
"(config)#"? Connected to 192.168.1.1.no
"#"? Escape character is '^]'.no
"More-- or (q)uit"? no

expect: does "Trying 192.168.1.1...\r\n\r\nConnected to 192.168.1.1.\r\nEscape character is '^]'.\r\n\r" (spawn_id e
xp4) match glob pattern ">"? no
"(config)#"? no
"#"? no
"More-- or (q)uit"? no

expect: does "Trying 192.168.1.1...\r\n\r\nConnected to 192.168.1.1.\r\nEscape character is '^]'.\r\n\r\r       " (s
pawn_id exp4) match glob pattern
">"? no
"(config)#"? no
"#"? no
"More-- or (q)uit"? no

expect: does "                  Trying 192.168.1.1...\r\n\r\nConnected to 192.168.1.1.\r\nEscape character is '^]'.\
r\n\r\r                  \rvrrp                     Dis
" (spawn_id exp4) match glob pattern ">"? no
"(config)#"? no
"#"? no
"More-- or (q)uit"? no

expect: does "vrrp                     Display virtual router redundancy protocol status.Trying 192.168.1.1...\r\n\r\n
Connected to 192.168.1.1.\r\nEscape character is '^]'.\r\n\r\r                  \rvrrp                     Display vir
tual router redundancy protocol status.\r\r\n\r\r\ncon" (spawn_id exp4) match glob pattern
">"?
no
"(config)#"? no
"#"? no
"More-- or (q)uit"? no

expect: does "Trying 192.168.1.1...\r\n\r\nConnected to 192.168.1.1.\r\nEscape character is '^]'.\r\n\r\r
       \rvrrp                     Display virtual router redundancy protocol status.\r\r\n\r\r\nconsole(config)#do show
" (spawn_id exp4) match glob pattern ">"? no
"(config)#"? yes
expect: set expect_out(0,string) "(config)#"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "Trying 192.168.1.1...\r\n\r\nConnected to 192.168.1.1.\r\nEscape character is '^]'.\
r\n\r\r                  \rvrrp                     Display virtual router redundancy protocol status.\r\r\n\r\r\nconsol
e(config)#"
send: sending "\r" to { exp4 }

expect: does "do show " (spawn_id exp4) match glob pattern "Use ? to list commands."? no
"% Unrecognized command"? no
"(config)#"? no

expect: does "console(config)#do show do show \r\r\n\r\r\n% " (spawn_id exp4) match glob pattern
"Use ? to list commands."?
no
"% Unrecognized command"? no
"(config)#"? no

expect: does "% Unrecognized commanddo show \r\r\n\r\r\n% Unrecognized command\r\r\nc" (spawn_id exp4) match glob patter
n
"Use ? to list commands."?
no
"% Unrecognized command"? yes
expect: set expect_out(0,string) "% Unrecognized command"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "do show \r\r\n\r\r\n% Unrecognized command"
send: sending "end\r" to { exp4 }
expect: does "\r\r\nc" (spawn_id exp4) match glob pattern "#"? no
expect: does "\r\r\nconsole(config)#" (spawn_id exp4) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "\r\r\nconsole(config)#"
send: sending "\r" to { exp4 }

expect: does "" (spawn_id exp4) match glob pattern "#"? no

expect: does "e" (spawn_id exp4) match glob pattern "#"? no

console(config)#endexpect: does "end\r\r\nconso
" (spawn_id exp4) match glob pattern "
#"? no

expect: does "end\r\r\nconsole#\r\r\nconsole#console#" (spawn_id exp4) match glob pattern
"#"?
yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "end\r\r\nconsole#"
send: sending "delete startup-config\r" to { exp4 }

exp_internal 1输出(不匹配的部分):

以下诊断显示,在" end \ r" 发送后,期望正在寻找"#" "#" 是通过匹配" \ r \ r \ n nconsole(config)#" 找到的。随后发送" \ r"

send: sending "end\r" to { exp4 }
expect: does "\r\r\nc" (spawn_id exp4) match glob pattern "#"? no
expect: does "\r\r\nconsole(config)#" (spawn_id exp4) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "\r\r\nconsole(config)#"
send: sending "\r" to { exp4 }

Telnet输出:

以下输出与上述诊断不匹配。在"结束\ r" 发送后,下一个"#"发现实际上是" console#" ,而不是" console(config)#" 。这一点很明显,下一个" \ r" 也是在"控制台#" 提示符下发送的,不是" console(config)#" ,如上面的诊断输出所示..

Your group is currently "mkpasswd".  This indicates that your
gid is not in /etc/group and your uid is not in /etc/passwd.

The /etc/passwd (and possibly /etc/group) files should be rebuilt.
See the man pages for mkpasswd and mkgroup then, for example, run

mkpasswd -l [-d] > /etc/passwd
mkgroup  -l [-d] > /etc/group

Note that the -d switch is necessary for domain users.


==================================================================================
 Preparing switch N3024-2 in station 1 for the beginning of class...         
==================================================================================


spawn telnet 10.206.196.69 3004
Trying 10.206.196.69...

Connected to 10.206.196.69.
Escape character is '^]'.



vrrp                     Display virtual router redundancy protocol status.



console(config)#do show 



% Unrecognized command

console(config)#end

console#

console#delete startup-config

我正在阅读诊断信息,还是有其他方法可以确保准确性?

1 个答案:

答案 0 :(得分:1)

(不是答案,延伸评论)

是的,诊断输出很乱。我不知道如何解决这个问题:你必须展示你期望看到的输入,以及它与你的模式是否匹配。

我不明白你输入后会出现“do show”。这是预期的行为吗?

我也不明白你为何在产生telnet命令后立即输入。这需要吗?

是的,在看到(config)#并发送end\r后,期望命令结束,下一个命令需要“#”

    expect {
        "Use ? to list commands." {send "end\r"}
        "% Unrecognized command" {send "end\r"}
        "(config)#" {send "end\r"}
    }
    expect "#"
    send "\r"

我建议您尝试摆脱嵌套期望语句。它使逻辑很难遵循。了解exp_continue命令。