Skip to content

Instantly share code, notes, and snippets.

@ottomata
Created March 29, 2013 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ottomata/5271910 to your computer and use it in GitHub Desktop.
Save ottomata/5271910 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"
ports = []
for pid in iter_pids():
cmd = get_cmd(pid)
if pattern in cmd[0]:
print(cmd)
p_index = False
try:
p_index = cmd.index('-p')
except ValueError, e:
continue
ports.append(int(cmd[p_index + 1]))
return ports
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