Linux Expect/TCL Comm Port Comunication Cisco Switch

天涯浪子 提交于 2019-12-25 03:26:46

问题


I have done much reading on TCL/Expect and wrote the following script to connect to the comm port on my RPi-Kali-Linux box.

#!/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

Everything works fine up until I try to read from the serial port.

When I connect to the switch manually using minicom, I am greeted with the standard "Welcome to minicom" banner. From there, I press enter (carrage return) and I can then interact with the switch with standard Cisco AT-Commands. But from using the above script, I see no output.

I therefore do not know what to "expect" so I cannot proceed accordingly with configuring the switch via the script.

Any help or advice will be greatly appreciated.

Thanks in advance.





EDIT: From comments and more reading, I have modified the above script to what we see below. Commands now transfer to the Cisco Switch, although it seems as if the serial port is still getting traffic. The terminal also freezes when I try to manually log into the switch to check the configuration. I think the Serial Port is not closed. I am unsure what I did wrong.

I was also told not to call $portID manually after using the "spawn" command. I, therefore, commented out any "puts" that would call this port. Because of this, I am unsure how to display the output results while the script is executing.

#!/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

回答1:


Don't mix Expect's operations on a channel with Tcl's; once you hand control over with spawn -open, you need to use send instead of puts to write to the channel, and expect instead of read to read from the channel.

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


来源:https://stackoverflow.com/questions/52819052/linux-expect-tcl-comm-port-comunication-cisco-switch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!