-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnc_scan.sh
50 lines (42 loc) · 1.06 KB
/
nc_scan.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
# 检查是否提供了IP地址和端口列表
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: $0 <target_host> <ports> [--debug]"
echo "Example: $0 192.168.1.1 8080,443,9090,443-1000 --debug"
exit 1
fi
# 目标主机地址
HOST=$1
# 端口列表
IFS=',' read -r -a PORTS <<< "$2"
# 检查是否使用 --debug 参数
DEBUG=false
if [ "$3" == "--debug" ]; then
DEBUG=true
fi
# 超时时间(秒)
TIMEOUT=1
# 测试端口函数
test_port() {
local port=$1
if nc -z -w $TIMEOUT $HOST $port &> /dev/null; then
echo -en "Testing $HOST:$port Connected\n"
else
if [ "$DEBUG" == true ]; then
echo -en "Testing $HOST:$port Failed\n"
fi
fi
}
# 遍历端口列表并测试
for port_range in "${PORTS[@]}"; do
if [[ $port_range == *"-"* ]]; then
IFS='-' read -r start_port end_port <<< "$port_range"
for ((port=start_port; port<=end_port; port++)); do
test_port $port &
done
else
test_port $port_range &
fi
done
# 等待所有后台任务完成
wait