简体   繁体   English

与TCL /期望和思科的意外行为

[英]Unexpected behaviours with TCL/Expect and Cisco

I'm trying to log into a Cisco switch and run a list of commands. 我正在尝试登录Cisco交换机并运行命令列表。
Using the following code, I'm able to log into the device, enable, and configure terminal: 使用以下代码,我可以登录设备,启用和配置终端:

    # 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*#"
}

However, using the following code, I get the output below. 但是,使用以下代码,我得到下面的输出。 ($payload contains a file which has one IOS command per line) ($ 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
}
}

My expectation is that this loop will iterate over each line in the file, however, the Expect debug (from exp_internal 1) is as follows: 我的期望是,此循环将遍历文件中的每一行,但是,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

I'm confused as to why it's trying to expect "conf t" which is being sent to the host; 我对为什么它试图期望发送到主机的“ conf t”感到困惑。 not received. 没收到。
I'm also confused as to why any of the commands end after conf t is applied don't hit the switch, and time out instead. 我也很困惑为什么在应用conft之后没有结束任何命令而不点击开关,而是超时了。

You can try sending the configurations with the spwan_id 您可以尝试使用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

Now the variable 'id' is holding the reference to the ssh process. 现在,变量“ id”保存了对ssh进程的引用。 We can very well use the send and expect with the spawn id. 我们可以很好地将send和期望与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
}

Or the other way around is as follows, 或相反,如下所示,

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
}

I found that each function/procedure was outputting to a new spawn ID. 我发现每个函数/过程都输出到一个新的生成ID。

One method is to follow Dinesh's advice and explicitly define the spawn id. 一种方法是遵循Dinesh的建议并显式定义生成ID。

My workaround was to simply stuff everything into a single output procedure. 我的解决方法是将所有内容简单地填充到单个输出过程中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM