There are lots of sample codes on the web (do a google search on "telnet
program using expect).

Here is one of the links
http://www.elektro.com/~mlud/pettefar.de/docs/html/v08/i03/a3.htm


Also here is a sample code I wrote for one of my codes I found lying
around.  You can clean it up and use it.

#!/usr/X11R5/bin/expect
#----------------------------------------------------------
#PROGRAM:   proberouter_new.xpt
#PURPOSE:   sends a probe to a given router and records the
#        current configuration information in a text file
#DATE:      Thu Sep 30 21:50:42 EDT 1999
#UPDATED:   Thu Mar 30 12:59:23 EST 2000
#----------------------------------------------------------

set timeout 180               ;#set timeout here; -1 disables time out

#................ telnet processbegins here ...............
if {[llength $argv] < 6} {
      puts "\n\n ***********************************************"
      puts "\n\tUSAGE: $argv0 {hostname} {username} {tacacs_password}
{line_password} {enable_password} output_file\n"
      puts "\n\t E.g.: $argv0 router4 username4 tacacs_passwd4 line_passwd4
enable_passwd4 output_file\n"
      puts "\n ***********************************************\n\n\n"
      exit 1
}

#...........................................................
#log_user 0                                   ;#0 = disable spawn output
#exp_internal 1                            ;#print diagnotics -- FOR
TESTING
#
set TERMINAL_LENGTH 0
#
set hostname [lindex $argv 0]
set username [lindex $argv 1]
set tacacs_password [lindex $argv 2]
set line_password [lindex $argv 3]
set enable_password [lindex $argv 4]
set output_file [lindex $argv 5]

#.................. clean them up, remove \ ....
regsub -all {\\} $hostname {} hostname
regsub -all {\\} $username {} username
regsub -all {\\} $tacacs_password {} tacacs_password
regsub -all {\\} $line_password {} line_password
regsub -all {\\} $enable_password {} enable_password
regsub -all {\\} $output_file {} output_file

# .............................................

puts "\nhost = $hostname\n user = $username\n ep = $enable_password\n lp =
$line_password \n e= $enable_password\n\n"
#.......................testing ...................................

set command "./my_router.sh"         ;#good configuration
#set command "./my_router_2.sh"      ;#bad configuration

spawn $command [lindex $argv 0]

#.................... production ..................................
#spawn /usr/bin/telnet [lindex $argv 0]
#

log_file -noappend $output_file      ;#to record what we want

