Figuring out the open network connections for processes ran by python
Posted by jpluimers on 2023/07/11
pidof python | tr " " "\n" | xargs -r -n 1 lsof -i -a -e /run/user/1001/gvfs -p
Breakdown:
- Getting the process IDs of any python process using pidof (most of my systems do not have
pgrepinstalled):
# pidof python 26128 12583 - Given the above list is space separated, and
xargsprefers line separated, lets replace spaces with newlines (I showed this before in Source: firewalld: show interfaces with their zone details and show zones in use):
# pidof python | tr " " "\n" 26128 12583 - By default,
xargssquashes all input on one line:
# pidof python | tr " " "\n" | xargs echo 26128 12583 - To work around that, you can either use the
-L 1or-n 1argument to keep them on separate lines:
# pidof python | tr " " "\n" | xargs -L 1 echo 26128 12583 # pidof python | tr " " "\n" | xargs -n 1 echo 26128 12583 - Now
lsofcan not only show open files, but also IP sockets (-i), and *only* those (-a), for a specific process ID (-p #). So by having the-pas last argument,xargswill append the process ID after it:
# pidof python | tr " " "\n" | xargs -n 1 lsof -i -a -p lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1001/gvfs Output information may be incomplete. lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1001/gvfs Output information may be incomplete. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME python 12583 jeroenp 7u IPv4 8347396 0t0 TCP 192.168.124.38:54576->192.168.124.23:1012 (ESTABLISHED) python 12583 jeroenp 8u IPv4 8345460 0t0 TCP 192.168.124.38:48250->192.168.124.23:http (CLOSE_WAIT) - The
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1001/gvfsis a warning not easy to workaround in a short manner as per [Wayback/Archive] privileges – lsof: WARNING: can’t stat() fuse.gvfsd-fuse file system – Unix & Linux Stack Exchange (thanks [Wayback/Archive] pabouk and [Wayback/Archive] jmunsch):
In your case
lsofdoes not need to check the GVFS file systems so you can exclude thestat()calls on them using the-eoption (or you can just ignore the waring):lsof -e /run/user/1000/gvfs(via: [Wayback/Archive] lsof: WARNING: can’t stat() fuse.gvfsd-fuse file system /run/user/1001/gvfs – Google Search)
So you get this:
# pidof python | tr " " "\n" | xargs -n 1 lsof -i -a -e /run/user/1001/gvfs -p COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME python 12583 jeroenp 7u IPv4 8347396 0t0 TCP 192.168.124.38:54576->192.168.124.23:1012 (ESTABLISHED) python 12583 jeroenp 8u IPv4 8345460 0t0 TCP 192.168.124.38:48250->192.168.124.23:http (CLOSE_WAIT) - When there are no process IDs, you do not want to run
lsof, andxargshas an argument just for that:-r, see my earlier post Source:-rargument to pipe (no argument for MacOS)- If no input is given to xargs, don’t let xargs run the utility – Unix & Linux Stack Exchange, so you get this
# pidof python | tr " " "\n" | xargs -r -n 1 lsof -i -a -e /run/user/1001/gvfs -p
Via:
- [Wayback/Archive] linux find network connections open for process – Google Search
- [Wayback/Archive] networking – Show network connections of a process – Unix & Linux Stack Exchange (thanks [Wayback/Archive] alxrem and [Wayback/Archive] ᴜsᴇʀ)
–jeroen






Leave a comment