Created
February 2, 2017 11:39
-
-
Save UKNC/b7cb6cc2378c6878be8279fc161dc89b to your computer and use it in GitHub Desktop.
Determines range of ephemeral ports and number of unused ports.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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