I would like to be able to setup a expect script file that would remote into multiple switches.
Do you know how to write expert script for telnet to 200 switches and run a command example: show ip interface brief
then list all switch status on Linux??
#!/usr/bin/expect -f
spawn telnet 192.168.0.1
expect "Username:"
send "MyUsername\r"
expect "assword:"
send "MyPassword\r"
send "show ip int br\r"
interact timeout 5
expect {
"MORE --, next page" {send -- " "; exp_continue}
"*?2626#*" {send -- "exit \r"}
}
Thanks Dinesh, after my expect script as below:
set fp [open "input.txt" r]
set file_data [read $fp]
close $fp
set prompt ">"
log_file -noappend switch_port_status.txt
foreach ip [split $file_data "\n"] {
puts "Switch $ip Interface Status"
spawn telnet $ip
expect "Username:"
send "MyUsername\r"
expect "assword:"
send "MyPassword\r"
expect $prompt
# To avoid sending 'Enter' key on huge configurations
send "show ip int br\r"
expect $prompt
expect {
-ex "--More--" { send -- " "; exp_continue }
"*>" { send "exit\r" }
}
set timeout 1; # Reverting to default timeout
# Sending 'exit' at global level prompt will close the connection
expect eof
}
Thats workable. But following is the error message i got, how can i fix this? thanks
switch-hostname>Switch Interface Status
spawn telnet
usage: telnet [-l user] [-a] host-name [port]
send: spawn id exp9 not open
while executing
"send "MyUsername\r""
("foreach" body line 5)
invoked from within
"foreach ip [split $file_data "\n"] {
puts "Switch $ip Interface Status"
spawn telnet $ip
expect "Username:"
send "MyUsername\r"
expec..."
(file "./autotelnet.sh" line 8)
You can use a file which inputs the switch IP information in a line by line manner. Using terminal length 0
will save our work & finally log_file
comes in handy to save the output.
#!/usr/bin/expect -f
#Slurp up the input file
set fp [open "input.txt" r]
# To avoid empty lines, 'nonewline' flag is used
set file_data [read -nonewline $fp]
close $fp
set prompt "#"
log_file -noappend switch_port_status.txt
foreach ip [split $file_data "\n"] {
puts "Switch $ip Interface Status"
spawn telnet $ip
expect "Username:"
send "MyUsername\r"
expect "assword:"
send "MyPassword\r"
expect $prompt
# To avoid sending 'Enter' key on huge configurations
send "terminal length 0\r"
expect $prompt
set timeout 120;# Increasing timeout to 2mins, as it may take more time to get the prompt
send "show ip int br\r"
expect $prompt
set timeout 10; # Reverting to default timeout
# Sending 'exit' at global level prompt will close the connection
send "exit\r"
expect eof
}
Note : The close of connection is handled on the basis of sending exit
command only. So, make sure, the exit
command always sent at global level of prompt. Instead of depending on exit
, we can also use typical way of closing the telnet connection i.e. 'Ctrl+]' key combination.
#This will send 'Ctrl+]' to close the telnet connection gracefully
send "\x1d"
expect "telnet>"
send "quit\r"
expect "Connection"
If suppose you want to get multiple commands output, then keep them in a file and send them one by one.