IT management/strategy

Mapping Ports and PIDs

May 6, 2009, 11:56 AM — 

There are many reasons why you might need to determine what process is using a particular port or, conversely, what port(s) a particular process has open. There are also a number of tools such as lsof (list open files) and pfiles that can provide this information to you in various formats.

You can, for example, use the pfiles command to find out that process 27727 (an Apache process) has port 80 open:

# pfiles 27727 | grep -i port
        sockname: AF_INET 0.0.0.0  port: 80

If you don't use grep to pare down your display to just the open ports, you will see information on open files as well as ports and would then have to locate the port in the output.

You can view the same output for all snmpd processes like this:

# pfiles `pgrep snmpd` | grep port:
        sockname: AF_INET 0.0.0.0  port: 16161
        sockname: AF_INET 0.0.0.0  port: 32808
        sockname: AF_INET 0.0.0.0  port: 32809
        sockname: AF_INET 0.0.0.0  port: 161
        sockname: AF_INET 0.0.0.0  port: 32817
        sockname: AF_INET 0.0.0.0  port: 32873
	...

It gets a little trickier if you want a nice clean listing showing each PID along with its ports. Here's an example:

# for P in `pgrep snmpd`
> do
>     pfiles $P | egrep "$P|port:"
> done
10084:  /usr/lib/snmp/snmpdx -y -c /etc/snmp/conf
        sockname: AF_INET 0.0.0.0  port: 16161
        sockname: AF_INET 0.0.0.0  port: 32808
        sockname: AF_INET 0.0.0.0  port: 32809
10137:  /usr/sfw/sbin/snmpd
        sockname: AF_INET 0.0.0.0  port: 161
        sockname: AF_INET 0.0.0.0  port: 32817
        sockname: AF_INET 0.0.0.0  port: 32873
	...

Note that, in the commands above, I specified "port:" rather than simply "port" to avoid including in the display files (such as /export/home/jdoe/myfile) that happen to have the word "port" in their paths.

An even easier and more generalizable way to extract process/port mappings from your system is to make use of a Korn script named "pcp" (for "PID con Port").

The pcp script uses pfiles and other "proc" commands such a ptree to provide PID and port information in a tidy display. In fact, you can ask the script to supply ports associated with a process by using the -P option and specifying the PID or you can ask the script to supply PIDs responsible for certain open ports by using the -p option and specifying the port. Got that?

Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world

I like it!
Comments

port/process info

As always, Sandra, you've come through with some simple and great information on managing *nix. Thanks a bunch for this article! :)
| reply

Warning!!!!!

The pfiles command, and the pcp script that uses it seemed a great idea. I tried it on a test machine and it worked a treat, then I tried it on a Sybase database server - ASE 12.5.3 running on Solaris 8. It hung the database server! I had to kill the dataservers and restart them.

WARNING: pfiles is not like ps. It isn't harmless!

I don't know what it did, but at one point there was a message saying that it couldn't gain control (presumably of the process) but why it would want to ???

USE AT OWN RISK!
| reply
peer-to-peer

Esther Schindler
If the comments are ugly, the code is ugly

claird
SVG a graphics format for 21st century

pasmith
Take Chrome OS for a test spin

Sandra Henry-Stocker
Solaris Tip: Have Your Files Changed Since Installation?

sjvn
64-bits of protection?

jfruh
Android fragments vs. the iPhone monolith

mikelgan
What Gizmodo missed about the Pro WX Wireless USB disk drive

 

Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325

Join the conversation here

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace