-
-
Save milimetric/5272159 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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