Skip to content

Instantly share code, notes, and snippets.

@UKNC
Created February 2, 2017 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UKNC/b7cb6cc2378c6878be8279fc161dc89b to your computer and use it in GitHub Desktop.
Save UKNC/b7cb6cc2378c6878be8279fc161dc89b to your computer and use it in GitHub Desktop.
Determines range of ephemeral ports and number of unused ports.
#!/usr/bin/perl
#################################################################################
# Author: NQPC
# Description: determines range of ephemeral ports and number of unused ports
# OS: CentOS
##################################################################################
use strict;
use warnings;
my ($minport, $maxport) = split /\s+/, `cat /proc/sys/net/ipv4/ip_local_port_range`;
print "minport=$minport, maxport=$maxport, number=", ($maxport - $minport + 1). "\n";
my @portrange = ($minport..$maxport);
my @freeflag = (1) x scalar @portrange;
my %free;
@free{@portrange} = @freeflag;
my @out = `netstat -ant`;
my @ports;
for my $line (@out) {
if ( $line !~ /^tcp/ ) { next; }
my (@f) = split /\s+/, $line;
my ($ip, $port) = split ':', $f[3];
$free{$port} = 0;
}
my @freeports;
while ( my ($k, $v) = each %free ) {
if ( $v ) {
push @freeports, $k;
}
}
print "free ports: ", scalar @freeports, "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment