Quantcast
Channel: Jason L. Froebe - Tech tips and How Tos for Fellow Techies » netstat
Viewing all articles
Browse latest Browse all 2

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

$
0
0

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

Viewing all articles
Browse latest Browse all 2

Trending Articles