Skip to content

Instantly share code, notes, and snippets.

@milimetric
Forked from ottomata/get_udp2log_ports.py
Last active December 15, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milimetric/5272159 to your computer and use it in GitHub Desktop.
Save milimetric/5272159 to your computer and use it in GitHub Desktop.
def get_udp2log_ports():
"""Returns the listen ports of running udp2log processes"""
pattern = "/usr/bin/udp2log"
return [get_p(cmd) for cmd in [get_cmd(pid) for pid in iter_pids()] if has_p(pattern, cmd)]
def has_p(pattern, cmd):
return pattern in cmd[0] and '-p' in cmd
def get_p(cmd):
return int(cmd[cmd.index('-p') + 1])
def get_cmd(pid):
"""Get the command-line instantiation for a given process id"""
with open('/proc/%s/cmdline' % pid, 'rt') as f:
return f.read().split('\x00')
def iter_pids():
"""Returns an iterator of process ids of all running processes"""
return (int(node) for node in os.listdir('/proc') if node.isdigit())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment