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>
"
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 -- "
"
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
"
after 100
send -- "enable
"
after 100
;#puts "Modem Echo: [read $portID]"
after 100
send -- "configure terminal
"
;#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
"
;#puts "Modem Echo: [read $portID]"
after 100
send -- "power inline static
"
;#puts "Modem Echo: [read $portID]"
after 2000
send -- "no cdp enable
"
;#puts "Modem Echo: [read $portID]"
after 100
send -- "exit
"
;#puts "Modem Echo: [read $portID]"
after 100
send -- "exit
"
;#puts "Modem Echo: [read $portID]"
after 100
send -- "copy running-config startup-config
"
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 -- "
"
expect "#"
after 100
send -- "exit
"
expect "#"
;#close $portID
close
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…