get process name, pid and port mapping from netstat command in SunOS
I am trying to get the mapping of port number to application that is running/using the port in SunOS
$netstat开发者_JAVA百科 -tlnp
netstat: illegal option -- t
It seems the -t option is illegal in SunOS.
how can i get this mapping?
I got his script from somewhere. Log into solaris system. Open vi editor. Go into insert mode. Copy and paste this script. Close the file. Give execute permission. Run this script with -p or -P swithc. It will give an output with the PID, PROCESS Name and Port.
PCP is a script that enables administrators to see what open TCP ports are in use on a Solaris system. It maps ports to PIDs and vice versa. It accepts wildcards and will also show at a glance all open ports and their corresponding PIDs. It is nice script gives a very fine out put. Just try it.
Example:
#pcp -p PORT_NUMBER or #pcp -P PROCESS_ID
#!/usr/bin/ksh
#
# Wildcards are accepted for -p and -P options.
#
# for the help, much appreciated.
i=0
while getopts :p:P:a opt ; do
case "${opt}" in
p ) port="${OPTARG}";i=3;;
P ) pid="${OPTARG}";i=3;;
a ) all=all;i=2;;
esac
done
if [ $OPTIND != $i ]; then
echo >&2 "usage: $0 [-p PORT] [-P PID] [-a] (Wildcards OK) "
exit 1
fi
shift `expr $OPTIND - 1`
if [ "$port" ]; then
# Enter the port number, get the PID
#
port=${OPTARG}
echo "PID\tProcess Name and Port"
echo "_________________________________________________________"
for proc in `ptree -a | awk '/ptree/ {next} {print $1};'` ; do
result=`pfiles $proc 2> /dev/null| egrep "port: $port$"`
if [ ! -z "$result" ];then
program=`ps -fo comm= -p $proc`
echo "$proc\t$program\t$port\n$result"
echo "_________________________________________________________"
fi
done
elif [ "$pid" ]; then
# Enter the PID, get the port
#
pid=$OPTARG
# Print out the information
echo "PID\tProcess Name and Port"
echo "_________________________________________________________"
for proc in `ptree -a | awk '/ptree/ {next} $1 ~ /^'"$pid"'$/ {print $1};'`; do
result=`pfiles $proc 2> /dev/null| egrep port:`
if [ ! -z "$result" ];then
program=`ps -fo comm= -p $proc`
echo "$proc\t$program\n$result"
echo "_________________________________________________________"
fi
done
elif [ $all ]; then
# Show all PIDs, Ports and Peers
#
echo "PID\tProcess Name and Port"
echo "_________________________________________________________"
for proc in `ptree -a | sort -n | awk '/ptree/ {next} {print $1};'` ; do
out=`pfiles $proc 2>/dev/null| egrep "port:"`
if [ ! -z "$out" ];then
name=`ps -fo comm= -p $proc`
echo "$proc\t$name\n$out"
echo "_________________________________________________________"
fi
done
fi
exit 0
If you don't have lsof installed, here is one way using standard Solaris commands:
pfiles /proc/* 2>/dev/null | nawk -v port=$port '
/^[0-9]/ { cmd=$0; type="unknown"; continue }
$1 == "SOCK_STREAM" { type="tcp" }
$1 == "SOCK_DGRAM" { type="udp" }
$2 == "AF_INET" { if((port!="")&&($5!=port)) continue;
if(cmd!="") { printf("%s\n %s:%s/%s\n",cmd,$3,$5,type); cmd="" }
else { printf(" %s:%s/%s\n",cmd,$3,$5,type); }}'
Set the port variable to the port number you are looking for, if any, or leave it unset to see all IPV4 ports in use.
精彩评论