Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

Linux Expect/TCL Comm Port Comunication Cisco Switch

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

close

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...