Files
ShellCrash/scripts/menus/check_port.sh
Feng Yu c61c637e0d 修复端口重复检查逻辑,保持函数职责单一
- check_port 函数只负责检查端口范围和占用,移除重复检查逻辑
- 端口重复检查移到 inputport 函数中,只排除当前编辑的端口变量
- 修复编辑端口时错误地跳过所有已配置端口重复检查的 bug
2026-03-22 23:52:58 +08:00

50 lines
897 B
Bash

#!/bin/sh
# Copyright (C) Juewuy
load_lang check_port
_get_netstat_cmd() {
case "$1" in
tcp) echo "netstat -ntl" ;;
udp) echo "netstat -nul" ;;
*) echo "netstat -ntul" ;;
esac
}
check_port() {
local port="$1"
local protocol="${2:-all}"
if [ "$port" -gt 65535 ] || [ "$port" -le 1 ]; then
msg_alert "\033[31m$CHECK_PORT_RANGE_ERR\033[0m"
return 1
fi
local check_cmd
check_cmd=$(_get_netstat_cmd "$protocol")
if $check_cmd 2>/dev/null | grep -q ":${port}[[:space:]]"; then
msg_alert "\033[31m$CHECK_PORT_OCCUPIED_ERR\033[0m"
return 1
fi
return 0
}
check_port_with_info() {
local port="$1"
local protocol="${2:-all}"
local check_cmd
check_cmd=$(_get_netstat_cmd "$protocol")
local conflict_line
conflict_line=$($check_cmd 2>/dev/null | grep ":${port}[[:space:]]" | head -n 1)
if [ -n "$conflict_line" ]; then
echo "$conflict_line"
return 1
fi
return 0
}