#!/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}"
原文地址:https://www.cnblogs.com/uglyliu/p/10192919.html
时间: 2024-09-30 02:38:12