Home » OS » AIX » HOWTO: Determine what process is listening on a port (AIX Unix specific)

HOWTO: Determine what process is listening on a port (AIX Unix specific)

I needed an easy way to determine which process was listening on a port. For AIX, you need to get the socket id from “netstat -Ana” and use the rmsock “rmsock socket_id tcpcb” to get the PID and command. It would be easy to expand this out to list command line and owner for each PID.

--------------------------------------------------------------------------------------
| Process              | PID             | Protocol | Listening On                   |
--------------------------------------------------------------------------------------
| WEBAPL               |         4915396 |      UDP |                127.0.0.1.32807 |
| WEBAPL               |         4915396 |      UDP |                127.0.0.1.32808 |
| WEBAPL               |        12058770 |      UDP |                127.0.0.1.51714 |
| WEBAPL               |        12058770 |      UDP |                127.0.0.1.51715 |
| backupserver         |        19791994 |      TCP |              192.168.1.4.50021 |
--------------------------------------------------------------------------------------
#!/bin/ksh93

OS_NAME=$( uname -s )

if [[ $OS_NAME == "AIX" ]] ; then
    echo "--------------------------------------------------------------------------------------"
    printf "| %-20s | %-15s | Protocol | %-30s |\n" "Process" "PID" "Listening On";
    echo "--------------------------------------------------------------------------------------"

    netstat -Ana | awk '
    /[0-9\*].[0-9].+LISTEN/ {
        SOCKET=$1;
        IPPORT=$5;
        "rmsock " SOCKET " tcpcb" | getline SOCKOUT;
        split(SOCKOUT, sockarray, " ");
        gsub(/[\.\(\)]/, "", sockarray[10]);
        LISTENERS[ sprintf("| %-20s | %15d | %8s | %30s |", sockarray[10], sockarray[9], "TCP", IPPORT) ] = 1;
    }
    /udp.*.[0-9]/ {
        SOCKET=$1;
        IPPORT=$5;
        "rmsock " SOCKET " inpcb" | getline SOCKOUT;
        split(SOCKOUT, sockarray, " ");
        gsub(/[\.\(\)]/, "", sockarray[10]);
        LISTENERS[ sprintf("| %-20s | %15d | %8s | %30s |", sockarray[10], sockarray[9], "UDP", IPPORT) ] = 1;
    }
    END {
        for (var in LISTENERS)
            print var

    }' | sort | uniq

    echo "--------------------------------------------------------------------------------------"
else
    echo "ERROR: Requires AIX"
    exit 1
fi
Share Button

Comments

  1. Peter says:

    Thanks, very helpful for me!!

  2. maha says:

    Thanks , nice script, getting output very understandable .. Good work

  3. Glenn L says:

    Its a great script… and I echo how understandable/readable the output table is.
    I do worry if the rmsock command has any “destructive” affects…
    Could lsof be used instead to gather the PID & such ?

  4. Glenn L says:

    Its a great script… and I echo how understandable/readable the output table is.
    I do worry if the rmsock command has any “destructive” affe
    Could lsof be used instead to gather the PID & such ?

  5. Glenn L says:

    I like the script… very neat, understandable/readable output.
    I worry if the use of the rmsock has any destructive affect.
    Could you possible use lsof instead to gather same PID info ?

    1. Unfortunately, lsof isn’t part of the standard AIX install and is often locked down even if it is. rmsock in this context isn’t destructive despite its name.

      Note with Linux or similar GNU based *nixes, lsof is part of the standard install and usually isn’t restricted.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Facebook login by WP-FB-AutoConnect