Below is the script, plus the output of running the script
with the '-d' flag.
There is an entry in the FAQ that addresses "spawn id XXX not open"
but, assuming that entry applies to this situation, I don't understand
why the spawned process would close the connection prematurely.
I'd be grateful for any ideas.
Darin
########## script begin #########
set prompt "router.(>|#)"
set timeout 40
eval spawn telnet $argv
interact -o -nobuffer -re "Username:" return
send "ops\r"
interact -o -nobuffer -re "Password:" return
send "nicetry\r"
expect {
eof { exit 1 }
-re $prompt { send "terminal length 0\r" }
timeout { exit 1 }
}
expect {
eof { exit 1 }
-re $prompt { send "terminal width 60\r" }
timeout { exit 1 }
}
expect {
eof { exit 1 }
-re $prompt { send "show ip route\r" }
timeout { exit 1 }
}
expect {
eof { exit 1 }
-re $prompt { send "quit\r" }
timeout { exit 1 }
}
exit
########## script end #########
########## failed execution start #########
expect version 5.31.5
argv[0] = /usr/local/bin/expect argv[1] = -d argv[2] =
sh-ip-route.exp argv[3] = router1
set argc 1
set argv0 "sh-ip-route.exp"
set argv "router1"
executing commands from command file sh-ip-route.exp
spawn telnet router1
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {19258}
defining key Username:, action return
interact: received eof from spawn_id exp0
send: sending "ops\r" to { exp3 }
defining key Password:, action return
interact: spawn id exp0 not open
while executing
"interact -o -nobuffer -re "Password:" return"
(file "sh-ip-route.exp" line 16)
########## failed execution end #########
Read <URL: http://wiki.tcl.tk/cron >. You're absolutely right
to try running the script outside cron. Next up: run it out-
side cron as user 'nobody' or whatever Solaris uses. A problem
is likely to turn up there.
--
Cameron Laird <Cam...@Lairds.com>
Business: http://www.Phaseit.net
Personal: http://phaseit.net/claird/home.html
I've never seen the above interact -o used in this manner before,
especially in a cron driven script. Maybe substitute a traditional:
if {[info exists spawn_id]} {
expect -re "Username: "
send "$name\r"
exp_continue
}
> send "ops\r"
> interact -o -nobuffer -re "Password:" return
As above..
> send "nicetry\r"
> expect {
> eof { exit 1 }
> -re $prompt { send "terminal length 0\r" }
> timeout { exit 1 }
> }
> expect {
> eof { exit 1 }
> -re $prompt { send "terminal width 60\r" }
> timeout { exit 1 }
> }
> expect {
> eof { exit 1 }
> -re $prompt { send "show ip route\r" }
> timeout { exit 1 }
> }
> expect {
> eof { exit 1 }
> -re $prompt { send "quit\r" }
> timeout { exit 1 }
> }
> exit
> ########## script end #########
>
This whole section is weird..I've never seen so many expect blocks for
something like this...
I would redo this like so:
set i 0;
expect {
-re $prompt { send "terminal length 0\r" ; incr i ;
exp_continue}
-re $prompt { send "terminal width 60\r" ; incr i ;
exp_continue}
-re $prompt { send "show ip route\r" ; incr i; exp_continue}
-re $prompt { send "quit\r" ; incr i ; exp_continue}
timeout {catch {close $spawn_id ; wait} ; exit}
eof {do_alert_proc_for_eof ; exit}
}
If you are having trouble with the command order you can use the $i
count
and if-else to guide the script. Shouldn't be a problem though..
Also you may want to read Chapter 17 in "exploring expect.."
Good Luck.
That "interact -o" idiom comes from the Expect book where it solves
the "optional-answerback" problem. However, using cron, there's no
stdin (i.e., exp0), hence the diagnostic. The solution is to do what
marsd suggested augmented with an expect/send of the answerback prompt
and faked response.
Don