LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 02-26-2014, 11:59 AM   #1
sumncguy
Member
 
Registered: Jun 2013
Posts: 42

Rep: Reputation: Disabled
Beginner - Expect - error condition


On occasion I need to go out to net devices and do a few show commands.

This script works ok when devices are reachable. My problem is I introduced a bogus ip to test my error handling. I have two problems.

The first is even though the time value is set to 30, much more than 30 seconds expire before I get the following error.

Quote:
Servername $ expect: spawn id exp6 not open
while executing
"expect "*?admin:*""
(file "/home/sumncguy/mmexp" line 23)

servername$
Code:
#!/local/bin/expect -f 
log_user 0
set username [lindex $argv 0]
set password [lindex $argv 1]
set ip [lindex $argv 2]
set timeout 30 
match_max 100000
#
spawn ssh -q -o StrictHostKeyChecking=no $unam@$ip
#set err [open $ip.err w]
log_file -a $ip.log
expect {
        "*?assword:*" { send "$psswd\r" }
        "*?connecting*" { send "yes\r"  }
        "*?Connection refused*" { set err [open $ip.err w]; puts $err "$ip not reachable connection refused.\n"; log_file; exp_continue }
        "*?Connection timed out*" { set err [open $ip.err w];  puts $err "$ip not reachable connection timed out.\n"; log_file; exp_continue }
        timeout { set err [open $ip.err w];  puts $err "$ip not reachable connection timed out.\n"; log_file; exp_continue }
#timeout continue
#expect -re ".*#"
       }
expect "*?admin:*"
send "show version\r"
expect "*?admin:*"
send "show status\r"
expect "*?admin:*"
send "show hardware all\r"
expect "*?admin:*"
send "show upgrade detail\r"
expect "*?admin:*"
send "show cdp neighbors\r"
expect "*?admin:*"
sleep 3
send "quit\r"
log_file
close
So my first question is, Why isnt the script timing out after 30 secs (approximately)

Why is the line
Quote:
timeout { set err [open $ip.err w]; puts $err "$ip not reachable connection timed out.\n"; log_file; exp_continue }
not working ?

Additionally, the script terminates, I see in ps -ef |grep sumncguy that the job is actually still running, then some time later I get the error message.

Thanks in advance for the help.

Last edited by sumncguy; 02-26-2014 at 12:00 PM. Reason: correct explaination
 
Old 02-26-2014, 12:15 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,428
Blog Entries: 1

Rep: Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144
Because you are continuing the script *after* the error?

Code:
timeout { set err [open $ip.err w];  puts $err "$ip not reachable connection timed out.\n"; log_file; exp_continue }
If you want to end the script after a failure, you would want:

Code:
timeout { set err [open $ip.err w];  puts $err "$ip not reachable connection timed out.\n"; log_file; exit }

Last edited by szboardstretcher; 02-26-2014 at 12:17 PM.
 
Old 02-26-2014, 04:11 PM   #3
sumncguy
Member
 
Registered: Jun 2013
Posts: 42

Original Poster
Rep: Reputation: Disabled
Red face Sheez !!

TSCH !!! Such a rookie I am ... just didnt see it !!! ... a bit embarassing

thanks !
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[root@fugo trace]# sh expect.sh expect.sh: line 9: expect: command not found sivaloga Linux - Kernel 1 08-22-2013 04:29 AM
check Negative egrep condition in an if condition novicunix Programming 5 02-02-2013 12:52 AM
[SOLVED] If condition is throwing error vishnukumar Programming 3 11-01-2010 08:09 AM
Silly error: If test condition shalomajay Programming 3 10-31-2005 03:57 PM
"expect" shell beginner question, please help me out yuzuohong Linux - General 1 06-28-2003 01:17 PM


All times are GMT -5. The time now is 11:50 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration