Chinaunix

标题: 试写勒个script用来从port找PID,设想在Unix/Linux运行,学识浅薄,见笑 [打印本页]

作者: 黄色潜水艇    时间: 2008-04-07 17:05
标题: 试写勒个script用来从port找PID,设想在Unix/Linux运行,学识浅薄,见笑
#!/usr/bin/ksh
#  Author: TS
# This script maps portnumber to PID,tried on AIX 5.3,6.1 Solaris 5.9 Linux 2.6
# Usage: ./mapport.sh $portnum
# The script uses Eric Steed's excellent "getport.sh" script
# as it's basis, see
# http://www.sun.com/bigadmin/jsp/descFile.jsp?url=descAll/getport_sh_port_pids
#  Still in developing...
platform=`uname`
echo $platform
portnumber=$1
if [ $# -lt 1 ]
then
    echo "usage: $0 {port}"
    exit
fi
ID=$(/usr/bin/id | cut -d " " -f1 | grep root)
        if [ $ID != "uid=0(root)" ]; then
                echo "\nERROR: ($ID) You must be root to run this script."
                exit 1
        fi

echo "ID\tCommand"
echo "========================================================="

case $platform in
   "AIX" )
        netstat -Aan|egrep ".$portnumber"|awk '{print $1}'|xargs -I {} rmsock {} tcpcb|awk '{print $9,$10}';;
   "Linux"
        netstat -ntlp|grep "portnumber" |sed 's/\//  /'|awk '{print $7,$8}';;  
   "SunOS"
        for procnum in `ptree -a | grep -v ptree | awk '{print $1};'`
                do
                result=`pfiles $procnum 2> /dev/null| grep "port: $portnumber"`
                if [ ! -z "$result" ]
                then
                program=`ps -fo comm -p $procnum | tail -1`
                echo "$procnum\t$program"
                fi
                done ;;
        
   *)
        echo " OS can not be supported yet"
        exit ;;
esac
#echo $procnum
#result=`kill -9 $procnum`
#echo "Server on port $portnumber was killed"
作者: 黄色潜水艇    时间: 2008-04-07 17:19
标题: 怎么那么多图标:-(
怎么那么多图标

mapport.rar

910 Bytes, 下载次数: 36


作者: yddll    时间: 2008-04-07 20:50
发脚本记得加上代码符号以屏蔽表情

[ 本帖最后由 yddll 于 2008-4-7 20:52 编辑 ]




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2