0

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.

9
  • Try ss instead: ss -OHnltp Oct 24, 2023 at 7:51
  • Or lsof: lsof -nP -i tcp -s tcp:LISTEN -Fcn Oct 24, 2023 at 7:53
  • Nice. Unfortunately, I can't seem to get the process name and its command line with ss
    – LiamF
    Oct 24, 2023 at 7:56
  • Unfortunately, lsof -nP .. (w/o sudo) shows nothing. With sudo, it outputs unusable data all crammed into one column.
    – LiamF
    Oct 24, 2023 at 7:58
  • with -F lsof outputs a parsable output with the first letter the type of data (pid, fd, command, name) followed by the value. Remove that -Fcn for a multicolumn output. Oct 24, 2023 at 8:00

1 Answer 1

1

It seems that Program name column's width is hard coded[0] in netstat to 20 characters. So it is not possible to widen it without modifying the source itself.

Like you, I use ps to get broader program name. Ways to list full program names are discussed here [1].

[0] https://sources.debian.org/src/net-tools/2.10-0.1/netstat.c/#L256

[1] netstat: See process name like in `ps aux`

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .