Skip to content

Instantly share code, notes, and snippets.

@pepoluan
Created January 15, 2026 12:32
Show Gist options
  • Select an option

  • Save pepoluan/0a51bf1a650dbd743a4acd4ea2f1c0de to your computer and use it in GitHub Desktop.

Select an option

Save pepoluan/0a51bf1a650dbd743a4acd4ea2f1c0de to your computer and use it in GitHub Desktop.
Show Listener Processes with Detailed Arguments
#!/usr/bin/env bash
# © 2026, Pandu POLUAN
# SPDX-License-Identifier: MPL-2.0
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
function show_listeners() {
netstat -tulpnW |
awk '
# Print the header. Note the "@@" in front of "CmdLine" is needed!
BEGIN {print "Proto", "Local", "Pid", "@@CmdLine"}
# tcp protocol has an additional column that we need to suppress
$1 ~ /^tcp/ {$6=""; $0=$0}
# Generate output only for tcp and udp protocols (including their v6 counterparts)
$1 ~ /^(tc|ud)p/ {
$2=$3=$5="";
$0=$0;
$0=gensub(/( [0-9]+)\/[^/]+$/, "\\1", "g");
print $0, "@@" $NF
}' |
column -t |
(
while read ln; do
pid=${ln##*@@}
if [[ $pid = CmdLine ]]; then
cmd=($pid)
else
# /proc/PID/cmdline is null-separated
readarray -d $'\0' cmd < /proc/${pid}/cmdline
fi
echo "${ln/@@*/${cmd[*]}}";
done
)
}
show_listeners
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment