使用TCL / Expect和思科的意外行为

时间:2014-06-26 13:54:50

标签: ios tcl expect cisco

我正在尝试登录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之后任何命令结束时都没有按下开关,而是超时。

2 个答案:

答案 0 :(得分:0)

您可以尝试使用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
}

答案 1 :(得分:0)

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

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

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