Solved

ip address and port check in perl

Posted on 2011-03-04
4
650 Views
Last Modified: 2013-12-25
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
Comment
Question by:djroh
  • 3
4 Comments
 
LVL 8

Accepted Solution

by:
point_pleasant earned 500 total points
Comment Utility
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
 
LVL 8

Expert Comment

by:point_pleasant
Comment Utility
sorry usage

perl progname.pl address_file
0
 
LVL 8

Expert Comment

by:point_pleasant
Comment Utility
sorry usage: perl progname.pl < address_file
0
 

Author Comment

by:djroh
Comment Utility
Thank you.. I will give it a try.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now