0

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.

RobEarl
  • 7,862
  • 6
  • 35
  • 50
Ram
  • 1,153
  • 4
  • 16
  • 34
  • your tcpdump shows that you're simply not getting the expected prompt from the server. You'll have to check the logs on the server to see if there is anything useful there. You could also use `strace -ff` to attach to the telnetd process to see what syscalls it is making -- that might give a hint. – Brian White Oct 04 '12 at 17:43

2 Answers2

0

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.

CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
  • @Ram Well, I'm not sure what to say, the thing about eof is basically saying that the server is disconnecting. Are you able to login manually from the machine you're running this script on as the script is failing? – CrazyCasta Sep 26 '12 at 06:54
  • yes, I can login manually. Even If I re-run the script, it does go through few more attempts and ultimately fails. – Ram Sep 26 '12 at 06:55
  • Well, I don't know what to say except that it appears that your server is dropping/refusing the connection for some reason. – CrazyCasta Sep 26 '12 at 06:59
0

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.

Vivek
  • 41
  • 2
  • You will notice in his packet capture that the server doesn't even seem to be sending anything (if I understand the output properly). – CrazyCasta Oct 03 '12 at 23:46