script finished and document code repo in the module

This commit is contained in:
Zane C. B-H 2019-02-28 04:02:21 -06:00
parent c94ad23412
commit 00640e9d65
2 changed files with 80 additions and 15 deletions

View File

@ -15,22 +15,24 @@ sub help{
&version;
print '
-a All connections, including LISTENing ones.
-c <cidrs> A comma seperated list of CIDRs to search for.
--drp Don\t resolve port names.
-h Print help info.
-i Invert the sort.
-l Equivalent of "-a -s listen". Can be combined with -s.
-p <poorts> A comma seperated list of ports to search for.
-P <protocols> A comma seperated list of protocols to search for.
-s <states> A comma seperated list of states to search for.
-S <sort> The sort method to use.
-t Don\'t fetch TCP connection information.
-u Don\'t fetch UDP connection information.
-a All connections, including LISTENing ones.
-l Equivalent of "-a -s listen". Can be combined with -s.
--drp Don\t resolve port names.
-S <sort> The sort method to use.
-c <cidrs> A comma seperated list of CIDRs to search for.
-p <poorts> A comma seperated list of ports to search for.
-s <states> A comma seperated list of states to search for.
-p <protocols> A comma seperated list of protocols to search for.
-v Print version info.
-h Print help info.
';
}
# command line option holders
my $tcp=0;
my $udp=0;
my $help=0;
@ -43,6 +45,14 @@ my $states_string;
my $protocols_string;
my $all=0;
my $listening;
my $invert=0;
#set the default sort via ENV if requested
if ( defined( $ENV{CNETSTAT_sort} ) ){
$sort=$ENV{CNETSTAT_sort};
}
# get the commandline options
Getopt::Long::Configure ('no_ignore_case');
Getopt::Long::Configure ('bundling');
GetOptions(
@ -54,6 +64,7 @@ GetOptions(
'h' => \$help,
'a' => \$all,
'l' => \$listening,
'i' => \$invert,
'drp' => \$dont_resolve_ports,
'c=s' => \$cidr_string,
'S=s' => \$sort,
@ -72,13 +83,18 @@ if ( $version ){
exit 42;
}
# XOR the invert value if needed
if ( defined( $ENV{CNETSTAT_invert} ) ){
$invert= $invert ^ $ENV{CNETSTAT_invert};
}
#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;
exit 255;
}
#process the CIDR list if requested
@ -88,7 +104,7 @@ if (defined ( $cidr_string ) ){
$search->set_cidrs( \@cidrs );
if ( $search->error ){
warn( 'One of your CIDRs is invalid' );
exit 1;
exit 255;
}
}
@ -98,7 +114,7 @@ if (defined( $ports_string ) ){
$search->set_ports( \@ports );
if ( $search->error ){
warn( 'One of your ports is invalid' );
exit 1;
exit 255;
}
}
@ -108,10 +124,15 @@ if ( defined( $protocols_string ) ){
$search->set_protocols( \@protocols );
if ( $search->error ){
warn( 'Failed to the requested protocols' );
exit 1;
exit 255;
}
}
# invert if requested
if ( defined($invert) ){
$pnc->set_invert($invert);
}
#process the requested ports if needed
if (
(!defined( $states_string)) &&
@ -150,13 +171,20 @@ my $res=parse_netstat(output => join("", `$netstat_command`), tcp=>$tcp, udp=>$u
print $pnc->colorize($res);
# figure out the exit code to use
if ( $pnc->error ){
exit $pnc->error;
}else{
exit 0;
}
=head1 NAME
cnetstat - a netstat like utility that supports color and searching
=head1 SYNOPSIS
cnetstat [B<-t>] [B<-u>] [B<--drp>] [B<-S> <sort>] [B<-s> <states>] [B<-c> <CIDRs>] [B<-p> <ports>] [B<-P> <protocols>] [<-a>] [B<-l>]
cnetstat [B<-t>] [B<-u>] [B<--drp>] [B<-S> <sort>] [B<-s> <states>] [B<-c> <CIDRs>] [B<-p> <ports>] [B<-P> <protocols>] [<-a>] [B<-l>] [B<-i>]
=head1 FLAGS
@ -172,6 +200,10 @@ A comma seperated list of CIDRs to search for.
Don't resolve port numbers to names.
=head2 -i
Invert the sort.
=head2 -l
Show connections in the LISTEN state. This is the equivalent of '-a -s listen'. If combined
@ -181,6 +213,10 @@ with -s, it will display LISTENing sockets and whatever is specified via -s.
A comma seperated list of ports to search for.
=head2 -P <protocols>
A comma seperated list of protocols to saerch for.
=head2 -s <states>
A comma seperated list of states to search for.
@ -217,6 +253,22 @@ Don't fetch TCP info.
Don't fetch UDP info.
=head1 ENVIRONMENT VARIABLES
=head2 CNETSTAT_invert
This is either 0 or 1. If defined it will be used for XORing the -i flag.
export CNETSTAT_invert=1
# run cnetstat inverted
cnetstat
# run it non-inverted, the opposite of what the -i flag normally is
cnetstat -i
=head2 CNETSTAT_sort
Sets the default sort method. -S overrides this.
=head1 EXAMPLES
cnestat -s established,time_wait
@ -235,4 +287,8 @@ Return all connections to localhost.
Display all connections listening explicitly on 192.168.15.2.
cnetstat -S host_f -i
Sort the connections by the foreign host and invert the results.
=cut

View File

@ -46,7 +46,9 @@ our $VERSION = '0.0.0';
my $sorter=$pnc->get_sort;
$sorter->set_sort( 'host_l' );
Sorting and searching is handled via L<Parse::Netsat::Search> and
L<Parse::Netstat::Search::Sort>. Their objects for tweaking can be
fetched via get_sort and get_search.
=head1 METHODS
@ -88,6 +90,9 @@ One value is taken and that is the array ref returned
by Parse::Netstat.
my $colorized=$pnc->colorize($res);
if ( $pnc->error ){
warn( 'Either $res is not valid post a basic check or sorting failed.
}
=cut
@ -367,6 +372,10 @@ L<https://cpanratings.perl.org/d/Parse-Netstat-Colorizer>
L<https://metacpan.org/release/Parse-Netstat-Colorizer>
=item * Code Repo
L<https://gitea.eesdp.org/vvelox/Parse-Netstat-Colorizer>
=back