|
Home > Archive > Unix Shell > November 2006 > telnet script via expect hanging
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
telnet script via expect hanging
|
|
| kenkahn 2006-11-29, 1:17 pm |
| I have the following telnet script that I run using expect v5.38:
#!/grid/common/bin/expect -f
#
set timeout -1
spawn telnet 158.140.161.167
match_max 100000
expect -exact "NCC_12>"
send -- "setvar(\"ipdstaddr0=158.140.161.62\")\r"
expect -exact "NCC_12>"
send -- "exit\r"
expect eof
Everything works to a point; the telnet session connects and I get the
prompt "NCC_12>". The problem is that the send text always seems to
stop before finishing; i.e. I get
NCC_12> setvar(\"ipdstaddr0=158.140.16
and then nothing; the session appears to hang. It always seems to stop
at the same point. Is there something I'm doing wrong (this is my
first attempt at using expect)?
| |
| Glenn Jackman 2006-11-29, 1:17 pm |
| At 2006-11-29 12:29PM, "kenkahn" wrote:
> I have the following telnet script that I run using expect v5.38:
>
> #!/grid/common/bin/expect -f
> #
> set timeout -1
Don't set an infinite timeout when you're developing an Expect script --
you'll see just the symptom you see. Try 10 (or some reasonable value)
Very useful for debugging is:
exp_internal 1
which allows you to see exactly what expect sees and what it does with
the data.
> spawn telnet 158.140.161.167
> match_max 100000
> expect -exact "NCC_12>"
Don't use -exact when waiting for a prompt. Any command output will
break it. You can use something like this:
expect {
timeout {error "timed out waiting for prompt"}
-re {NCC_12>$}
}
Because you use it frequently, you can generalize this into a proc:
proc expect_prompt {} {
global spawnid ;# important!
expect {
timeout {error "timed out waiting for prompt"}
-re {NCC_12>$}
}
}
Then, when you would otherwise wait for a prompt, you can:
expect_prompt
> send -- "setvar(\"ipdstaddr0=158.140.161.62\")\r"
> expect -exact "NCC_12>"
> send -- "exit\r"
> expect eof
>
> Everything works to a point; the telnet session connects and I get the
> prompt "NCC_12>". The problem is that the send text always seems to
> stop before finishing; i.e. I get
>
> NCC_12> setvar(\"ipdstaddr0=158.140.16
>
> and then nothing; the session appears to hang. It always seems to stop
> at the same point. Is there something I'm doing wrong (this is my
> first attempt at using expect)?
>
--
Glenn Jackman
Ulterior Designer
| |
| johngnub 2006-11-29, 1:17 pm |
| One more silly step, add in a send "\n" before we start looking for the
shell prompt.
`send "\n"`
`expect -exact "NCC_12>" `
This may help set up the shell and prompt, and kills a few mill-secs.
For sure try the exp_internal 1, outputs a LOT of info, but is vary
handy.
Some thing I do to see if my expect got loged in OK to the server,
thus toss up a message that it at least got a login.....
-re "login:|$prompt"
{
puts "Visualise A Sunset."
log_user 1
send "\n"
}
A deffault or time out block is a good idea, , example: in my case, $x
is the host name, $d is the date....
default
{
puts "Zarquon :: $x $d, I did not login, Exiting.\n"
exit
}
kenkahn wrote:
> I have the following telnet script that I run using expect v5.38:
>
> #!/grid/common/bin/expect -f
> #
> set timeout -1
> spawn telnet 158.140.161.167
> match_max 100000
> expect -exact "NCC_12>"
> send -- "setvar(\"ipdstaddr0=158.140.161.62\")\r"
> expect -exact "NCC_12>"
> send -- "exit\r"
> expect eof
>
> Everything works to a point; the telnet session connects and I get the
> prompt "NCC_12>". The problem is that the send text always seems to
> stop before finishing; i.e. I get
>
> NCC_12> setvar(\"ipdstaddr0=158.140.16
>
> and then nothing; the session appears to hang. It always seems to stop
> at the same point. Is there something I'm doing wrong (this is my
> first attempt at using expect)?
| |
| johngnub 2006-11-29, 1:17 pm |
| Opps: forgot, -1 timeouts can have expect waiting for some to go wrong,
set that to some value, 360, etc.. Then after I get my login, I bring
it down some.... set timeout 120.
2 cents ...
johngnub wrote:[vbcol=seagreen]
> One more silly step, add in a send "\n" before we start looking for the
> shell prompt.
> `send "\n"`
> `expect -exact "NCC_12>" `
>
> This may help set up the shell and prompt, and kills a few mill-secs.
> For sure try the exp_internal 1, outputs a LOT of info, but is vary
> handy.
>
> Some thing I do to see if my expect got loged in OK to the server,
> thus toss up a message that it at least got a login.....
>
> -re "login:|$prompt"
> {
> puts "Visualise A Sunset."
> log_user 1
> send "\n"
> }
>
> A deffault or time out block is a good idea, , example: in my case, $x
> is the host name, $d is the date....
>
> default
> {
> puts "Zarquon :: $x $d, I did not login, Exiting.\n"
> exit
> }
>
>
>
>
> kenkahn wrote:
| |
| kenkahn 2006-11-29, 7:22 pm |
| Thanks for all the suggestions; even though they haven't yet resolved
the problem, I am learning a good deal. I'm trying to get a copy of
"Exploring Expect" which seems to be "the" book for expect.
Getting back to my problem, is there a default maximum length for the
text sent by the send command? If I change
send -- "setvar(\"ipdstaddr0=158.140.161.62\")\r"
to
send -- "setvar(\"ipdstaddr0=158.140")\r"
it works.
| |
| Glenn Jackman 2006-11-30, 1:33 am |
| At 2006-11-29 04:20PM, "kenkahn" wrote:
> Thanks for all the suggestions; even though they haven't yet resolved
> the problem, I am learning a good deal. I'm trying to get a copy of
> "Exploring Expect" which seems to be "the" book for expect.
>
> Getting back to my problem, is there a default maximum length for the
> text sent by the send command? If I change
>
> send -- "setvar(\"ipdstaddr0=158.140.161.62\")\r"
>
> to
>
> send -- "setvar(\"ipdstaddr0=158.140")\r"
>
> it works.
I don't think that's the answer. Did the output from "exp_internal 1"
help?
--
Glenn Jackman
Ulterior Designer
| |
| kenneth kahn 2006-11-30, 7:27 am |
| >> Getting back to my problem, is there a default maximum length for the
>
> I don't think that's the answer. Did the output from "exp_internal 1"
> help?
>
Not really. Everything looked OK as far as I can tell; the prompts were found
and the send, with all the text, was issued. However, the full text didn't show
up on the telnet server side. I'm beginning to think it might be a problem with
the telnet server itself. The system I'm connecting to is running IBM's OS
Open; this is a flavor of Unix, not quite AIX. I've tried issuing the same
expect script to other systems (Solaris and Linux) and it works fine, so I have
to assume it's not a problem with expect, but rather with the telnet server
running on OS Open. We've had other 'issues' with it's network support; the OS
was last updated almost 10 years ago now.
I guess I could try using rsh or ssh; I have to verify if OS Open supports these.
| |
| kenkahn 2006-11-30, 7:27 am |
| If anyone is interested, I got it to work as follows:
send -- "setvar(\"ipdstaddr0="
expect "setvar"
send -- "158.140.161.62\")\r"
| |
| kenkahn 2006-11-30, 1:17 pm |
| kenkahn wrote:
> If anyone is interested, I got it to work as follows:
>
>
> send -- "setvar(\"ipdstaddr0="
> expect "setvar"
> send -- "158.140.161.62\")\r"
Actually, I found a better (?) way of doing this:
set send_slow {1 .001}
send -s -- "setvar(\"ipdstaddr0=158.140.161.62\")\r"
expect "setvar(\"ipdstaddr0=158.140.161.62\")\r
This slows down the transmission enough to allow the entire string to
be sent without splitting it up.
|
|
|
|
|