【问题标题】:Linux Expect/TCL Comm Port Comunication Cisco SwitchLinux Expect/TCL Comm 端口通信 Cisco Switch
【发布时间】: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


【解决方案1】:

不要将通道上的 Expect 操作与 Tcl 混合;使用spawn -open 移交控制权后,您需要使用send 而不是puts 来写入通道,并使用expect 而不是read 来读取通道。

set portID [open /dev/ttyUSB0 r+]
fconfigure $portID -mode "9600,n,8,1"
spawn -open $portID
# From here on, DO NOT USE $portID YOURSELF!

send "\r"
# You may need to think what you're trying to receive here
set timeout 2
expect {
    -re {(.*)\n} {
        puts "Received line: $expect_out(1,string)"
        exp_continue;   # <<< KEEP ON WAITING
    }
    timeout {
        # Do nothing on timeout
    }
}

close

【讨论】:

  • 谢谢你的上述。我仍在消化它并在linux.die.net/man/1/expect 上阅读更多内容,我现在将对原始帖子进行编辑;我能够获得一个工作脚本,但似乎串行端口不会关闭,我必须物理断开 USB 电缆,以便我可以手动重新连接到交换机,以验证脚本的执行。
猜你喜欢
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-21
相关资源
最近更新 更多