telnet automation script fails sometimes

2019-09-15 14:58发布

问题:

I am running the following simple telnet script which just logs into a machine and exits.

The same script works fine (goes through 1000 iterations) from one Linux server but fails (consistently) from another Linux server (fails after say 200 attempts).

In failure case, the number of iterations it takes to fail varies but failure is persistent.

#!/usr/bin/perl
use Net::Telnet;

my $loop = 0;
my $dumpfile = "dump.log";
my $inputfile = "input.log";

for ($loop =1; $loop <=1000; $loop++) {
        print "===============Loop: $loop =====================\n";
        $telnet = new Net::Telnet ( Timeout=>20, Errmode=>'die');
        print "$telnet\n";
        $telnet->open('sys-007');
        $telnet->dump_log($dumpfile);
        $telnet->input_log($inputfile);
        $telnet->waitfor('/login: $/i');
        $telnet->print('root');
        $telnet->waitfor('/Password:$/i');
        $telnet->print('007');
        sleep 2;
        $telnet->print('exit');
        print "=================================================\n";
}

Script exits with:

pattern match read eof at ./telnettest.pl line 15 (i.e, waitfor('/login: $/i'); Line)

I tried the following to see what is going wrong:

  1. In the client machine: (sys-007)

    tcpdump -nvvv -w test.txt host <Server IP>
    

strings test.txt has:

Successful attempt Log:

sys-007 (ttyp0)
^Fl$4
^!^Fl$
login: 
^Fl$4
^Fl$4
root
^(^Fl*
root
bP"u
^Fl*4
bP5u
^.^Fl*
Password:
^Fl*4
^Fl*4
007
^7^Fl6
^Fl64
^9^Fl6
Terminal type? [xterm] 
^Fl64
^Fl64
exit
^Fl<4
^Fl<
exit
^Fl<

Failed Attempt:

sys-007 (ttyp0)
*^hn
+^hn

No login: prompt!

  1. In server machine: (Linux Server)

    [telnet@server]~% netstat --inet -a | grep telnet | grep sys-007
    
    There are no TIME_WAIT or CLOSE_WAIT sockets.
    

Please tell me what should I look for to find out what is going wrong.

回答1:

Are you able to login normally after this? I'm guessing this other server is cutting you off because you've made too many connections.



回答2:

What I suspect is that the telnet program is waiting for the prompt. I usually use the below format to define the prompt, so that the script usually is able to find the prompt from the below given options.

test  = new Net::Telnet (Timeout => 3000 , Prompt => '/[%#\$>?:] $/' );
$test->open($IPAddress1);
$test->login($Login,$Password1);
my @input=$test->cmd("uname -a\n");
print "Connected to : @input";
@input=$cmd->cmd("pwd\n");

Please let us know if you still face the problem.