expect_after {
      eof { puts "\n\n         * * *   ERROR!!!    $command $hostname
terminated unexpectedly.  * * * \n\n\n" }
      exit 1
}
#

#...................... BLOCK 1: up to line password ...........
set loop_detector1 0
set loop_detector2 0
set tacacs_passwd 0               ;#assume tacacs_passwd not needed

while 1 {
      if { $loop_detector1 > 3 || $loop_detector2 > 3 } {
            puts "\n\nBLOCK 1:  Username or Password Error.  Use modify
router to update $hostname information!!!\n\n"
            exit 1
      }

      expect  {
            timeout { puts "\n\nWarning:  Process timed out  -- BLOCK
1\n\n"; exit 1 }
            -nocase eg: { puts "\n\n Error from $hostname.  Possibly a
password error - Blk 1. Modify rounter information\n\n"; exit 1}
            -nocase error: {puts "\n\n Error from $hostname.  Possibly a
password error - Blk 1. Modify rounter information\n\n"; exit 1}
            -nocase error {puts "\n\n Error from $hostname.  Possibly a
password error - Blk 1. Modify rounter information\n\n"; exit 1}
            -nocase authentication { puts "\n\n Error from $hostname.
Authentication - Blk 1. Modify rounter information\n\n"; exit 1}
            -nocase bad { puts "\n\n Error from $hostname.  Possibly a
password error. Modify rounter information\n\n"; exit 1}
            "%" { puts "\n\n Error from $hostname.  Possibly a password
error. Modify rounter information\n\n"; exit 1}
            "User Access Verification" {incr loop_detector1}
            Username: {stty -echo; send "$username\r"; stty echo; incr
tacacs_passwd; incr loop_detector2}
            Password: { if { $tacacs_passwd > 0 } {
                        stty -echo; send "$tacacs_password\r"; stty echo;
break
                  } else {
                        stty -echo; send "$line_password\r"; stty echo;
break
                  }
            }
            eof { puts "\n\n Error from $hostname.  End of file error --
blk 1. Modify rounter information\n\n"; exit 1 }
      }
}

#........................ BLOCK 2: show config ..............
set loop_detector1 0
set loop_buf_in_use 0
set out_buf_in_use 0

while 1 {
      if { $loop_detector1 > 3 } {
            puts "\n\nBLOCK 2:  Username or Password Error.  Use modify
router to update $hostname information!!!\n\n"
            exit 1
      }

      expect {
            timeout { puts "\n\nWarning:  Process timed out  -- BLOCK
2\n\n"; exit 5 }
            Password: { stty -echo; send "$enable_password\r" ; stty echo;
incr loop_detector1}
            "#"  { if { $TERMINAL_LENGTH < 1 } {
                                    stty -echo; send "terminal length 0\r";
stty echo; set TERMINAL_LENGTH 1
                              } else {

                                    while 1 {
                                          #puts "\n .......... TRIAL
$loop_buf_in_use  .... \n"
                                          if { $loop_buf_in_use > 3 } {
                                                puts "\n\nBLOCK 2:  ERROR
from $hostname.  Possibly a buffer in use.  Trials =
$loop_buf_in_use!!!\n\n"
                                                exit 1
                                          }
                                          stty -echo; send "show config\r";
stty echo; incr loop_buf_in_use
                                          #expect "show config\r\n"
                                          expect {
                                                 timeout { puts
"\n\nWarning:  Process timed out on show config -- BLOCK 2\n\n"; exit 5 }
                                                -re "(\r\n)*.*!" { incr
out_buf_in_use; break }
                                                -re "\r\n(\[#\n\r]*)" {
sleep 15 }
                                                eof { puts "\n\n Error from
$hostname.  End of file error -- blk 2.  Possibly a buffer in use!!\n\n";
exit 1 }
                                          }
                                    }

                              }
                              if { $out_buf_in_use > 0 } {
                                    break
                              }
                        }
            -nocase error: {puts "\n\n Error from $hostname.  Possibly a
password error -- Blk 2. Modify rounter information\n\n"; exit 1}
            -nocase error {puts "\n\n Error from $hostname.  Possibly a
password error -- Blk 2. Modify rounter information\n\n"; exit 1}
            -nocase bad { puts "\n\n Error from $hostname.  Possibly a
password error -- Blk 2. Modify rounter information\n\n"; exit 1}
            "%" { puts "\n\n Error from $hostname.  Possibly a password
error -- Blk 2. Modify rounter information\n\n"; exit 1}
            ">" {stty -echo; send "enable\r"; stty echo }
            eof { puts "\n\n Error from $hostname.  End of file error --
blk 2. Modify rounter information\n\n"; exit 1 }
      }
}


# ..............This is where I am having lots of trouble .............
#expect -re "\r\n(.*)\r\n"                 ;#match output fron show config
expect -re "\n(\[#\r\n]*)\r"                ;#match output fron show config
#expect -re "\r\n(\[#\n\r]*)\r\n"          ;#match output fron show config
#expect -re "\r\n(\[#\n\r]*)"               ;#match output fron show config
#expect -re "\n(\[^\r]*)\r"                ;#match output fron show config
log_user 1; puts "$expect_out(1,string)"   ;#print expected data only to
outfile


#......................  close session and exit ...............
expect {
      "#" { exit 0 }
      eof { puts "\n\n Error from $hostname.  End of file error -- blk 3.
Modify rounter information\n\n"; exit 1 }
      timeout { puts "\n\nWarning:  Timed out: Last BLOCK\n\n"; exit 6 }
}

#...............................................................
exit 1
__________________

William Ampeh (x3939)
Federal Reserve Board


                                                                                       
                                           
                      "Ohad Ohad"                                                      
                                           
                      <[EMAIL PROTECTED]         To:      [EMAIL PROTECTED]            
                                            
                      .com>                    cc:                                     
                                           
                                               Subject: Perl <-> Expect                
                                           
                      07/15/2004 07:36                                                 
                                           
                      AM                                                               
                                           
                                                                                       
                                           
                                                                                       
                                           




Hi,

I need to telnet some hosts automatically without supplying password.

I can do it with Expect but I hardly know the language.

I wonder what you think of the Perl module Expect - if it's any good and
easy to use.

Also if you can think of another way to do the job this will also be great.

Thanks

_________________________________________________________________
Help STOP SPAM with the new MSN 8 and get 2 months FREE*
http://join.msn.com/?page=features/junkmail


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to