Last active
August 12, 2017 15:58
-
-
Save eddy85br/3469344902a26830fe27 to your computer and use it in GitHub Desktop.
Grep Netstat Header and Results (netstat -tupan | egrep ...)
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/env perl | |
use warnings; | |
use strict; | |
use feature 'say'; | |
die 'Pass some port, PID, string or RegExp (between quotation marks) as command arguments.'."\n" if !scalar(@ARGV); | |
map { $_ =~ s/\"/\\\"/g; } @ARGV; | |
my $grep_options = join(' ', grep(/^-\w+/, @ARGV)); | |
my $user_args = join('|', grep(!/^-\w+/, @ARGV)); | |
my @head = ( | |
'Active Internet connections|servers and established|Conex.es Internet Ativas|servidores e estabelecidas', | |
'Proto|Recv-Q|Send-Q|Local Address|Foreign Address|State|PID|Program name' | |
); | |
my $command = 'netstat -tupan | egrep '; | |
if (scalar $grep_options) { | |
if ($grep_options =~ /-.*c/ ) { | |
$command .= $grep_options.' "('.$user_args.')"'; | |
} | |
else { | |
$command .= $grep_options.' --color=always "('.$head[0].'|'.$head[1].'|'.$user_args.')"'; | |
} | |
} | |
else { | |
$command .= '--color=always "('.$head[0].'|'.$head[1].'|'.$user_args.')"'; | |
} | |
say '## COMMAND: '.$command; | |
my @netstat = `$command`; | |
print join('', @netstat); ## Already has EOL ("\n") from netstat ... ## | |
exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment