Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^4: Telnet list of IP and get information stored to a file

by sanju7 (Acolyte)
on Jul 29, 2010 at 01:28 UTC ( [id://851834]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Telnet list of IP and get information stored to a file
in thread Telnet list of IP and get information stored to a file

Roboticus:

Thanks for reply, i think i am just overwhelmed of the obstacles to achieve the task. I have some success in the form of basic structure of the script and logic --however few more steps to go. Here is the code doing the job.

Success: The code iterates through a text file (list of ip addresses) and telnets to the specific port and logs the error

Issues: (a) The code for some reason doesn't logs the message when its success. When tested with a single IP its logging successfully though. (b) I am not getting how to expand an ip address which is a network (eg:10.3.3.0/24). This would make the code successfully iterate through most of the org servers if not all (assuming some of them have different network setup, firewall etc)

The below code is the basic structure working fine but not picking the success when run on a list

use strict; use Net::Telnet; # definitions my $testfilename = "iplist1.txt"; my $dumplog = "dumplog.txt"; my $outputlog = "outputlog.txt"; my $optionlog = "optionlog.txt"; my $inputlog ="inputlog.txt"; our $recordlog ="recordlog.txt"; my $string0 = 'Connected' ; my $string1 = "\015\012" ; my $string2 = '\\CR \\LF' ; # #open (OUT,'>>', "$recordlog"); # iterating file open (IPS, '<', $testfilename) or die('unable to open the file', $testfilename ); while (my $ip = <IPS>) { chomp $ip; my $telnett = Net::Telnet->new(Host => "$ip" +, Port => 'xxxx', Dump_log => "$dumplog", input_log => "$inputlog", o +ption_log => "$optionlog", output_log => "$outputlog", timeout => 10, + errmode => (sub { open(OUT, '>>', "$recordlog"); print OUT "Bad connection - Unable to connec +t to IP $ip at \r\n" ; print OUT "-------------------\r\n"; next;})); $telnett->open("$ip") or die "hai $telnett->errmsg "; # add Errmode + and output handle print "connected \n"; $telnett->waitfor('//'); print "carriage return: sending \\CR \\LF \n"; print "carriage return: waiting ...\n"; my $output = $telnett->put(String => $string2, Errmode => 'die', Ti +meout => '4',); print "The server returned: $output \n"; # Error handling # my $etc0 # expanding IP Networks # my $etc1 # Logging everything # my $etc2 }

The file iplist1.txt as below

10.xx.yy.zzz 127.0.0.1 127.0.0.1 10.aa.bb.cc 10.aaa.bb.cc

This populates recordlog.txt only as below

ad connection - Unable to connect to IP 10.xx.yy.zz at ------------------- Bad connection - Unable to connect to IP 10.xx.yy.zz at ------------------- Bad connection - Unable to connect to IP 10.xx.yy.zz at -------------------

When i tested with single ip the code seems to populate dumplog file giving me some idea as to what return values i am dealing with even though I am still looking why it doesn't log to the outputlog or inputlog or optionslog. The following test code i used to populate the dumplog

# simple telnet_test use strict; use Net::Telnet; ### #my $testfilename = "telnet-tests.txt"; my $testfilename = "iplist1.txt"; my $dumplog = "dumplog.txt"; my $outputlog = "outputlog.txt"; my $inputlog="inputlog.txt"; my $logfilename = "./log/telnetlog${t}.txt"; our $testcount = 0; our $debug = 1; ### # instantiate a new telnet object my $telnet = Net::Telnet->new(Port => 'xxxx', Timeout => 10, Telnetmod +e => '0', Errmode => 'die', Prompt => '//', Dump_log => "$dumplog", output_log = +> "$outputlog", input_log => "$inputlog" ); my $string = "\015\012" ; #my $string = '\CR \LF' ; $telnet->open("10.aaa.bb.cc") or die "hai $telnet->errmsg "; print "connected \n"; $telnet->waitfor('//'); print "about to execute carriage return \n"; print "sending \\cr waiting ...\n"; my $output = $telnet->put(String => $string, Errmode => 'die', Timeout + => '4',); print " $output \n";

This is the screendump as below

root@xxxxx scripts]# perl test_telnet1.pl connected about to execute carriage return sending \cr waiting ... 1

This is how the dumpfile looks as below

root@xxxxx scripts]# cat dumplog.txt > 0x00000: 0d 0d 0a ...

