1. 程式人生 > >查詢伺服器埠指令碼

查詢伺服器埠指令碼

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