websoft9/scripts/check_ports.sh

45 lines
1015 B
Bash
Raw Normal View History

2023-11-23 15:52:03 +08:00
#!/bin/bash
# Command-line options
# ==============================================================================
#
# --version
# Use the --version option to install a special version for installation. default is latest, for example:
#
# $ sudo bash install.sh --version "0.8.25"
#
# --port check ports
# Use the --port option to check port, for example:
#
# $ sudo bash check_ports.sh --port 9001,9001
# ==============================================================================
# 获取参数值
while [[ $# -gt 0 ]]; do
case $1 in
--port)
IFS=',' read -ra ports <<< "$2"
shift 2
;;
*)
shift
;;
esac
done
check_ports() {
2023-11-23 16:03:30 +08:00
local used_ports=()
2023-11-23 15:52:03 +08:00
for port in "${ports[@]}"; do
if ss -tuln | grep ":$port " >/dev/null; then
2023-11-23 16:03:30 +08:00
used_ports+=("$port")
2023-11-23 15:52:03 +08:00
fi
done
2023-11-23 16:03:30 +08:00
if [ ${#used_ports[@]} -eq 0 ]; then
echo "0"
else
IFS=','; echo "${used_ports[*]}"
fi
2023-11-23 15:52:03 +08:00
}
2023-11-23 16:03:30 +08:00
check_ports