#!/usr/bin/perl use strict; use Net::Telnet (); #Trying to get default values working here -- still working on it my $clearfile; my $ipbase; # = shift || "192.168.1"; my $ipcurrent; # = shift || '1'; my $ipend; # = shift || '255'; my $port; # = shift || '23'; my $username; my $password; my $telnet; #This is the log file generated by any failed connections my $downreport = 'report.log'; print "Do you want to keep the old log file and append the new info? [y/n]\n"; $clearfile=; chomp ($clearfile); if ($clearfile eq "n"){ open(INFO,">$downreport"); print INFO "\n";} #Grabbing all pertinent information -- the defaults are not there yet print "What is the base IP? (Example: 192.168.1)\n"; $ipbase = ; chomp ($ipbase); print "What is the bottom IP? (Default 1)\n"; $ipcurrent = ; chomp ($ipcurrent); print "What is the top IP? (Default 255)\n"; $ipend = ; chomp ($ipend); print "What port would you like to connect to (Default 23)?\n"; $port = ; chomp ($port); print "Username:"; $username = ; chomp ($username); #I would like to find a way to turn off echo or mask this portion print "Password:"; $password = ; chomp ($password); for($ipcurrent .. $ipend){ print "Connecting to $ipbase.$ipcurrent\n"; #This is the TRY part of the try-catch portion, where it tries to connect #If it doesn't it outputs the error without killing the program eval { #Sets parameters for the telnet session $telnet = new Net::Telnet->new( Timeout=>3,Dump_Log=>'dump.log', input_log=>'input.log', output_log=>'output.log', Port=>$port, Prompt => '/[\$%#>]\ $/'); die &Log_Message("Can't open telnet session to the remote host") unless #warn &Log_Message("Can't open telnet session to remote host") unless $telnet; #opens the particular IP that we're at. Not sure if these "die" commands are necessary $telnet->open("$ipbase.$ipcurrent"); #die ("Cannot open $ipbase.$ipcurrent\n"); #This may need some tweaking depending on the log-on format $telnet->login( Name=>"$username", Password=>"$password"); print "Logged on\n"; print $telnet->getline; #once connected it tries to exit cleanly $telnet->print("exit"); }; #The other part was try, this is the CATCH. It catches the errors and logs them if ($@){ print $@; print "$ipbase.$ipcurrent failed.\n"; open(INFO, ">>$downreport"); print INFO "Failure at \t $ipbase.$ipcurrent \t Please investigate.\n"; }; print "Trying the next IP\n"; #Stepping up to the next IP $ipcurrent++; } exit 0;