Capturing Port Data

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I could not get the yahoo modules to work, nor did I have success with
IO::Socket. So now I'm trying to use Net::Telnet. I think the problem has
to do with needing two returns after $msg. I've tried using both print and
cmd without success.

Anybody know why I'm not able to capture the output?

use strict;
use warnings;
use Net::Telnet;

my $msg = 'GET /capacity HTTP/1.1';
my $port = 5050;
my $timeout = 5;

my $telnet = new Net::Telnet (
             Timeout   => $timeout,
             Errmode   => 'die',
             Port      => $port,);


  print "No connection\n";
else {
  # Tried using combinations of print and cmd
  my @output1 =  $telnet->print($msg);
  print "First:  $_\n" foreach(@output1);

  my @output2 =  $telnet->print("\n");
  print "Second: $_\n" foreach(@output2);


I'm trying to capture the following: "CS_IP_ADDRESS="


Re: Capturing Port Data

Quoted text here. Click to load it

What's wrong with LWP::Simple?


Re: Capturing Port Data

Quoted text here. Click to load it

Hi Ben,

I read the info about LWP::Simple, but I don't see how that will help. I
should have been able to do this program using IO::Socket, but couldn't get
it to work. (I've written several other programs using IO::Socket without
running into this problem.) It appears the same issue that is preventing me
from being successful using IO::Socket is probably the same issue I am
encountering with Net::Telnet.

Thanks for the suggestion.


Re: Capturing Port Data

Quoted text here. Click to load it

Well, AFAICS you are trying to do a GET of
http://88.xx.xx.161:5050/capacity (sorry, I can't remember the exact IP
address). What happens if you simply pass that URL to LWP::Simple::get?


Re: Capturing Port Data

Quoted text here. Click to load it

Interesting. This actually works. It does not give me all of the info I get
when I telnet to the port, but it at least returns the IP address.

Even though nobody could help with the Net::Telnet (which I have used
successfully for other problems), you came up with a viable solution.

Thank you very much Ben!

Re: Capturing Port Data

Quoted text here. Click to load it

I strongly suspect that if you use the full LWP::UserAgent rather than
LWP::Simple you will find the rest of the information in the headers of
the returned HTTP::Response.


Re: Capturing Port Data

On Sun, 19 Apr 2009 21:37:44 +0000, Cosmic Cruizer wrote:

Quoted text here. Click to load it

You need to read the documentation of Net::Telnet more carefully.

Re: Capturing Port Data

Ben Bullock wrote:
Quoted text here. Click to load it

Looks like you are trying to log into a router and get the IP address...
As this is in this forum you may need to ensure proper line lengths if
you copy/paste.

this is what I have used for my linksys router for ages now...

#!/usr/bin/perl -w
## $ENV = (exists($ENV) ? "$ENV:" : "") .
## "/sbin:/usr/sbin:/bin:/usr/bin:/etc:/usr/lib:";

use Getopt::Long;
use Sys::Hostname;
use IO::Socket;

sub encode_base64 ($;$) {
     my $res = '';
     my $eol = $_[1];
     $eol = "\n" unless defined $eol;
     pos($_[0]) = 0;                          # ensure start at the
     while ($_[0] =~ /(.)/gs) {
         $res .= substr(pack('u', $1), 1);
     $res =~ tr|` -_|AA-Za-z0-9+/|;               # `# help emacs

     # fix padding at the end
     my $padding = (3 - length($_[0]) % 3) % 3;
     $res =~ s/.$/'=' x $padding/e if $padding;

     # break encoded string into lines of no more than 76 characters each
     if (length $eol) {
         $res =~ s/(.)/$1$eol/g;
     $0 = "findIP";
     my $version  = "3.5.4";
     my $programd  = $0;
     $programd =~ s%^.*/%%;
     my $program   = $programd;
     $program  =~ s/d$//;
     my $now       = time;
     my $hostname  = hostname();
     my ($peer, $server, $port);
     my ($sd, $rq, $request, $reply);
     my $url = "HTTP:// ";

     $url    =~ s%^HTTP://%%i;
     $server = $url;
     $server =~ s%/.*%%;
     $url    = "/" unless $url =~ m%/%;
     $url    =~ s%^[^/]*/%%;

     ## determine peer and port to use.
     $peer   = $server;
     $peer   =~ s%/.*%%;
     $port   = $peer;
     $port   =~ s%^.*:%%;
     $port   = 80 unless $port =~ /^\d+$/;
     $peer   =~ s%:.*$%%;

     my $auth = encode_base64("doesntmatter:mypassword");
     $request  = "GET ";
     $request .= "/$url HTTP/1.0\n";
     $request .= "Authorization: Basic $auth";
     $request .= "User-Agent: $/$0.6\n";
     $request .= "Connection: open\n";
     $request .= "\n";

     ## make sure newlines are <cr><lf> for some pedantic proxy servers
     ($rq = $request) =~ s/\n/\r\n/g;

# uncomment the following for debug mode.
#        printf("$rq");

     local $^W = 0;
    $sd = IO::Socket::INET->new(PeerAddr => $peer,
                                  PeerPort => $port,
                                  Proto    => 'tcp');
     my $skip='interipaddr';
     my $DNS1='script>1';
     my $DNS2='script>2';
     my $DNS3='script>3';
     my $result = send $sd, $rq, 0;
         if ($result != length($rq)) {
             printf("cannot send to ($!).\n");
         } else {
             while ($_ = <$sd>) {
#        verbose("RECEIVE:", "%s", define($_, "<undefined>"));
                 $reply .= $_ if defined $_;
# uncomment the following for debug mode - additional information
#       printf(" $reply\n");
             if ($reply =~
                 $WANip = $1;
             if ($reply =~
                 $DNS1 = $1;
             if ($reply =~
                 $DNS2 = $1;
             if ($reply =~
                 $DNS3 = $1;
         printf("Current IP address is: $WANip\n");
         printf("DNS 1   IP address is: $DNS1\n");
         printf("DNS 2   IP address is: $DNS2\n");
         printf("DNS 3   IP address is: $DNS3\n");

sampleoutput (munged to protect the guilty :)
Current IP address is: 555.555.555.140
DNS 1   IP address is: 556.556.556.1
DNS 2   IP address is: 557.557.557.1
DNS 3   IP address is: script>3       <-- obviously this does not exist.

Re: Capturing Port Data

Quoted text here. Click to load it

I'm not sure if it's relevant to your problem (and you don't
tell what exactly goes wrong), but the message you send to
the server

Quoted text here. Click to load it

looks wrong. HTTP version 1.1 requires you to send quite a
bit more than just that (you must send at least another line
starting with 'Host:' and followed by the name of the client).
For a start try HTTP version 1.0 with

my $msg = "GET /capacity HTTP/1.0\n"

Note that sending an empty line (thus the extra "\n") is what
tells the server that it has seen all of the request from the
client. If that does the trick you better read the HTTP spec-
ifications (RFC 2616 for HTTP 1.1 IIRC) carefully;-)

                             Regards, Jens
  \   Jens Thoms Toerring  ___

Site Timeline