Using telnet and interacting with it even with its very basic form needs understanding of how the application is responding to the telnet request etc. The application i am querying is kind of like SMTP service. It doesn't has a prompt or so ..thats how i deviced my code on both the scripts above matching with ('//') and trying to "put" a carriage return to the main "$telnet" object. If you have any suggestion here please let me know. I am dumping the app behavior for your understanding as below

[root@xxxxx scripts]# telnet 10.aaa.bb.cc xxxx Trying 10.aaa.bb.cc... Connected to nodecc.dom.org.local (10.aaa.bb.cc). Escape character is '^]'. dds_pc: _ms=nodecc.dom.org.localþ_si=Process controllerþ_mid=9016þ_sev +=0þ_dt=2010/07/29þ_tm=01:12:02þ_pkg=þ Connection closed by foreign host.

Usually on success within 30 second the telnet connection disconnects dumping the nodename and that is what i need. From the many servers around the spread out internal network this seems easiest to spot the server with that particular app, app only responds to telnet and won't otherwise (tried nmap not sure if i did correct enough switch etc though). Your expert suggestion to complete script /resolve both the issue will help me a lot.

Replies are listed 'Best First'.
Re^5: Telnet list of IP and get information stored to a file
by roboticus (Chancellor) on Jul 29, 2010 at 06:16 UTC

    sanju7:

    (a) The code for some reason doesn't logs the message when its success. When tested with a single IP its logging successfully though.

    You're not being very specific on *which* log isn't updated. Looking at your code, I can think of several possibilities:

    • You may be referring to the print "connected" message, in which case you're not telling it to go to a log file, like you are for the failures.
    • You may be referring to the log/dump files specified in the Net::Telnet modules new function. When I look at the documentation, it appears to me that you're handing it the name of a file instead of a file handle. (I've not used the module, so I may be misreading the documentation.
    • Also, since you're creating the telnet objects in a loop, each telnet object overwrites the specified log files with a new version. So you'd only get the logs for the last address (unless the files were kept open, in which case you'd only get the first addresses logs.)
    (b) I am not getting how to expand an ip address which is a network (eg:10.3.3.0/24).

    There are lots of ways to do it. For the example you give, 10.3.3.0/24, the first 24 bits of the address should remain a constant: 10.3.3, and you should vary the last eight bits through the values 1..255. So first you need to detect that your address needs to be expanded, then figure out which part(s) of the address need to vary, and then generate the appropriate sequence. So the code that does the work on an IP address should be in a subroutine. Then you can call it in a loop for each address in the expansion.

    I get the feeling that you're trying to bite off too much at a time. You might consider breaking your project into distinct chunks, and solve each chunk individually.

    ...roboticus

      Roboticus

      You may be referring to the print "connected" message, in which case you're not telling it to go to a log file, like you are for the failures.

      Indeed i am referring that, more preciously I need to record this as below like a success message, because telnet gives this why the script cann't get this ?

      dds_pc: _ms=nodecc.dom.org.localþ_si=Process controllerþ_mid=9016þ_sev +=0þ_dt=2010/07/29þ_tm=01:12:02þ_pkg=þ

      but instead the only reply i am getting is ("The server returned: 1") ,

      The server returned: 1

      The server returned: 1

      The server returned: 1

      Bad connection - Unable to connect to IP 3.xx.yy.zz at -------------------

      Bad connection - Unable to connect to IP 3.xxx.yyy.zz at -------------------

      Bad connection - Unable to connect to IP 3.xxx.yyy.y at --------------------

        but instead the only reply i am getting is ("The server returned: 1")

        That's because you're using the $telnet->put method which returns 1 on success. I think you want my @output = $telnet->cmd which will give you the output from the server.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://851834]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-24 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found