{"id":968,"date":"2016-06-23T04:51:05","date_gmt":"2016-06-23T11:51:05","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=968"},"modified":"2016-06-23T04:51:05","modified_gmt":"2016-06-23T11:51:05","slug":"solaris-find-process-id-tied-to-ip-address","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/solaris-find-process-id-tied-to-ip-address\/","title":{"rendered":"Solaris Find Process Id tied to IP Address"},"content":{"rendered":"

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.<\/p>\n

I wrote this quick and dirty python script.<\/p>\n

\r\n#!\/usr\/bin\/python\r\nimport subprocess\r\n\r\n## No doubt you would want to exclude some non local or expected IP addresses\r\nexcludeIPs="10.2.16.86|10.2.16.62|10.2.16.83|\\*.\\*"\r\n\r\np = subprocess.Popen("\/usr\/bin\/netstat -an | grep 1521 | awk '{print $2}' | egrep -v '" + excludeIPs + "'", stdout=subprocess.PIPE, shell=True)\r\nnonlocals= p.stdout\r\n \r\nif nonlocals <> '':\r\n  p = subprocess.Popen("pfiles `ls \/proc` 2>\/dev\/null", stdout=subprocess.PIPE, shell=True)\r\n  try:\r\n    outs, errs = p.communicate()\r\n  except TimeoutExpired:\r\n    p.kill()\r\n    outs, errs = p.communicate()\r\n\r\n  pfiles = outs\r\n\r\n  for line in nonlocals:\r\n    line=line.strip()\r\n    (IP,port) = line.rsplit('.',1)\r\n    print ("Going to find PID for connection with IP %s and port %s" % (IP,port) )\r\n\r\n    for line in pfiles.splitlines():\r\n      if line[:1].strip() <> '':\r\n        pid = line\r\n      if "port: " + port in line:\r\n        print pid\r\n<\/pre>\n

I plan to enhance this script a little bit but for now it did exactly what I needed.<\/p>\n","protected":false},"excerpt":{"rendered":"

Recently I needed to find out who is connecting to an Oracle database and at the same time I wanted<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,14],"tags":[],"class_list":["post-968","post","type-post","status-publish","format-standard","hentry","category-python","category-solaris"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=968"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/968\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}