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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help in spawn telnet

Status
Not open for further replies.

himayun1

Technical User
Oct 16, 2003
2
US
Hi
I am trying to telnet to a Unit under test
from my SUSE Linux via expect script
following is my scrip

#!/usr/bin/expect
set tel 192.168.1.136
set port 6500
spawn telnet $tel $por

but my scrip do not telnet to unit but instead just executes the command as it is
the result
spawn telnet 192.168.1.136 6500
please help
Thank
HZ
 
Either you need to use eval(doubtful) or you are
not giving us details we need to help you.
Try 'man autoexpect' and see if a
script generated using autoexpect gives you
any clues before reposting.
 
Telnet via autoexpect is scuccessfull but when I use
the code generated via file (script.exp) of autoexpect
it fails
following is the output
1)Telnet to UUT from autoexpect
nux:~/home # autoexpect telnet 192.168.1.136 6500
autoexpect started, file is script.exp
Trying 192.168.1.136...
Connected to 192.168.1.136.
Escape character is '^]'.

M,2
S,M,0
"^],0
telnet> exit
?Invalid command
telnet> logout

E,2
'^]
telnet> '
?Invalid command
telnet> '^]'
?Invalid command
telnet> quit
Connection closed.
autoexpect done, file is script.exp

2) Code vinux:~/home # more script.exp
#!/usr/bin/expect -f
#
# This Expect script was generated by autoexpect on Mon Oct 20 19:21:46 2003
# Expect and autoexpect were both written by Don Libes, NIST.
#
# Note that autoexpect does not guarantee a working script. It
# necessarily has to guess about certain things. Two reasons a script
# might fail are:
#
# 1) timing - A surprising number of programs (rn, ksh, zsh, telnet,
# etc.) and devices discard or ignore keystrokes that arrive "too
# quickly" after prompts. If you find your new script hanging up at
# one spot, try adding a short sleep just before the previous send.
# Setting "force_conservative" to 1 (see below) makes Expect do this
# automatically - pausing briefly before sending each character. This
# pacifies every program I know of. The -c flag makes the script do
# this in the first place. The -C flag allows you to define a
# character to toggle this mode off and on.

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
}
}

#
# 2) differing output - Some programs produce different output each time
# they run. The "date" command is an obvious example. Another is
# ftp, if it produces throughput statistics at the end of a file
# transfer. If this causes a problem, delete these patterns or replace
# them with wildcards. An alternative is to use the -p flag (for
# "prompt") which makes Expect only look for the last line of output
# (i.e., the prompt). The -P flag allows you to define a character to
# toggle this mode off and on.
#
# Read the man page for more info.
#
# -Don


set timeout -1
spawn telnet 192.168.1.136 6500
match_max 100000
expect -exact "Trying 192.168.1.136...\r\r
Connected to 192.168.1.136.\r\r
Escape character is '^\]'.\r\r
"
send -- "\r"
expect -exact "\r
E,2\r"
send -- "M\r"
expect -exact "M\r
=,M,0\r"
send -- "S\r"
expect -exact "S\r
=,S,0\r"
send -- "\""
expect -exact "^\]\r
telnet> "
send -- "exit\r"
expect -exact "exit\r
?Invalid command\r\r
telnet> "
send -- "logout\r"
expect -exact "logout\r
"
send -- "\r"
expect -exact "\r
E,2\r"
send -- "\r"
expect -exact "\r
E,2\r"
send -- "'"
expect -exact "^\]\r
telnet> "
send -- "'\r"
expect -exact "'\r
?Invalid command\r\r
telnet> "
send -- "'"
ia script.exp

3) I use the above script with modifications naming a file
myscript#!/usr/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 192.168.1.136 6500
match_max 100000

4) The result of this script is

~
linux:~/home # ./myscript
spawn telnet 192.168.1.136 6500
linux:~/home #

5) I want to telnet to my UUT (Unit under test)
and send some commands.

6) my orignal file was as follow
I tried the spawn telnet with quots and without quotes
with \r, \n also
#!/usr/bin/expect -f
send "this\n"
send "is a test\n"
spawn telnet "192.168.1.136 6500\n"

7)Please help thanks
HZ
 
You should buy Don Libes "Exploring Expect" and work
through the examples.
Your example doesn't make any logical or programmatic
sense I'm afraid.

If I was to write a script to grab mail service
information I would do something like this.
Code:
#!/usr/bin/tclsh
package require Expect

#################bailout if no target for connect.
if {![llength $argv] > 0} {error "Must give a target host for connect!!"}
#################globals#################
set host [lindex $argv 0]
set port [expr {[string length [lindex $argv 1]] > 0 ? [lindex $argv 1] : 25}] 
set timeout 55


##MAIN()######################
spawn -noecho telnet $host $port


      expect {

               -re "ESMTP(.*)\n" {
                                     send_user "Snarfed id string at: $expect_out(1,string)\r\n"
                                     close ; wait
                }

                eof {send_user "\n Spawn Id: $spawn_id , died unexpectedly\n"}
                timeout {send_user "\n Spawn Id: $spawn_id terminated without matching.."}
       }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top