#!/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