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

telneting w/use of expect and/or tcl scripts

Status
Not open for further replies.

fgg123

IS-IT--Management
Mar 7, 2003
6
US
Scenario:

1. Logged into a host UNIX (System A).
2. Telneting to a switch (1.2.3.4) that requires a
login/password for access. (telnet is the only method
allowed to enter switch)
3. Running a display command (e.g.: display 123/S 23/A 14)
to capture data back to a file on host UNIX (System A).

Trying to use a here document on System A as follows:

#####################################################
get-switch-data.sh

telnet 1.2.3.4 <<-EOF 2>&1 > /home/switch/sw-data/log-data
guest
guest
display 123/S 23/A 14
EOF

#####################################################

Trouble I'm running into, is with the login/password
required. How can I pass it(login/password) to telnet (I know about the -l login) option on command line telnet -l guest, but no password option), and get my log-data from the command being passed.

Anny help, Thanks!

I am also looking at trying the following with an expect script and/tcl.

I am not a user of expect/tcl so, if someone could/would
just assist me on the following to see if it is steering
me in the right direction, I would appreciate it.

Thanks!
FGG
#####################################
#!/usr/bin/expect --

spawn /usr/bin/telnet
expect &quot;telnet>&quot;
send &quot;open YOUR_IP_HERE\r&quot;
expect &quot;ogin&quot; # In case login is &quot;Login or login&quot;
send &quot;YOUR_LOGIN_HERE\r&quot;
expect &quot;ssword&quot; # In case password is &quot;Password or password&quot;
send &quot;YOUR_PW_HERE\r&quot;
expect &quot;SOME_PROMPT (I.E. $>)&quot;
send &quot;YOUR_COMMANDS_HERE\r&quot;
expect &quot;SOME_PROMPT (I.E. $>)&quot;
send &quot;exit\r&quot;
expect eof


To log, just run script name > log 2>&1
#####################################3

FGG123
 
This is very simple with expect.
Expect can log everything with the log_file
command.

Your example is fairly simplistic.
Try something like this:
Code:
proc getuserIn {msg} {
 stty -echo
 send_user $msg
 set resp [gets stdin]
  
   if {[string length $resp]} {
      stty echo
      return $resp
   }
stty echo
return -1
}

proc cb {var varind tt} {
    if {$var > 2} {
        send_user &quot;WARNING: You have either fatfingered 
        an entry or there is a looping problem with the      
        expected program output.&quot;
        uplevel #0 &quot;set cnt 0&quot;
        }
return 1
}

set timeout 125 
set cmd &quot;display 123/S 23/A 14&quot;
spawn telnet [lindex $argv 0]
set cnt 0
trace add variable cnt w [list cb]
log_file [lindex $argv 1]

   expect {
 
          -re &quot;.*ogin.*&quot; {
             set ans [getuserIn &quot;Login: &quot;]
             send &quot;$ans\r&quot;
             incr cnt
             exp_continue
          }


          -re &quot;.*asswo.*&quot; {
             set ans [getuserIn &quot;Password: &quot;]
             send &quot;$ans\r&quot;
             incr cnt
             exp_continue
          }

          eof  {send_user &quot;ABEND of telnet session! last message read was: $expect_out(buffer)\n\n&quot; }
 
          timeout { send_user &quot;Connection with for [lindex $argv 0] timed out unsuccessfully\n\n&quot; }

    }

Untested but it should work well enough.
Called like:
scriptname address/name of switch logfilename

HTH
 
THanks for the support/help.
The solutiuons provided worked just fine for me.

Regards!
FGG123
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top