Article 4TR6S using expect script to automate telnet session - timeout issue

using expect script to automate telnet session - timeout issue

by
manit123
from LinuxQuestions.org on (#4TR6S)
hi,
I am using ubuntu 14.04 LTS 32 bit OS.
I have an expect script that telnets to various devices in network and reboots them.
It sleeps for 15 minutes then reboots all of them again.
By that time all devices in network are ready again.
Actually, devices get ready in 5 minutes.
The conversation happens as follows

Quote:
BCM968500 Broadband Router
Login: support
Password:
> reboot
My script is as follows
Code:#!/usr/bin/expect -f
log_file blah.txt
set timeout 2
set ip_addr_start [lindex $argv 0]
set ip_addr_end [lindex $argv 1]
set loop_max_count [lindex $argv 2]
set chan [open mylog.txt a ]
for {set loop_count 1} {$loop_count <= $loop_max_count} {incr loop_count} {
for {set ip_addr $ip_addr_start} {$ip_addr <= $ip_addr_end} {incr ip_addr} {
set result [catch {exec ping -c 1 -W 1 10.0.0.$ip_addr}]
set timestamp [clock format [clock seconds]]
puts "result of pinging $ip_addr is $result"
puts $chan "$timestamp - ping $ip_addr is $result"
if {$result == 0} {
spawn telnet 10.0.0.$ip_addr
expect "*Login:"
send -- "support\r"
expect "*Password:"
send -- "support\r"
expect ">"
send -- "reboot\r"
} else {
puts "not going to telnet to $ip_addr"
puts $chan "$timestamp - ping $ip_addr failed"
}
}
if {$loop_count == $loop_max_count} {
break
}
sleep 900
}
close $chan
expect eofYou can run it as follows
./script start_ip end_ip count
where start_ip-end_ip specify range in 10.0.0.0 network
and count specifies number of reboots.

If some error causes device to be inaccessible then ping to it will fail.
So script wont try to telnet to it.

It has been observed that sometimes ping to device will work but its telnet server won't respond.
In such case, my script fails.
I have observed that telnet to an IP that does not exists times out after 15 minutes which is too long.How can I change that ?
Can you suggest some other solution ?

Thanks.latest?d=yIl2AUoC8zA latest?i=XJqzgdQLePM:AKlqHxEe6qU:F7zBnMy latest?i=XJqzgdQLePM:AKlqHxEe6qU:V_sGLiP latest?d=qj6IDK7rITs latest?i=XJqzgdQLePM:AKlqHxEe6qU:gIN9vFwXJqzgdQLePM
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments