我已经在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”标语。从那里,我按Enter(回车),然后可以使用标准的Cisco AT-Commands与交换机进行交互。但是使用上面的脚本,我看不到任何输出。
因此,我不知道要“期待”什么,因此无法通过脚本配置开关。
任何帮助或建议将不胜感激。
谢谢。
编辑:从评论和更多阅读中,我已经将上面的脚本修改为我们在下面看到的内容。命令现在传输到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
答案 0 :(得分: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