Skip to content

Solaris find process id tied to ip address

wordpress meta

title: 'Solaris Find Process Id tied to IP Address'
date: '2016-06-23T04:51:05-05:00'
status: publish
permalink: /solaris-find-process-id-tied-to-ip-address
author: admin
excerpt: ''
type: post
id: 968
    - Python
    - Solaris
tag: []
post_format: []

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.

import subprocess

## No doubt you would want to exclude some non local or expected IP addresses

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)
    outs, errs = p.communicate()
  except TimeoutExpired:
    outs, errs = p.communicate()

  pfiles = outs

  for line in nonlocals:
    (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 pid

I plan to enhance this script a little bit but for now it did exactly what I needed.