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