Information Technology Grimoire

Version .0.0.1

IT Notes from various projects because I forget, and hopefully they help you too.

check ports

the script

#!/usr/bin/perl -w
use strict;
use IO::Socket::PortState qw(check_ports);
# this is the icmp timeout
my $timeout = 1;
# use the format as per below to add new ports
# perl is not going to be as fast a nmap, this is
# a specialized tool to check for RDP and SSH
# and print it out to a spreadsheet, use nmap!
my $proto = 'tcp';
my %port_hash = (
        $proto => {
            22     => {},
            3389   => {},
            }
        );
# loop over __DATA__ and process line by line
while (<DATA>){
    my $host = $_;
    # strip off the new line character
    chomp($host);
    # get a hash ref (I think that's the data structure returned)
    my $host_hr = check_ports($host,$timeout,\%port_hash);
    # print whatever host this
    print "$host";
    # loop over each key in the hash that matches $proto (tcp), so 22 and 3389
    for my $port (keys %{$host_hr->{$proto}}) {
        # if it's open, say "yes", else say "no"
        my $yesno = $host_hr->{$proto}{$port}{open} ? "yes" : "no";
                # if it's "yes", then print it out
		if ($yesno eq 'yes') {
			print ",$port";
		}
    }
    # add a new line for formatting
    print "\n";
}
# don't include spaces or extra lines below the __DATA__ mark
__DATA__
a.com
b.com
c.com
d.com
10.1.1.1