I have a list of ip addresses to telnet to a specific port and check about a specific application. If present it responds with a a line of output, no login is necessary. The list of IP i have is in this format below,
3.3.3.*
3.3.9.*
23.23.1.49
My code as below,
#! /usr/bin/perl -w
# run telnet to port xx29
# read data
use strict;
use diagnostics;
use Net::Telnet;
my $file = 'iplist1.txt' ;
#my $command = `/bin/ping` ;
open (OUT,'>', "telnet.log");
open (IPS, '<', $file)
or die('unable to open the file', $file );
while(<IPS>) {
#ping($_) ;
#print "$_ " ;
my $telnet = Net::Telnet->new(Host=>"$_", Port=>'xx29', timeout=>4), e
+rrmode=>
(sub {
open(OUT, ">>telnet.log");
print "Bad connection - Unable to connect to IP $_ at \r\n" ;
print "-------------------\r\n";
next;}));
}
close OUT ;
close IPS ;
I am getting error on this code as below
Use of uninitialized value $_ in concatenation (.) or string at run_te
+lnet.pl
line 22, <IPS> line 1 (#1)
(W uninitialized) An undefined value was used as if it were alread
+y
defined. It was interpreted as a "" or a 0, but maybe it was a mi
+stake.
To suppress this warning assign a defined value to your variables.
To help you figure out what was undefined, perl will try to tell y
+ou the
name of the variable (if any) that was undefined. In some cases it
+ cannot
do this, so it also tells you what operation you used the undefine
+d value
in. Note, however, that perl optimizes your program and the opera
+tion
displayed in the warning may not necessarily appear literally in y
+our
program. For example, "that $foo" is usually optimized into "that
+ "
. $foo, and the warning will refer to the concatenation (.) operat
+or,
even though there is no . in your program.
Bad connection - Unable to connect to IP at
-------------------
Exiting subroutine via next at run_telnet.pl line 24, <IPS> line 1 (#2
+)
(W exiting) You are exiting a subroutine by unconventional means,
+such
as a goto, or a loop control statement.
Use of uninitialized value $_ in concatenation (.) or string at run_te
+lnet.pl
line 22, <IPS> line 2 (#1)
Bad connection - Unable to connect to IP at
The logic behind is to connect the IP address or the whole subnet (when its in xx.yy.zz.* ; asterisk in last quadrant) to a specific port, connect next if connection timeout or error, store the output to a file if connection successful, if not successful then move to next connection. I am yet to figure the code to connect complete subnet when asterisk, but the initial form is giving weird variable initialization error which i am not able to trace reason.Still looking at documentation of Net::Telnet. I am not very familiar with Net::Telnet and IP address and subnet iteration from a list, your opinion would be very useful to bring this code to action.