now installs the script, MANIFEST updated, and script finished and documented

This commit is contained in:
Zane C. B-H 2019-02-26 04:58:06 -06:00
parent c476f7c6f7
commit c94ad23412
4 changed files with 151 additions and 8 deletions

View File

@ -7,3 +7,4 @@ t/00-load.t
t/manifest.t
t/pod-coverage.t
t/pod.t
bin/cnetstat

View File

@ -11,6 +11,7 @@ WriteMakefile(
LICENSE => 'artistic_2',
PL_FILES => {},
MIN_PERL_VERSION => '5.006',
INST_SCRIPT => 'bin',
CONFIGURE_REQUIRES => {
'ExtUtils::MakeMaker' => '0',
},
@ -19,7 +20,12 @@ WriteMakefile(
},
PREREQ_PM => {
'Parse::Netstat'=>'0.14',
'Parse::Netstat::Search'=>'0.0.0',
'Error::Helper'=>'1.0.0',
'Parse::Netstat::Search'=>'0.1.1',
'Parse::Netstat::Search::Sort'=>'0.0.0',
'Term::ANSIColor'=>'0.0.0',
'Text::Table'=>'0.0.0',
'Getopt::Long'=>'0.0.0',
},
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
clean => { FILES => 'Parse-Netstat-Colorizer-*' },

View File

@ -2,13 +2,13 @@
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
use Getopt::Long;
use Parse::Netstat qw(parse_netstat);
use Parse::Netstat::Colorizer;
sub version{
print "cnetstat v. 0.0.0";
print "cnetstat v. 0.0.0\n";
}
sub help{
@ -17,9 +17,16 @@ sub help{
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.
-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.
';
}
@ -33,6 +40,11 @@ my $sort='none';
my $cidr_string;
my $ports_string;
my $states_string;
my $protocols_string;
my $all=0;
my $listening;
Getopt::Long::Configure ('no_ignore_case');
Getopt::Long::Configure ('bundling');
GetOptions(
't'=>\$tcp,
'u'=>\$udp,
@ -40,11 +52,14 @@ GetOptions(
'v' => \$version,
'help' => \$help,
'h' => \$help,
'P' => \$dont_resolve_ports,
'a' => \$all,
'l' => \$listening,
'drp' => \$dont_resolve_ports,
'c=s' => \$cidr_string,
'S=s' => \$sort,
'p=s' => \$ports_string,
's=s' => \$states_string,
'P=s' => \$protocols_string,
);
# print version or help if requested
@ -87,7 +102,30 @@ if (defined( $ports_string ) ){
}
}
# process the requested protocols if needed
if ( defined( $protocols_string ) ){
my @protocols=split(/\,/, $protocols_string);
$search->set_protocols( \@protocols );
if ( $search->error ){
warn( 'Failed to the requested protocols' );
exit 1;
}
}
#process the requested ports if needed
if (
(!defined( $states_string)) &&
$listening
){
# set states to listen if none are given
$states_string='listen';
$all=1;
}elsif( defined( $states_string ) &&
$listening
){
$states_string='listen,'.$states_string;
$all=1;
}
if (defined( $states_string ) ){
my @states=split(/,/, $states_string);
$search->set_states( \@states );
@ -97,6 +135,104 @@ if (defined( $states_string ) ){
$tcp=$tcp ^ 1;
$udp=$udp ^ 1;
my $res=parse_netstat(output => join("", `netstat -n`), tcp=>$tcp, udp=>$udp, unix=>0, flavor=>$^O);
# don't resolve the ports if asked not to
if ( $dont_resolve_ports ){
$pnc->set_port_resolve;
}
# put together the command
my $netstat_command='netstat -n';
if ( $all ){
$netstat_command=$netstat_command.' -a';
}
my $res=parse_netstat(output => join("", `$netstat_command`), tcp=>$tcp, udp=>$udp, unix=>0, flavor=>$^O);
print $pnc->colorize($res);
=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>]
=head1 FLAGS
=head2 -a
Show all connections, including those in the LISTEN state.
=head2 -c <CIDRs>
A comma seperated list of CIDRs to search for.
=head2 --drp
Don't resolve port numbers to names.
=head2 -l
Show connections in the LISTEN state. This is the equivalent of '-a -s listen'. If combined
with -s, it will display LISTENing sockets and whatever is specified via -s.
=head2 -p <ports>
A comma seperated list of ports to search for.
=head2 -s <states>
A comma seperated list of states to search for.
=head2 -S <sort>
The sort method to use. This is one of the supported
methods by L<Parse::Netstat::Sort>.
host_f Host, Foreign (default)
host_l Host, Local
port_f Port, Foriegn
port_l Port, Local
state State
protocol Protocol
q_r Queue, Receive
q_s Queue, Send
none No sorting is done.
The ones below are dual sort and take noticably longer than the ones above.
host_ff Host, Foreign First
host_lf Host, Local First
port_ff Port, Foreign First
port_lf Port, Local First
q_rf Queue, Receive First
q_sf Queue, Send First
=head2 -t
Don't fetch TCP info.
=head2 -u
Don't fetch UDP info.
=head1 EXAMPLES
cnestat -s established,time_wait
Return a list of connection that are in the established or time_wait state.
cnestat -c ::/0
Return a list of all IPv6 addresses.
cnestat -c ::1/128,127.0.0.1/32
Return all connections to localhost.
cnestat -c 192.168.15.2/32 -l
Display all connections listening explicitly on 192.168.15.2.
=cut

View File

@ -134,7 +134,7 @@ sub colorize{
color('underline white').'RecvQ'.color('reset'),
color('underline white').'Local Host'.color('reset'),
color('underline white').'Port'.color('reset'),
color('underline white').'Remete Host'.color('reset'),
color('underline white').'Remote Host'.color('reset'),
color('underline white').'Port'.color('reset'),
color('underline white').'State'.color('reset'),
]);