1

I would like to get information about network connections (like netcat) using the command:

/proc/net/tcp

by obtaining an output with the following columns:

sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode

Now I would like to get the PID of each row.

How can I do?

It is possible to do it with the command?

/proc/PID/net/tcp

Or the best way is to go to(via inode):

/proc/<PID>/fd/

In any case, what are the steps to follow?

invictus1306
  • 587
  • 1
  • 4
  • 19
  • In bash I usually just run: `for FOOFOO in /proc/*/net/tcp ; do ;echo $FOOFOO ; cat $FOOFOO ; done` You would need to edit this a little but could run it from C easily enough – Vality Jul 31 '14 at 10:46
  • Do you want something like this: `awk 'NR>1{system("lsof -i | grep "$10) | getline pid; print $0,pid}' /proc/net/tcp` (Note the command isn't perfect yet, but it should basically do what you want, or did I misunderstood the question?) – hek2mgl Jul 31 '14 at 10:55
  • Please also have a look here: `http://stackoverflow.com/questions/14667215/finding-a-process-id-given-a-socket-and-inode-in-python-3` – hek2mgl Jul 31 '14 at 10:58
  • @Vality the script is smart, but I can not figure out how to extract the individual PID – invictus1306 Jul 31 '14 at 12:27
  • @hek2mgl I do not want to use lsof – invictus1306 Jul 31 '14 at 12:29
  • @invictus1306 Try this instead: `pushd /proc ; for FOOFOO in * ; do ; echo $FOOFOO ; cat $FOOFOO/net/tcp 2> /dev/null ; done ; popd` That leaves each pid on it's own line. – Vality Jul 31 '14 at 12:56

0 Answers0