CIDR search implemnted as well as -u and -t

This commit is contained in:
Zane C. B-H 2019-02-25 06:31:02 -06:00
parent f69ad91436
commit a1a2ce09ea
1 changed files with 77 additions and 0 deletions

View File

@ -1 +1,78 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
use Parse::Netstat qw(parse_netstat);
use Parse::Netstat::Colorizer;
sub version{
print "cnetstat v. 0.0.0";
}
sub help{
&version;
print '
-t Don\'t fetch TCP connection information.
-u Don\'t fetch UDP connection information.
-P Don\t resolve port names.
-s <sort> The sort method to use.
-c <cidrs> A comma seperated list of CIDRs to search for.
';
}
my $tcp=0;
my $udp=0;
my $help=0;
my $version=0;
my $dont_resolve_ports=0;
my $sort='none';
my $cidr_string;
GetOptions(
't'=>\$tcp,
'u'=>\$udp,
'version' => \$version,
'v' => \$version,
'help' => \$help,
'h' => \$help,
'P' => \$dont_resolve_ports,
'c=s' => \$cidr_string,
's=s' => \$sort,
);
# print version or help if requested
if ( $help ){
&help;
exit 42;
}
if ( $version ){
&version;
exit 42;
}
#init what is required for setting sort, incase we need to error here
my $pnc=Parse::Netstat::Colorizer->new;
my $sorter=$pnc->get_sort;
$sorter->set_sort($sort);
if ( $sorter->error ){
warn( '"$sort" is not a valid sort method' );
exit 1;
}
#process the CIDR list if requested
my $search=$pnc->get_search;
if (defined ( $cidr_string ) ){
my @cidrs=split(/\,/, $cidr_string);
$search->set_cidrs( \@cidrs );
}
# invert the TCP/UDP values for telling Parse::Netstat what we want
$tcp=$tcp ^ 1;
$udp=$udp ^ 1;
my $res=parse_netstat(output => join("", `netstat -n`), tcp=>$tcp, udp=>$udp, unix=>0, flavor=>$^O);
print $pnc->colorize($res);