Created
March 29, 2013 16:26
-
-
Save ottomata/5271910 to your computer and use it in GitHub Desktop.
This file contains 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" | |
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