查询服务器端口脚本

#!/bin/bash
#IPaddr=`ifconfig | grep inet\\\\s | grep -v "127.0.0." | sed "s/addr://g"| awk '{print $2}'`
check_port()
{
    linenum=$(netstat -tlpn | grep "\b$1\b" | wc -l);
    echo "$linenum"
}

function getfreeport()
{
    first=$1
    last=$2
    for (( port = $first; port <= ${last}; port++ ))
        do
            #(echo > /dev/tcp/$IPaddr/$port) &> /dev/null
            echo $port
        check_values=$(check_port $port);
        echo "check=$check_values";
            if [ $check_values -eq 0 ];then
                 echo "端口$port可以使用"
                 break
         return $port
        fi
        done
}

port=$(getfreeport 20880 20900)
echo "free port=${port}"

 

posted @ 2018-12-28 21:16  梦轻尘  阅读(263)  评论(0编辑  收藏  举报