Created
June 14, 2018 15:04
-
-
Save jordemort/37a6e5d36b343f0714ecc346c7624363 to your computer and use it in GitHub Desktop.
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 | |
our %state = (); | |
for my $line (split /\n/, `netstat -lntup`) | |
{ | |
$line =~ s/\s+$//; | |
my $proto, $revq, $sendq, $local, $foreign, $state, $proc; | |
if ($line =~ m/^tcp/) | |
{ | |
($proto, $revq, $sendq, $local, $foreign, $state, $proc) = split /\s+/, $line; | |
} | |
elsif ($line =~ m/^udp/) | |
{ | |
($proto, $revq, $sendq, $local, $foreign, $proc) = split /\s+/, $line; | |
} | |
else | |
{ | |
next; | |
} | |
my @addr = split ':', $local; | |
my $port = pop @addr; | |
my $addr = join ':', @addr; | |
my @proc = split '/', $proc; | |
my $pid = shift @proc; | |
my $service = `systemctl status $pid | head -n1 | cut -d' ' -f2`; | |
$service =~ s/\s+$//; | |
$state{$proto}{$addr}{$port} = $service; | |
} | |
for my $proto ('tcp', 'udp') | |
{ | |
for my $port (sort { $a+0 <=> $b+0 } keys %{$state{$proto}{'127.0.0.1'}}) | |
{ | |
unless (exists $state{$proto . '6'}{'::1'}{$port}) | |
{ | |
print "$proto $port: listening on v4 localhost but not v6 -- ", $state{$proto}{'127.0.0.1'}{$port}, "\n"; | |
} | |
} | |
for my $port (sort { $a+0 <=> $b+0 } keys %{$state{$proto}{'0.0.0.0'}}) | |
{ | |
unless (exists $state{$proto . '6'}{'::'}{$port}) | |
{ | |
print "$proto $port: listening on v4 global but not v6 -- ", $state{$proto}{'0.0.0.0'}{$port}, "\n"; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment