Tcpping.pl

From Federal Burro of Information
Revision as of 14:18, 10 July 2020 by David (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
#!/usr/bin/perl -w

use strict;
use Net::Ping;

if (defined($ARGV[1])){
} else {
        print "Error: wrong number of arguments.\n";
        print "Usage: $0 host port\n";
        exit 1;
}

if ( $ARGV[1] =~ /^\d+$/ ){
} else {
        print "Error: bad port\n";
        print "Usage: $0 host port\n";
        exit 1;
}

if (defined($ARGV[0])){
        my $p = Net::Ping->new("tcp", 2);
        # $p->{port_num} = getservbyname($ARGV[1], "tcp");
        # print "Protocol is ".getservbyport($ARGV[1], "tcp" )."\n";
        $p->{port_num} = $ARGV[1];
        # older versions of Net::Ping dont do this
        # FIXME detect version of Net::Ping module UNIVERSAL -> can (method )
        # $p->service_check( 1 );
        exit 1 unless $p->ping($ARGV[0])
} else {
        print "Error: wrong number of arguments.\n";
        print "Usage: $0 host port\n";
        exit 1;
}