Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 713
  • Last Modified:

ip address and port check in perl

I need help in scripting out a multiple ip address and port checking in perl.  The script will read from a  text file with the ip address and port associated with it.  If an ip address and port doesn't work it will create a separate logfile and after all the ip address and port has been checked from the text file, this error logfile with the ip address and port that didn't work will be emailed to me.  Any assistance will be great.  Thanks
0
djroh
Asked:
djroh
  • 3
1 Solution
 
point_pleasantCommented:
Give this a try assumes address file is in format of

xxx.xxx.xxx.xxx xx

ip adresss space port

#!/usr/bin/perl

use strict;
use warnings;
use IO::Socket::INET;

my $err_log="/tmp/port_errlog";
open(ERR_LOG, ">$err_log");

while (<>) {
        my ($host, $port) = split / /,$_;
        #Attempt to connect to $host on $port.
        my $socket;
        my $success = eval {
                $socket = IO::Socket::INET->new(
                PeerAddr => $host,
                PeerPort => $port,
                Proto => 'tcp' )
        };
        #If the port was opened, say it was and close it.
        if ($success) {
                shutdown($socket, 2);
        } else {
                chomp ($port);
                print (ERR_LOG "$host $port Failed\n");
        }
}
system ("mail -s "PORT FAILURE ALERT!" your.email@domain.com < /tmp/port_errlog");
~
0
 
point_pleasantCommented:
sorry usage

perl progname.pl address_file
0
 
point_pleasantCommented:
sorry usage: perl progname.pl < address_file
0
 
djrohAuthor Commented:
Thank you.. I will give it a try.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now