Professional Web Applications Themes

Perl <-> Expect - PERL Beginners

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

  1. #1

    Default Perl <-> Expect

    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

    Ohad Guest

  2. #2

    Default Re: Perl <-> Expect



    Hi,
     
    great.

    I use ssh for lots of this type of stuff. Set up keys for authentication,
    then you have autologin.
    Depending on what you need after you get into the remote host, this could
    be a good option.

    I guess that had nothing to do with Perl :(

    hth
    jeff
     

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


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







    Jeffrey Guest

  3. #3

    Default Re: Perl <-> Expect

    ssh is not an option - I must use telnet

     
    >great.
    >
    >I use ssh for lots of this type of stuff. Set up keys for authentication,
    >then you have autologin.
    >Depending on what you need after you get into the remote host, this could
    >be a good option.
    >
    >I guess that had nothing to do with Perl :(
    >
    >hth
    >jeff

    >
    >_________________________________________________ ________________
    >Help STOP SPAM with the new MSN 8 and get 2 months FREE*
    >http://join.msn.com/?page=features/junkmail
    >
    >
    >--
    >To unsubscribe, e-mail: org
    >For additional commands, e-mail: org
    ><http://learn.perl.org/> <http://learn.perl.org/first-response>
    >
    >
    >
    >
    >
    >
    >[/ref]

    __________________________________________________ _______________
    The new MSN 8: advanced junk mail protection and 2 months FREE*
    http://join.msn.com/?page=features/junkmail

    Ohad Guest

  4. #4

    Default RE: Perl <-> Expect

    >> 
    >>great.[/ref][/ref]


    I've used expect before- i don't have the script with me :/

    I'll try to dig it up...

    Also, using Net::Telnet can work... it's a little more complex,
    but feels more like Perl :) well, it /is/ Perl... *laugh*


    if you use expect then here is one way to do it :

    open (IN, "$expect_script |");#pipe the output of the expect script
    #to a filehandle

    while (<IN>){
    manipulate data
    }

    you can't do much to manipulate the expect script here, but it lets you grab
    the results.


    If you want a Perlish way to login outomatically, look at the Perl expect
    module (i haven't - and i understand it's a bit complicated) or look at
    Net::Telnet. I recommend the latter. It may be complex, but the
    doentation is fairly complete. It will take practice getting used to it.


    regards,

    willy



    http://www.hackswell.com/corenth

    William Guest

  5. #5

    Default Re: Perl <-> Expect





    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"
    <ohad_nghotmail To: org
    .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: org
    For additional commands, e-mail: org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>





    William Guest

Similar Threads

  1. using Net::SSH::Perl with Expect
    By dan byers in forum PERL Modules
    Replies: 0
    Last Post: November 21st, 08:41 PM
  2. Perl Expect question
    By Tim in forum PERL Modules
    Replies: 0
    Last Post: November 8th, 07:20 PM
  3. Telling perl to expect input data to be in UTF8
    By Graham Wood in forum PERL Miscellaneous
    Replies: 4
    Last Post: September 5th, 05:47 PM
  4. Replies: 0
    Last Post: July 28th, 06:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139