The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,262 other subscribers

Archive for July 11th, 2023

SUSE Preserves Choice in Enterprise Linux by Forking RHEL with a $10+ Million Investment | SUSE

Posted by jpluimers on 2023/07/11

https://www.suse.com/news/SUSE-Preserves-Choice-in-Enterprise-Linux/

Via

https://twitter.com/jilles_com/status/1678814306811379739

Posted in Uncategorized | Leave a Comment »

Figuring out the open network connections for processes ran by python

Posted by jpluimers on 2023/07/11

TL;DR:

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 pgrep installed):
    # pidof python
    26128 12583
    
  • Given the above list is space separated, and xargs prefers 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, xargs squashes all input on one line:
    # pidof python | tr " " "\n" | xargs echo
    26128 12583
    
  • To work around that, you can either use the -L 1 or -n 1 argument 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 lsof can not only show open files, but also IP sockets (-i), and *only* those (-a), for a specific process ID (-p #). So by having the -p as last argument, xargs will 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/gvfs is 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 lsof does not need to check the GVFS file systems so you can exclude the stat() calls on them using the -e option (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, and xargs has an argument just for that: -r, see my earlier post Source: -r argument 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:

–jeroen

Posted in *nix, *nix-tools, bash, bash, Development, lsof, Power User, Scripting, Software Development, xargs | Leave a Comment »