【发布时间】:2018-10-15 14:30:19
【问题描述】:
我对 TCL/Expect 做了很多阅读,并编写了以下脚本来连接我的 RPi-Kali-Linux 机器上的 comm 端口。
#!/usr/bin/expect -f
set timeout -1
;#set the portID and open it for reading and writing
set portID [open /dev/ttyUSB0 r+]
set baud 9600
;#Configure the port with the baud rate
;#and dont block on read, dont buffer output
fconfigure $portID -mode "9600,n,8,1"
fconfigure $portID -blocking 0 -buffering none
;#Write to the comm port by sending a carrage return
spawn -open $portID
puts -nonewline $portID "<CR>\r"
after 1000
puts "Modem echo: [read $portID]"
close $portID
在我尝试从串行端口读取之前,一切正常。
当我使用 minicom 手动连接到交换机时,我会看到标准的“欢迎使用 minicom”横幅。从那里,我按下回车键(carrage return),然后我可以使用标准 Cisco AT-Commands 与交换机交互。但是使用上面的脚本,我看不到任何输出。
因此,我不知道“期待”什么,因此我无法通过脚本相应地配置交换机。
任何帮助或建议将不胜感激。
提前致谢。
编辑:根据 cmets 和更多阅读,我已将上面的脚本修改为我们在下面看到的内容。命令现在传输到 Cisco 交换机,尽管串行端口似乎仍在获取流量。当我尝试手动登录交换机检查配置时,终端也会冻结。我认为串行端口没有关闭。我不确定我做错了什么。
我还被告知在使用“spawn”命令后不要手动调用 $portID。因此,我注释掉了任何会调用此端口的“放置”。因此,我不确定如何在脚本执行时显示输出结果。
#!/usr/bin/expect -f
;#exp_internal 1 ;#Can enable this line for debugging. add -df above
;#set the portID and open it for reading and writing
set portID [open /dev/ttyUSB0 r+]
set baud 9600
;#Configure the port with the baud rate
;#and dont block on read, dont buffer output
fconfigure $portID -mode "9600,n,8,1"
fconfigure $portID -blocking 0 -buffering none
spawn -open $portID
set timeout 2
send -- "\r"
after 100
;#puts "Modem Echo: $portID"
;#expect -re "Would you like to enter the initial configuration dialog?" ;#Something wrong with this line, it is not matching
;#using the below instead
expect -re "yes/no"
after 100
send -- "no\r"
after 100
send -- "enable\r"
after 100
;#puts "Modem Echo: [read $portID]"
after 100
send -- "configure terminal\r"
;#puts "Modem Echo: [read $portID]"
after 100
;#At a later date, modify this next line to take user input on the number
;#of ports on the switch in question
send -- "interface range GigabitEthernet 0/1-8\r"
;#puts "Modem Echo: [read $portID]"
after 100
send -- "power inline static\r"
;#puts "Modem Echo: [read $portID]"
after 2000
send -- "no cdp enable\r"
;#puts "Modem Echo: [read $portID]"
after 100
send -- "exit\r"
;#puts "Modem Echo: [read $portID]"
after 100
send -- "exit\r"
;#puts "Modem Echo: [read $portID]"
after 100
send -- "copy running-config startup-config\r"
after 100
;#puts "Modem Echo: [read $portID]"
after 100
;#expect -re "Destination filename" ;#Problem with this line
;#going to ignore what to expect and just send a return
send -- "\r"
expect "#"
after 100
send -- "exit\r"
expect "#"
;#close $portID
close
【问题讨论】:
-
您可以从
autoexpect命令开始——它将启动一个交互式shell会话,您将手动执行您需要做的事情:autoexpect 将记录该会话并将其保存为期望您可以查看和编辑的脚本。 -
在开发过程中,打开 expect 的详细调试输出,这样您就可以看到正在看到的内容以及哪些模式匹配和不匹配:使用
#!/usr/bin/expect -df或在 shebang 下添加exp_internal 1。 -
感谢调试提示,现已开启。对于“autoexpect”的使用,我不确定如何在 BASH shell 中使用期望命令。我无法使用“chsh -s /usr/bin/expect”
-
仔细观察,我发现 autoexpect 在这里真的帮不了你。当您必须通过 ssh 连接到远程机器并与那里的某些程序进行交互时,它会更加有用。你为什么要改变你的登录shell?那是铁杆。
-
在预期中,您使用
send "\r"“按回车键”
标签: tcl expect at-command cisco