Skip to content

Instantly share code, notes, and snippets.

@serxoz
Created October 26, 2022 10:18
Show Gist options
  • Save serxoz/70a5d44a1778a6580d8baf25efb0e656 to your computer and use it in GitHub Desktop.
Save serxoz/70a5d44a1778a6580d8baf25efb0e656 to your computer and use it in GitHub Desktop.
OpenBSD sockstat like script (fstat parser)
#!/bin/ksh
# Since OpenBSD does not have sockstat this script parses the output
# of fstat to print a summary indicating which process is listening on which port
fstat | awk '
BEGIN {
OFS="\t";
}
{
if ( NR == 1 ) {
print($1, $2, $3, $5, $7, "ADDR");
}
if( $0 ~ /tcp|udp/ ) {
if(!($0 ~ /[<-]-[->]/)) {
l = $1 "\t" $2 "\t" $3 "\t" $5 "\t" $7 "\t";
if($7 == "tcp") {
services[$3] = l $9;
} else if ($7 == "udp") {
services[$3] = l $8;
}
}
}
}
END {
for(service in services) {
print(services[service]);
}
}
' | column -t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment