Calendar
数据载入中,请稍候......
Placard
数据载入中,请稍候......
Category
数据载入中,请稍候......
Latest Entries
数据载入中,请稍候......
Latest Comments
数据载入中,请稍候......
Last Messages
数据载入中,请稍候......
User Login
数据载入中,请稍候......
Links
Information
数据载入中,请稍候......
Search
Other


Welcome to my blog!
  检测指定服务端口脚本
 

#!/bin/bash
#hahazhu 16/03/07
#detect service exists
usage(){
 echo "FORMAT:`basename $0` protocol portnumber"
 echo "EXAMPLE:`basename $0` tcp 80"
 exit 1
}
pro=$1
port=$2
portlist=`more /etc/services|grep -v "#"|sed "/^$/d"|awk '{print $1" "$2}'|tr "/
" " "|grep "$pro"|awk '{print $2}'`
porttotal=`more /etc/services|grep -v "#"|sed "/^$/d"|awk '{print $1" "$2}'|tr "
/" " "|grep "$pro"|awk '{print $2}'|wc -l`
`more /etc/services|grep -v "#"|sed "/^$/d"|awk '{print $1" "$2}'|tr "/" " "|gre
p "$pro"|awk '{print $1}'>serverlist`
res=`netstat -an|grep LISTEN|grep ":$port"`
if [ $# = 0 ];then
  usage
fi
for i in $portlist
do
    if [ $port = $i ];then
      declare -i j=$j+1
      server=`sed -n ""$j"p" serverlist`
      rm -f serverlist
      if [ "$res" != "" ];then
         echo "$server exist!"
      else
         echo "$server no live!"

      fi
      break
     else
      declare -i j=j+1
      if [ $j = $porttotal ];then
         echo "No this portnumber!"
      fi
    fi
done

[ 阅读全文 | 回复(0) | 引用通告 | 编辑

  Post  by  badboy 发表于 2007-5-15 17:44:00
发表评论:
数据载入中,请稍候......
数据载入中,请稍候......