Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to autotelnet.exp < tail -f /tmp/$$ <-- plain text?

Status
Not open for further replies.

codiau

Technical User
Apr 4, 2002
4
US
How to?
autotelnet.exp < tail -f /tmp/$$
/tmp/$$ is <-- plain text with
string commands, such as:

status
!ls
open localhost
quit

I need to use &quot;tail -f&quot; because I
don't want to close the process or look for an EOF this could conceivably use many processes to write to /tmp/$$

############################################
#This is what I've tried so far in a
#expect script called tel-server.exp

set timeout -1
spawn telnet
match_max 100000
expect -exact &quot;telnet> &quot;

send -- &quot;status\r&quot;
expect -exact &quot;status\r
No connection.\r\r
Escape character is '^\]'.\r\r

#########################################
#It is at this point that I would like
#to send strings of #commands to this #interactive telnet session. The key,
#is that I need the strings to come from a #file via:
# tail -f /tmp/$$
# It has to be tail on a file, no
# pipes or redirection
# This is useful for doing multiple
# telnet sessions to query a great
# many ports/services all from one
# telnet client.

telnet> &quot;
### DO I??
while [ gets &quot;/tmp/$$ line ] {
sleep 1
send -- $line
}

expect eof

# If you &quot;?&quot; at the telnet prompt
# you will see the type of strings
# that will need to be in /tmp/$$.
# Replys greatly needed. Thanks!
 
You can do this a couple of ways:

Possible addition:
The global pattern is scanned for all occurrences
of eof. This saves some time coding eof for every transaction sequence.

expect_after eof {
send_user &quot;Caught an unexpected eof from $spawn_id&quot;
}
}

First way:
set cmdfile [open &quot;tmpfilename&quot; r]
set i 1
while {[gets $cmdfile line] > 0) {
incr i ; set commands($i) $line
send &quot;$line\r&quot;
sleep 1s
expect {
-re &quot;pattern&quot; {
send_user &quot;$expect_out(buffer), transfer okay.&quot;
exp_continue
}

-re &quot;badpattern1&quot; {
send_user &quot;Failed to get an acceptable
response from $spawn_id.&quot;
sleep 1s
#dangerous::send &quot;$commands($i)\r&quot;
exp_continue
}

This way has the advantage of being much simpler
based on what you have said so far and does give you
some flexibility when used in conjunction with a
helper data structure for remembering and resending
commands based on certain instances.

The other way is through expects native spawn -open
facility which allows you to use pat matching and
action selection based on the expect code used.
This is more complex but could be very precise.

Good Luck
 
I tried your code and it works right until it needs to grab the commands from the /tmp/2 file. Sometimes it waits and then blasts away fast, sometimes too fast, the sleep commands ( sleep 5s) seem to work sometimes. Below is a copy of the script I used. The command file '/tmp/2' contains :
status
display
open localhost
username
passwd

This script is drafted from your suggestions and autoexpect.

Thank you in advance.

#!/usr/local/bin/expect -f


set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}

set timeout -1
spawn telnet
match_max 100000
expect -exact &quot;telnet> &quot;
send -- &quot;status\r&quot;
expect -exact &quot;status\r
No connection.\r\r
Escape character is '^\]'.\r\r
telnet> &quot;

set cmdfile [open &quot;/tmp/2&quot; r]
set i 1
while {[gets $cmdfile line] > 0 } {
incr i ; set commands($i) $line
send &quot;$line\r&quot;
sleep 1s
}
expect {
-re &quot;pattern&quot; {
send_user &quot;$expect_out(buffer), transfer okay.&quot;
exp_continue
}

-re &quot;badpattern1&quot; {
send_user &quot;Failed to get an acceptable
response from $spawn_id.&quot;
sleep 1s
#dangerous::send &quot;$commands($i)\r&quot;
exp_continue
}

}
 
Okay.
If you are using the code verbatim, then we have
a problem.


The code given was an example: I am amazed that anything
matches at all if you are using the script as it is
written above. ;)

Usually what I do for a telnet script is create a global
array of prompts and strings, commands, etc..and then
feed them to the spawned process.
Unless you have a lot more than five or six lines in your
file I wouldn't even use the file portion, or at least
just load the file into the array.
If you decide to do this a proc like this may help.

proc loader {FILE {i &quot;1&quot;}} {
global everything
set fd [open &quot;$FILE&quot; r]
set mycmds [read $fd]
foreach line $mycmds {
incr i
set everything($i) $line
}
return
}

Otherwise something like this should work as well.

#!/usr/bin/expect

array set everything {

1 status
2 display
3 open localhost
4 username
5 passwd
6 &quot;.*@.*&quot;
cmd,4 &quot;my username here&quot;
cmd,5 &quot;my passwd here&quot;
}

spawn telnet
set everything(id) $spawn_id

expect {

-re &quot;.*eln.*&quot; {
send_user &quot;Got telnet prompt,process opened.\n&quot;
send_user &quot;$expect_out(buffer).&quot;
send &quot;$everything(1)\r&quot;
exp_continue
}

-re &quot;$everything(1).*&quot; {
send_user &quot;Got status prompt line.\n&quot;
send_user &quot;$expect_out(buffer).&quot;
send &quot;$everything(3)\r&quot;
exp_continue
}

-gl &quot;$everything(4)&quot; {
send_user &quot;Got login prompt.&quot;
send &quot;$everything(cmd,4)\r&quot;
exp_continue
}

-gl &quot;$everything(5)&quot; {
send_user &quot;Got passwd prompt.&quot;
send &quot;$everything(cmd,5)\r&quot;
expect -re &quot;$everything(6)&quot; {
interact {}
}
}

timeout {
send_user &quot;Connection timed out.&quot;
exit
}

eof {
send_user &quot;Unexpected eof.&quot;
exit
}
}

Please check out Don Libes &quot;exploring expect&quot; book
and the examples that come with expect. For these
types of exercises they are invaluable.









 
OK, I thnk I'll try it AND get the book. Thanks a lot for your time.

Regards,
Clint
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top