http://www.perlmonks.org?node_id=202756
Category: Networking
Author/Contact Info
Description: Determines which TCP ports and services a host may be using by opening a TCP socket connection. Used for determining what services are running on a specific host. I used the RFC port number list to pull service names so you may have to download this list and modify unused lines with a (#) comment to match the regex. I am a Perl N00b so please be easy on me. Format for the rfc list looks like this:

# Keyword         Decimal    Description                     References
#                                   
#                          Jon Postel <postel@isi.edu>
tcpmux            1/tcp    TCP Port Service Multiplexer
tcpmux            1/udp    TCP Port Service Multiplexer
#                          Mark Lottor <MKL@nisc.sri.com>
compressnet       2/tcp    Management Utility
compressnet       2/udp    Management Utility
compressnet       3/tcp    Compression Process
compressnet       3/udp    Compression Process
Feel free to contact me if you'd like a copy.
#!/usr/bin/perl
use IO::Socket;

# ********************************************************************
# pscanner : portscan hosts using a tcp connection through
# IO::Socket module and prepackaged port-numbers RFC list 
#  
# Fri Oct  4 10:50:44 EDT 2002
# ********************************************************************


print "Enter the name of the server you would like to scan\n";
chomp( my $server = <STDIN> );
print "What port would you like to start at\? \(1-65000\)\n";
chomp( my $start = <STDIN> );

## Store portlist in @portlist
open( PORTLIST, "./port-numbers" ) or die "Unable to open portlist: $!
+";
my @portlist = <PORTLIST>;
close(PORTLIST);
chomp(@portlist);

foreach (@portlist) {
    next if $_ =~ /^\#/;
    @whatever = ( split ( /\s+/, ( split (/\//) )[0] ) );
    ( $service, $portnumber ) = ( @whatever[0], @whatever[1] );
    $servicelist{"$portnumber"} = "$service";
}

for ( $portnumber = $start ; $portnumber <= 65000 ; $portnumber++ ) {
    $sock = IO::Socket::INET->new(
        PeerAddr => $server,
        PeerPort => $portnumber,
        Proto    => 'tcp'
    );

    if ($sock) {
        print "Connected on port $portnumber $servicelist{$portnumber}
+\n";
    }
    else {

        #  print "$port failed\n";
    }
}    #  End for


=head1 NAME

pscanner - Scans host on TCP ports and pulls service type from RFC por
+t-numbers list

=head1 DESCRIPTION

Determines which TCP ports and services a host may be using by opening
a TCP socket connection.  Used for determining what services are runni
+ng on a 
specific host.

=head1 PREREQUISITE

IO::Socket module
Pre-packaged RFC port numbers list

=head1 COREQUISITE

None

=head1 README

Determines which TCP ports and services a host may be using by opening
a TCP socket connection.  Used for determining what services are runni
+ng on a 
specific host.

=pod OSNAMES

Unix, Linux, Win32


=pod SCRIPT CATEGORIES

Networking

=cut