11

I want to know which ports are used by which processes in embedded-linux. Since it is simple embedded-linux, there are no network command lines such as netstat, lsof. (only basic command lines such as cat, cp, echo, etc exist).

A partial solution seems to be to use "cat /proc/net/tcp" and "cat /proc/net/udp" command lines. However, I am not sure the printed list from those command lines shows all ports in use, and the list does not show which process is binded to certain port.

Any comments would be appreciated.

migrated from serverfault.com May 20 '14 at 10:59

This question came from our site for system and network administrators.

  • I think this question is better suited to Unix & Linux – Iain May 20 '14 at 7:45
15

You should be able to find all open ports in /proc/net/tcp and /proc/net/udp. Each of those files have an inode column, which can be used to find the process owning that socket.

Once you have an inode number, you can run an ls command such as ls -l /proc/*/fd/* | grep socket:.$INODE to find the processes using that socket. In case a process has been set up with different file descriptors for different threads, you may need to extend the command to ls -l /proc/*/task/*/fd/* | grep socket:.$INODE in order to find them all.

  • 1
    find /proc -lname "socket:\[$INODE\]" 2> /dev/null – Sammitch May 21 '14 at 23:14
0

To find the INODES for any port execute below command

PORT=8080;cat /proc/net/* | awk -F " " '{print $2 ":" $10 }' | grep -i `printf "%x:" $PORT` | awk -F ":" '{print "PORT=" $2 ", INODE=" $3 }'

Use any INODE from above command output to find associated PID as below

find /proc -lname "socket:\[$INODE\]" 2> /dev/null | head -n 1 | awk -F "/" '{print "PID="$3}'

here $INODE is the value of any INODE

In a single line command, we can check, if any port is open and associated to any PID as below

PORT=8080;find /proc -lname "socket:\[$(cat /proc/net/* | awk -F " " '{print $2 ":" $10 }' | grep -i `printf "%x:" $PORT` | head -n 1 | awk -F ":" '{print $3}')\]" 2> /dev/null | head -n 1 | awk -F "/" '{print "PID="$3}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.