16

When executing netstat, I find that the command's output width is limited regardless of the console size, in contrast with other commands such as ps that seem to get adjusted.

So for example:

$ sudo netstat -natp | grep sshd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1454/sshd       
tcp        0     48 xx.xx.xx.xx:22          xx.xx.xx.xx:44182       ESTABLISHED 1147/sshd: wtower [
tcp6       0      0 :::22                   :::*                    LISTEN      1454/sshd       

You can notice that the line width of the second output is short (ends at [). Is there any way so I get the proper output from netstat?

UPDATE: The package version is net-tools_1.60-24.1ubuntu2_i386 running on Ubuntu Server 12.04.5 LTS.

Unfortunately redirecting to file produces the same output.

At any console size the output is the above. At smaller sizes it just wraps each line, but still the output is the same, shortened.

6
  • I can't reproduce this on my debian. What's your netstat version? Does it work as expected if you redirect to a file?
    – terdon
    Jun 25, 2015 at 11:18
  • Thanks @terdon, the package version is net-tools_1.60-24.1ubuntu2_i386. Unfortunately redirect is the same, that would solve my problem. At any console size the output is this, at smaller sizes it just wraps each line.
    – Wtower
    Jun 25, 2015 at 11:22
  • 1
    Please edit your question to add new information, it is easy to miss and harder to read in the comments. Are you saying this works OK on smaller terminals? The entire line is wrapped and printed? Or is it only the smaller ones? Does running COLUMNS=1000 sudo netstat -natp | grep sshd make any difference? ( I doubt it will but it's worth a try). Does running netstat with the -Wflag help?
    – terdon
    Jun 25, 2015 at 11:27
  • 1
    It seems you're right, the problem is netstat which is changing it's output depending on whether the output is going to a terminal or somewhere else. So when it goes to a pipe, it doesn't know the terminal width and so truncates. Solutions are completely command specific. See here for discussion: unix.stackexchange.com/questions/108849/…
    – Diagon
    Mar 20, 2017 at 2:15
  • 2
    what an awful default behaviour for a utility like this! infuriating
    – iono
    Jul 21, 2019 at 10:44

4 Answers 4

3

As explained in this response to a similar question regarding aptitude and dpkg, netstat is truncating the output because when it's piped to grep, it doesn't know how wide the output should be. Solutions to this general problem depend specifically on the options to the program that's being fed into the pipe. In the case of netstat, the only choice I see is to use the --wide option, which tells it to assume the output is as wide as needed for the output. You might then want to use cut -c-100 eg. if you really only want 100 characters per line.

1
  • 2
    The --wide option does not expand the command line lenght (on Debian 10 at least). Running netstat ... | cat - solve the issue for me but not for all the command (netstat -lntp | cat - is still truncated)
    – Xorax
    Dec 20, 2021 at 10:54
2

This is something that may have been fixed by now. I was having the same problem trying see what ports were listening.

I was running netstat -vat. This resulted in truncated columns. I found that just by adding a capitol "T" to the end, it'll give me what I want.

netstat -vatT

v = verbose a = all t = tcp T = notrim (stop trimming long addresses)

1
  • 4
    netstat: invalid option -- 'T' (Ubuntu 18.04 LTS, net-tools 2.10-alpha) Mar 27, 2020 at 14:25
0

on Amazon linux I use -W option. eg: netstat -atgW

netstat --version

net-tools 2.10-alpha
Fred Baumgarten, Alan Cox, Bernd Eckenfels, Phil Blundell, Tuan Hoang, Brian Micek and others
...

man netstat

--wide , -W Do not truncate IP addresses by using output as wide as needed. This is optional for now to not break existing scripts.

0

You can notice that the line width of the second output is short (ends at [). Is there any way so I get the proper output from netstat?

Unfortunately the width of the PID/Program name column in netstat is hardcoded with #define PROGNAME_WIDTH 20 so there is no way of getting the full output from netstat directly. Also you end up with 19 characters and a space at the end.

In today's linux one can use sudo ss -natp to get the full info formatted a bit differently but with the full name:

State          Recv-Q         Send-Q                 Local Address:Port                   Peer Address:Port         Process                                                              
LISTEN         0              4096                   127.0.0.53%lo:53                          0.0.0.0:*             users:(("systemd-resolve",pid=1360261,fd=14))                       
LISTEN         0              128                          0.0.0.0:22                          0.0.0.0:*             users:(("sshd",pid=1360240,fd=3))                                   
LISTEN         0              4096                         0.0.0.0:25                          0.0.0.0:*             users:(("docker-proxy",pid=1388011,fd=4))                           
ESTAB          0              0                          10.0.2.15:22                         10.0.2.2:39156         users:(("sshd",pid=1052681,fd=4),("sshd",pid=1052550,fd=4))         
LISTEN         0              128                             [::]:22                             [::]:*             users:(("sshd",pid=1360240,fd=4))                                   
LISTEN         0              4096                            [::]:3478                           ```

You must log in to answer this question.

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