EXCEPT timeout problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting EXCEPT timeout problem
# 1  
Old 02-02-2010
EXCEPT timeout problem

i am trying to write an except script to ssh into a list of devices and run some commands, and i came across this problem, not every device is alive, which breaks the script, my script looks like this
Code:
#!/usr/bin/expect                           
# set defaults                              
set device [lindex $argv 0]                 

set user "admin"
set password "test123"
set list [open $device r] 
set resultfile [open setlog.log w]
set timeout 10                    
trap exit SIGINT                  

log_file -a -noappend setlog_logfile.log

# ssh to each IP on the list

while {[gets $list line] != -1} {
set pid [spawn ssh -o "ConnectTimeout 3" admin@$line]  
expect "password:"               
send "$password\r"               
expect eof                       
expect "#"                       
send "shell\r"                   
****some commands here****
send "exit\r"                             
expect eof                                
puts $resultfile "$line, Worked!"         
log_user 1                                
wait                                      
}

following is the error message i got, apparently expect runs across a dead ip and it times out and *continue* to run the next command in the script, how can i fix this? thanks
Code:
$ ./testrun /tmp/testip
spawn ssh -o ConnectTimeout 3 admin@192.168.1.2
ssh: connect to host 192.168.1.2 port 22: Connection timed out
send: spawn id exp9 not open
    while executing
"send "$password\r"               "
    ("while" body line 4)
    invoked from within


Last edited by Scott; 02-02-2010 at 03:06 PM.. Reason: Please use code tags
# 2  
Old 02-02-2010
Wrench

The mistype of the title will confuse!

I tend to write expect scripts using a loop, that way it can handle repeat questions and unexpected events rather than getting stuck when things don't occur in the correct order, the classic example is using expect to answer questions from fsck, can't remember where I found this example but it illustrates the point:
Code:
while 1 {
	expect {
		eof                    {break}
	        "UNREF FILE*CLEAR\\?"  {send "y\r"}
		"BAD INODE*FIX\\?"     {send "n\r"}
		"\\? "	               {interact +}
		}
	}

# The last question mark is a catch all.
#  \\ prevents the next character from being interpreted as a wild card.

In your case you could use the lines (plus others):
Code:
expect {
"password:"             {send "$password\r" }
Connection timed out    {break}
"#"                     {send "shell\r some more commands here"}
}

So it can then continue onto the next machine
I don't think this can handle the expect EOF followed by "#" that you are doing though...
Whether while statement can be nested in expect I'm not sure.

An idea that might be no good, but I thought I'd suggest it just in case?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expect timeout

Hello, I'm doing a simple script with expect (a telnet which works without user/pass), and I want to put a condition if timeout happens, then to print a message, but it doesn't work. The script looks like below: #!/usr/bin/expect log_user 0 set timeout 10 spawn telnet IP PORT send... (3 Replies)
Discussion started by: rainbow19
3 Replies

2. Shell Programming and Scripting

Setting a Timeout

Hi I'm writing a script which based on a condition, restarts a set of servers. The problem I'm facing is, say if one of the server is down, my script stops there and fails to proceed. How can I ensure to set a timeout value on that script, so when the server is not reachable, the script should... (2 Replies)
Discussion started by: mathbalaji
2 Replies

3. Red Hat

Freezing, Request Timeout - performance problem

Hi, The server is Redhat 5 32bit. It is not easy to produce the problem so I will just write the experienced problems not how it is produced. 1) During login, it shows the last login time and then waits for 3-4 seconds to give shell prompt (considerable) 2) Sometimes the connection window... (2 Replies)
Discussion started by: royalliege
2 Replies

4. Shell Programming and Scripting

sftp expect timeout problem

hello All, I am doing SFTP using expect. We just change our server from sun solaris 8 to sun solaris 10. The script was working good on sun solaris 8. But it is giving problem on 10. from shell, SFTP is working fine.Please help me. What can be the problem. LIB_sftp_get() { ... (0 Replies)
Discussion started by: mindtee_abhi
0 Replies

5. Shell Programming and Scripting

how to set timeout?

When I run a script where the 1st parameter is ip address ftp -n -i -v $1 I hang here if the ip is wrong how to set a timeout something like if (20s not complete "ftp -n -i -v $1") then echo "error" fi Thanks a lot. (14 Replies)
Discussion started by: uativan
14 Replies

6. UNIX for Advanced & Expert Users

timeout opening writing control channel /dev/initctl problem occur i cant shoudown

Hi... This is message that occurs when i am trying to shutdown the linux system timeout opening writing control channel /dev/initctl how can i shutdown what is the problem here.. Thanks in advance ... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

7. Filesystems, Disks and Memory

timeout problem

Hi can anyone help with the following:- when sending large e-mails via a ssh session the job always times out every 5 min before the mail is sent, this means that a user has to tap a key to stop it timming out. Is there a way to stop this from happening. Numpty (4 Replies)
Discussion started by: numpty
4 Replies

8. Solaris

About the Timeout

Hello everyone I am a new one,I want to know how to get the solaris force the loginer out if he do not in a time thanks (4 Replies)
Discussion started by: lyh003473
4 Replies

9. HP-UX

timeout

How can I kick a user out after being idle for a certain amount of time, would prefer not to use scripts, will TMOUT work on HP-UX? (5 Replies)
Discussion started by: csaunders
5 Replies
Login or Register to Ask a Question