Skip to content

Instantly share code, notes, and snippets.

@eddy85br
Last active August 12, 2017 15:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddy85br/3469344902a26830fe27 to your computer and use it in GitHub Desktop.
Save eddy85br/3469344902a26830fe27 to your computer and use it in GitHub Desktop.
Grep Netstat Header and Results (netstat -tupan | egrep ...)
#!/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