Recently I needed to find out who is connecting to an Oracle database and at the same time I wanted to see the load the specific connection add to the CPU. So in short I needed IP Address and Port tied to a Unix Pid.
I wrote this quick and dirty python script.
#!/usr/bin/pythonimport subprocess
## No doubt you would want to exclude some non local or expected IP addressesexcludeIPs="10.2.16.86|10.2.16.62|10.2.16.83|\*.\*"
p = subprocess.Popen("/usr/bin/netstat -an | grep 1521 | awk '{print $2}' | egrep -v '" + excludeIPs + "'", stdout=subprocess.PIPE, shell=True)nonlocals= p.stdout
if nonlocals <> '': p = subprocess.Popen("pfiles `ls /proc` 2>/dev/null", stdout=subprocess.PIPE, shell=True) try: outs, errs = p.communicate() except TimeoutExpired: p.kill() outs, errs = p.communicate()
pfiles = outs
for line in nonlocals: line=line.strip() (IP,port) = line.rsplit('.',1) print ("Going to find PID for connection with IP %s and port %s" % (IP,port) )
for line in pfiles.splitlines(): if line[:1].strip() <> '': pid = line if "port: " + port in line: print pidI plan to enhance this script a little bit but for now it did exactly what I needed.