Add (and document) options for showing only listening or connected sockets.
This commit is contained in:
parent
13d609b82e
commit
a0581bbcc2
@ -35,7 +35,7 @@
|
||||
.Nd list open sockets
|
||||
.Sh SYNOPSIS
|
||||
.Nm
|
||||
.Op Fl 46u
|
||||
.Op Fl 46clu
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Nm
|
||||
@ -47,13 +47,30 @@ The following options are available:
|
||||
Show AF_INET (IPv4) sockets.
|
||||
.It Fl 6
|
||||
Show AF_INET6 (IPv6) sockets.
|
||||
.It Fl c
|
||||
Show connected sockets.
|
||||
.It Fl l
|
||||
Show listening sockets.
|
||||
.It Fl u
|
||||
Show AF_LOCAL (Unix) sockets.
|
||||
.El
|
||||
.Pp
|
||||
If no option is specified,
|
||||
If neither
|
||||
.Fl 4 ,
|
||||
.Fl 6
|
||||
or
|
||||
.Fl u
|
||||
is specified,
|
||||
.Nm
|
||||
will list sockets of all three types.
|
||||
will list sockets in all three domains.
|
||||
.Pp
|
||||
If neither
|
||||
.Fl c
|
||||
or
|
||||
.Fl l
|
||||
is specified,
|
||||
.Nm
|
||||
will list both listening and connected sockets.
|
||||
.Pp
|
||||
The information listed for each
|
||||
socket is:
|
||||
@ -71,7 +88,7 @@ The transport protocol associated with the socket for Internet
|
||||
sockets, or the type of socket (stream or datagram) for Unix sockets.
|
||||
.It Li ADDRESS
|
||||
(Unix sockets only)
|
||||
For listening sockets, this is the filename of the socket.
|
||||
For bound sockets, this is the filename of the socket.
|
||||
For other sockets, it is the name, PID and file descriptor number of
|
||||
the peer, or "(none)" if the socket is neither bound nor connected.
|
||||
.It Li LOCAL ADDRESS
|
||||
|
@ -95,8 +95,10 @@ sub addr($) {
|
||||
#
|
||||
# Print information about Internet sockets
|
||||
#
|
||||
sub print_inet($) {
|
||||
sub print_inet($$$) {
|
||||
my $af = shift; # Address family
|
||||
my $conn = shift || 0; # Show connected sockets
|
||||
my $listen = shift || 0; # Show listen sockets
|
||||
|
||||
my $fsd; # Fstat data
|
||||
my $nsd; # Netstat data
|
||||
@ -106,6 +108,8 @@ sub print_inet($) {
|
||||
foreach $fsd (@{$fstat{$af}}) {
|
||||
next unless defined($fsd->[7]);
|
||||
$nsd = $netstat{$fsd->[7]} || $unknown;
|
||||
next if (!$conn && $nsd->[5] ne '*.*');
|
||||
next if (!$listen && $nsd->[5] eq '*.*');
|
||||
printf($inet_fmt, $fsd->[0], $fsd->[1], $fsd->[2],
|
||||
substr($fsd->[3], 0, -1),
|
||||
$nsd->[1], addr($nsd->[4]), addr($nsd->[5]));
|
||||
@ -116,7 +120,9 @@ sub print_inet($) {
|
||||
#
|
||||
# Print information about Unix domain sockets
|
||||
#
|
||||
sub print_unix() {
|
||||
sub print_unix($$) {
|
||||
my $conn = shift || 0; # Show connected sockets
|
||||
my $listen = shift || 0; # Show listen sockets
|
||||
|
||||
my %endpoint; # Mad PCB to process/fd
|
||||
my $fsd; # Fstat data
|
||||
@ -129,6 +135,8 @@ sub print_unix() {
|
||||
printf($unix_fmt, "USER", "COMMAND", "PID", "FD", "PROTO", "ADDRESS");
|
||||
foreach $fsd (@{$fstat{"local"}}) {
|
||||
next unless defined($fsd->[6]);
|
||||
next if (!$conn && defined($fsd->[8]));
|
||||
next if (!$listen && !defined($fsd->[8]));
|
||||
$nsd = $netstat{$fsd->[6]} || $unknown;
|
||||
printf($unix_fmt, $fsd->[0], $fsd->[1], $fsd->[2],
|
||||
substr($fsd->[3], 0, -1), $fsd->[5],
|
||||
@ -141,14 +149,14 @@ sub print_unix() {
|
||||
# Print usage message and exit
|
||||
#
|
||||
sub usage() {
|
||||
print(STDERR "Usage: sockstat [-46u]\n");
|
||||
print(STDERR "Usage: sockstat [-46clu]\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
MAIN:{
|
||||
my %opts; # Command-line options
|
||||
|
||||
getopts("46u", \%opts)
|
||||
getopts("46clu", \%opts)
|
||||
or usage();
|
||||
|
||||
gather();
|
||||
@ -156,14 +164,17 @@ MAIN:{
|
||||
if (!$opts{'4'} && !$opts{'6'} && !$opts{'u'}) {
|
||||
$opts{'4'} = $opts{'6'} = $opts{'u'} = 1;
|
||||
}
|
||||
if (!$opts{'c'} && !$opts{'l'}) {
|
||||
$opts{'c'} = $opts{'l'} = 1;
|
||||
}
|
||||
if ($opts{'4'}) {
|
||||
print_inet("internet");
|
||||
print_inet("internet", $opts{'c'}, $opts{'l'});
|
||||
}
|
||||
if ($opts{'6'}) {
|
||||
print_inet("internet6");
|
||||
print_inet("internet6", $opts{'c'}, $opts{'l'});
|
||||
}
|
||||
if ($opts{'u'}) {
|
||||
print_unix();
|
||||
print_unix($opts{'c'}, $opts{'l'});
|
||||
}
|
||||
|
||||
exit(0);
|
||||
|
Loading…
Reference in New Issue
Block a user