14

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.

Wtower
  • 335
  • 1
  • 3
  • 13
  • 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 '15 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 '15 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 `-W`flag help? – terdon Jun 25 '15 at 11:27
  • Updated question. Unfortunately neither helps. – Wtower Jun 25 '15 at 11:35
  • 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: http://unix.stackexchange.com/questions/108849/why-does-grep-change-the-length-of-output-lines – Diagon Mar 20 '17 at 02:15
  • 1
    what an awful default behaviour for a utility like this! infuriating – iono Jul 21 '19 at 10:44

3 Answers3

2

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.

Diagon
  • 600
  • 4
  • 13
  • 1
    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 '21 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)

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.

Mike Graf
  • 109
  • 3