Netstat: See process name like in `ps aux`

netstatps

The postfix daemon has only the name "master" if I use netsat like this:

root@myhost# netstat -tulpen| grep master
tcp  0  0 127.0.0.1:25  0.0.0.0:*  LISTEN  0  53191445 13640/master        

If I use ps I get a more verbose name:

root@myhost# ps aux| grep 13640
root     13640  0.0  0.0  25036  1500 11:35   0:00 /usr/lib/postfix/master

Is there a way to tell netstat to output the long name?

In this case it would be /usr/lib/postfix/master.

Update

It seems that netstat can't do it. If you know how to do this with an other tool, then this is valid question, too. (But netstat based solutions are still prefered).

Update2

All answers work. Thank you very much for showing your unix knowledge. But up to now the answers are far too long/complicated.

Is there no easy solution? I can install any tool which is needed, but I want the usage to be simple to use.

I can't give the bounty to all of you …

There are several answers which to post processing to get the needed information. Each answer uses a different way and I don't see that one solution is better than an other.

Unfortunately there seems to be no unix/linux which can do this out of the box. But that's not the fault of you, who tried to help me.

Unfortunately I can't give the bounty to all answers 🙂

I gave the bounty to the user with the least reputation points.

Best Answer

As you already figured netstat by default cannot provide full cmdline output with -p option. As per source it seem to limited to 20 chars and only lists portion of the full cmdline

You could write your own wrapper around netstat to display full details. Added below snippet of python code which displays full cmd line.

#!/usr/bin/env python

from subprocess import Popen,PIPE

out,err = Popen(['netstat','-antlp'],stdout=PIPE).communicate()

for l in out.splitlines():
    line = l.split()
    if '/' in line[-1]:
        p = line[-1].split('/')[0]
        line[-1] = str(p) + ' -> ' + open('/proc/'+p+'/cmdline','r').readline().split('-')[0]
    print '\t'.join(line)

Sample output:

$ sudo ./netstat.py 
Active  Internet    connections (servers    and established)
Proto   Recv-Q  Send-Q  Local   Address Foreign Address State   PID/Program name
tcp 0   0   0.0.0.0:11443   0.0.0.0:*   LISTEN  4007 -> /usr/sbin/apache2
tcp 0   0   192.168.2.125:53    0.0.0.0:*   LISTEN  3055 -> /usr/sbin/named
tcp 0   0   172.17.0.1:53   0.0.0.0:*   LISTEN  3055 -> /usr/sbin/named
tcp 0   0   192.168.125.1:53    0.0.0.0:*   LISTEN  3055 -> /usr/sbin/named
tcp 0   0   192.168.0.200:53    0.0.0.0:*   LISTEN  3055 -> /usr/sbin/named
tcp 0   0   127.0.0.1:53    0.0.0.0:*   LISTEN  3055 -> /usr/sbin/named
tcp 0   0   0.0.0.0:22  0.0.0.0:*   LISTEN  3125 -> /usr/sbin/sshd
tcp 0   0   127.0.0.1:631   0.0.0.0:*   LISTEN  30845 -> /usr/sbin/cupsd
tcp 0   0   127.0.0.1:25    0.0.0.0:*   LISTEN  3884 -> /usr/sbin/exim4
tcp 0   0   127.0.0.1:953   0.0.0.0:*   LISTEN  3055 -> /usr/sbin/named
tcp 0   0   0.0.0.0:32765   0.0.0.0:*   LISTEN  3014 -> /sbin/rpc.statd
tcp 0   0   0.0.0.0:8895    0.0.0.0:*   LISTEN  4078 -> /home/cv/jdk1.8.0_31/bin/java
tcp 0   0   0.0.0.0:23423   0.0.0.0:*   LISTEN  4078 -> /home/cv/jdk1.8.0_31/bin/java
tcp 0   0   0.0.0.0:32767   0.0.0.0:*   LISTEN  3827 -> /usr/sbin/rpc.mountd
tcp 0   0   0.0.0.0:23424   0.0.0.0:*   LISTEN  4078 -> /home/cv/jdk1.8.0_31/bin/java
tcp 0   0   0.0.0.0:32768   0.0.0.0:*   LISTEN  -
tcp 0   0   0.0.0.0:2049    0.0.0.0:*   LISTEN  -
tcp 0   0   0.0.0.0:23523   0.0.0.0:*   LISTEN  4078 -> /home/cv/jdk1.8.0_31/bin/java
tcp 0   0   0.0.0.0:23524   0.0.0.0:*   LISTEN  4078 -> /home/cv/jdk1.8.0_31/bin/java
tcp 0   0   192.168.0.200:44331 0.0.0.0:*   LISTEN  4078 -> /home/cv/jdk1.8.0_31/bin/java
tcp 0   0   0.0.0.0:111 0.0.0.0:*   LISTEN  3002 -> /sbin/rpcbind
tcp 0   0   0.0.0.0:8080    0.0.0.0:*   LISTEN  4007 -> /usr/sbin/apache2
tcp 0   0   0.0.0.0:6000    0.0.0.0:*   LISTEN  3908 -> /usr/bin/Xorg:0

You could write your own wrapper around similar lines and add to your toolbox!

Related Question