Solved

ip address and port check in perl

Posted on 2011-03-04
4
674 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
ID: 35039764
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
ID: 35039769
sorry usage

perl progname.pl address_file
0
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35039934
sorry usage: perl progname.pl < address_file
0
 

Author Comment

by:djroh
ID: 35043473
Thank you.. I will give it a try.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

830 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