【问题标题】:Unexpected behaviours with TCL/Expect and CiscoTCL/Expect 和 Cisco 的意外行为
【发布时间】:2014-06-26 13:54:49
【问题描述】:

我正在尝试登录 Cisco 交换机并运行命令列表。
使用以下代码,我可以登录设备、启用和配置终端:

    # Connect to single host, enable, and configure
    proc connect {host payload username password enablepassword} {

send_user "Connecting to: $host $payload $username $password $enablepassword\n"

spawn ssh -o "StrictHostKeyChecking no" -l $username $host

# # Pardon the rudeness; some switches are upper case, some are lower case
expect "assword:"
send "$password\r"

# Switch to enable mode
expect ">" 
send "en\r"

expect "assword:"
send "$enablepassword\r"
expect "*#"
send -- "conf t\r"

expect "config*#"
}

但是,使用以下代码,我得到以下输出。 ($payload 包含一个文件,每行有一个 IOS 命令)

proc drop_payload {payload} {
set f [open "$payload"]
set payload [split [read $f] "\n"]
close $f

foreach pld $payload {

    send -- "$pld\r"
    expect "config*#"

    sleep 2
}
}

我的期望是这个循环将遍历文件中的每一行,但是,Expect 调试(来自 exp_internal 1)如下:

    HOST-0001#
expect: does " \r\HOST-0001#" (spawn_id exp7) match glob pattern "*#"? yes
expect: set expect_out(0,string) " \r\nHOST-0001#"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) " \r\nHOST-0001#"
send: sending "conf t\r" to { exp7 }

expect: does "" (spawn_id exp7) match glob pattern "config*#"? no
c
expect: does "c" (spawn_id exp7) match glob pattern "config*#"? no
o
expect: does "co" (spawn_id exp7) match glob pattern "config*#"? no
n
expect: does "con" (spawn_id exp7) match glob pattern "config*#"? no
f
expect: does "conf" (spawn_id exp7) match glob pattern "config*#"? no

expect: does "conf " (spawn_id exp7) match glob pattern "config*#"? no
t
expect: does "conf t" (spawn_id exp7) match glob pattern "config*#"? no


expect: does "conf t\r\n" (spawn_id exp7) match glob pattern "config*#"? no
Enter configuration commands, one per line.  End with CNTL/Z.
HOST-0001(config)#
expect: does "conf t\r\nEnter configuration commands, one per line.  End with CNTL/Z.\r\nHOST-0001(config)#"     (spawn_id exp7) match glob pattern "config*#"? yes
expect: set expect_out(0,string) "configuration commands, one per line.  End with CNTL/Z.\r\nHOST-0001(config)#"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) "conf t\r\nEnter configuration commands, one per line.  End with CNTL/Z.\r\nHOST-0001(config)#"
}end: sending "no logging 172.x.x.20\r" to { exp0 no logging 172.x.x.20

expect: does "" (spawn_id exp0) match glob pattern "config*#"? no
expect: timed out
}end: sending "no logging 172.x.x.210\r" to { exp0 no logging 172.x.x.210

expect: does "" (spawn_id exp0) match glob pattern "config*#"? no
expect: timed out
}end: sending "no logging 172.x.x.9\r" to { exp0 no logging 172.x.x.9

expect: does "" (spawn_id exp0) match glob pattern "config*#"? no
expect: timed out
}end: sending "no logging 172.x.x.210\r" to { exp0 no logging 172.x.x.210

expect: does "" (spawn_id exp0) match glob pattern "config*#"? no
expect: timed out
}end: sending "no logging 172.x.x.20\r" to { exp0 no logging 172.x.x.20

expect: does "" (spawn_id exp0) match glob pattern "config*#"? no
expect: timed out
}end: sending "logging 172.x.x.50\r" to { exp0 logging 172.x.x.50

expect: does "" (spawn_id exp0) match glob pattern "config*#"? no
expect: timed out

我很困惑为什么它试图期待正在发送到主机的“conf t”;没有收到。
我也很困惑,为什么在应用 conf t 后任何命令结束时都没有按下开关,而是超时。

【问题讨论】:

  • 这里的问题是 ssh 会话在一个过程中打开,但随后尝试从另一个函数使用它被发送到另一个(不存在的)衍生会话。试图强制第二个过程使用初始 ssh 连接....

标签: ios tcl expect cisco


【解决方案1】:

您可以尝试使用 spwan_id 发送配置

spawn ssh -o "StrictHostKeyChecking no" -l $username $host
#After process creation the process id will be saved in 
#standard expect variable'spawn_id'
#Copying it to variable 'id'
set id $spawn_id

现在变量 'id' 持有对 ssh 进程的引用。我们可以很好地将 send 和 expect 与 spawn id 一起使用。

#Now we are setting the spawn id to our ssh process to make sure 
#we are sending the commands to right process
#You can pass this variable 'id' as arg in 'drop_payload'

set spawn_id $id


foreach pld $payload {

    send -- "$pld\r"
    expect "config*#"

    sleep 2
}

或者反过来如下,

foreach pld $payload {
    #This way is useful, when u want to send and expect to multiple process 
    #simultaneously.
    send -i $id "$pld\r"
    expect -i $id "config*#"

    sleep 2
}

【讨论】:

  • 如果您仍然有问题,请手动发布在路由器上完成的配置输出,例如 2 或 3 个命令。
【解决方案2】:

我发现每个函数/过程都输出到一个新的生成 ID。

一种方法是遵循 Dinesh 的建议并明确定义 spawn id。

我的解决方法是将所有内容简单地填充到单个输出过程中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多