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