Linux Shell 获取端口范围内可用端口
脚本内容#!/bin/bash# portRange="80-81"# 可用于读取配置文件# rangeStart=$(echo ${portRange} | awk -F '-' '{print $1}')# rangeEnd=$(echo ${portRange} | awk -F '-' '{print $2}')rangeStart=$1rangeEnd=$2if [ $1 -le $2
·
- 脚本内容
#!/bin/bash
# portRange="80-81" # 可用于读取配置文件
# rangeStart=$(echo ${portRange} | awk -F '-' '{print $1}')
# rangeEnd=$(echo ${portRange} | awk -F '-' '{print $2}')
rangeStart=$1
rangeEnd=$2
if [ $1 -le $2 ]; then
echo "123" > /dev/null
else
echo "error: please check port range"
exit
fi
PORT=0
# 判断当前端口是否被占用,没被占用返回0,反之1
function Listening {
# 不严谨写法
# TCPListeningnum=`netstat -an | grep ":$1 " | awk '$1 == "tcp" && $NF == "LISTEN" {print $0}' | wc -l`
# UDPListeningnum=`netstat -an | grep ":$1 " | awk '$1 == "udp" && $NF == "0.0.0.0:*" {print $0}' | wc -l`
# 严谨写法 -- 修改时间 2022-07-22
TCPListeningnum=`netstat -an | grep ":$1 " | awk '/^tcp.*/ && $NF == "LISTEN" {print $0}' | wc -l`
UDPListeningnum=`netstat -an | grep ":$1 " | awk '/^udp.*/ && $NF == "0.0.0.0:*" {print $0}' | wc -l`
(( Listeningnum = TCPListeningnum + UDPListeningnum ))
if [ $Listeningnum == 0 ]; then
echo "0"
else
echo "1"
fi
}
# 指定区间随机数
function random_range {
shuf -i $1-$2 -n1
}
# 得到随机端口
function get_random_port {
templ=0
while [ $PORT == 0 ]; do
temp1=`random_range $1 $2`
if [ `Listening $temp1` == 0 ] ; then
PORT=$temp1
fi
done
echo "port=$PORT"
}
# main
get_random_port ${rangeStart} ${rangeEnd};
- 运行示例
# 一个端口
sh test.sh 10001 10001
port=10001
# 端口范围
sh test.sh 10001 10020
port=10009
# 错误区间
sh test.sh 10020 10001
error: please check port range
参考
更多推荐
已为社区贡献1条内容
所有评论(0)