Skip to content

Instantly share code, notes, and snippets.

@hexiyou
Forked from mshock/host_ports.pl
Created December 8, 2022 13:41
Show Gist options
  • Save hexiyou/75fd60275bf160c069cb380292ca2763 to your computer and use it in GitHub Desktop.
Save hexiyou/75fd60275bf160c069cb380292ca2763 to your computer and use it in GitHub Desktop.
Perl test host port connectivity
#! perl -w
# test ports 80 and 1433 on given IP
# HTTP and SQL
# for new NAT addresses
use strict;
use IO::Socket::PortState qw(check_ports);
my $timeout = 10;
my %ports = (
tcp => {
80 => {},
1433 => {},
}
);
open(HOSTS, '<', $ARGV[0] || '/etc/hosts');
my @ips = <HOSTS>;
close HOSTS;
for my $ip (@ips) {
chomp $ip;
my $host_hr = check_ports($ip, $timeout, \%ports);
for my $port (sort {$a <=> $b} keys %{$host_hr->{tcp}}) {
my $stat = $host_hr->{tcp}{$port}{open} ? 'success' : 'failure';
print "$ip - $port - $stat\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment