pythonwise / wholistens

#!/usr/bin/env python
'''Find out who is listening on a port'''

__author__ = "Miki Tebeka <miki@mikitebeka.com>"

from os import popen
from os.path import isdir
import re

is_int = re.compile("\d+").match

def find_pid(port):
    for line in popen("netstat -nlp 2>&1"):
        match = re.search(":(%s)\\s+" % port, line)
        if not match:
            continue

        pidname = line.split()[-1].strip()
        return pidname.split("/")[0]

    return None

def find_cmdline(pid):
    cmd = open("/proc/%s/cmdline" % pid, "rb").read()

    return " ".join(cmd.split(chr(0)))

def find_pwd(pid):
    data = open("/proc/%s/environ" % pid, "rb").read()
    for line in data.split(chr(0)):
        if line.startswith("PWD"):
            return line.split("=")[1]

    return None

def main(argv=None):
    if argv is None:
        import sys
        argv = sys.argv

    from optparse import OptionParser

    parser = OptionParser("usage: %prog PORT")
    opts, args = parser.parse_args(argv[1:])
    if len(args) != 1:
        parser.error("wrong number of arguments") # Will exit

    port = args[0]
    pid = find_pid(port)
    if not (pid and is_int(pid)):
        raise SystemExit(
            "error: can't find who listens on port %s"
            " [try again with sudo?] " % port)

    if not isdir("/proc/%s" % pid):
        raise SystemExit("error: can't find information on pid %s" % pid)

    pwd = find_pwd(pid) or "<unknown>"
    print "%s (pid=%s, pwd=%s)" % (find_cmdline(pid), pid, pwd)

if __name__ == "__main__":
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.