LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-06-2019, 05:53 AM   #1
manit123
LQ Newbie
 
Registered: Nov 2019
Posts: 4

Rep: Reputation: Disabled
using expect script to automate telnet session - timeout issue


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 eof
You 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.
 
Old 11-06-2019, 10:02 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,711

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
Welcome to LinuxQuestions

My tcl/expect skills are not that good but this might work.

Code:
spawn telnet 10.0.0.$ip_addr
expect {
  timeout { send_user "\nFailed to get login prompt for $ip_addr\n"; continue }
  eof { send_user "\ntelnet failure for server $ip_addr\n"; continue }
  "*Login:"
}
Your timeout value is set to 2 seconds which may not be enough and if a timeout occurs the loop should go to the next IP address. eof is required if telnet fails to connect.

Last edited by michaelk; 11-06-2019 at 10:22 AM.
 
1 members found this post helpful.
Old 11-06-2019, 10:26 PM   #3
manit123
LQ Newbie
 
Registered: Nov 2019
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Welcome to LinuxQuestions

My tcl/expect skills are not that good but this might work.

Code:
spawn telnet 10.0.0.$ip_addr
expect {
  timeout { send_user "\nFailed to get login prompt for $ip_addr\n"; continue }
  eof { send_user "\ntelnet failure for server $ip_addr\n"; continue }
  "*Login:"
}
Your timeout value is set to 2 seconds which may not be enough and if a timeout occurs the loop should go to the next IP address. eof is required if telnet fails to connect.
That works for me.
I will post the updated code.
I will review if there is anything else I need help with in this before closing this thread.
Thanks.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Expect Script to automate Telnet login and Logs to File and Print Output. whatsup Linux - Newbie 5 03-15-2016 09:51 AM
[root@fugo trace]# sh expect.sh expect.sh: line 9: expect: command not found sivaloga Linux - Kernel 1 08-22-2013 04:29 AM
Automate FTP session interaction with Expect script phpguru Programming 4 11-21-2012 10:01 AM
Shell Script Commands to Automate Server or Host Telnet Login Session jakesjohn Linux - General 2 02-08-2010 11:28 PM
LXer: Speaking Unix, Part 6: Automate, automate, automate! LXer Syndicated Linux News 0 01-04-2007 09:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration