Can you widen the columns in netstat, specifically "PID/Program name" ...?
1
vote
1
answer
737
views
There is an old post about line truncation in
netstat
(Netstat output line width limit ) but my question is a bit different.
I'm using netstat
(net-tools 2.10) on Debian 12. My primary use is to list listening ports, e.g. netstat -tunlpWee
I find the PID/Program name
column to been too narrow. Is there a way to widen this?
Option -T
is unsupported. Option -W
(--wide
) does not help as this only affects IP addresses. Option -e
is about "additional information," not "wider information."
At this point, I see my only option to be to wrap netstat
in a script and leverage ps
to get a broader "program name." Unless ... I'm missing something obvious.
UPDATE:
Thanks, davidt930. That's disappointing. I came up with this solution:
#!/usr/bin/env bash
# show applications using ports
# use sudo to get the process name
# The "PID/Program name" as returned by netstat(8) is too narrow for my tastes.
# Therefore, I wrap netstat's output in a series of calls to ps(1) to get
# broader application details, i.e. the full command line.
PPWID=20
data=
while IFS= read -r ln ; do
[ -z "$data" ] && {
echo "$ln"
[ "${ln/PID\/Program name/}" != "$ln" ] && data=Y || :
continue
} || :
static="${ln:0:-$PPWID}"
program="${ln:0-$PPWID}"
[ "${program:0:1}" = "-" ] && command="(need privileges)" || {
pid=${program%%/*}
command=$(ps -o command -p $pid | tail -1)
}
echo "${static}${command}"
done< <(netstat -tunlpWee)
It's a tad fragile as it relies on netstat
keeping the PID/Program name
column fixed at 20.
Asked by LiamF
(175 rep)
Oct 24, 2023, 06:16 AM
Last activity: Oct 26, 2023, 06:27 AM
Last activity: Oct 26, 2023, 06:27 AM