-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnmap-for-poor-people.sh
executable file
·103 lines (88 loc) · 1.52 KB
/
nmap-for-poor-people.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env bash
has_wait_p() {
wait --help | grep -q -- '-p'
}
usage() {
echo "Usage: $0 [--timeout TIMEOUT] HOST PORT [PORT...]"
}
echo_success() {
echo -e "🟢 \e[32m${*}\e[0m"
}
echo_error() {
echo -e "🔴 \e[31m${*}\e[0m"
}
echo_warning() {
echo -e "⚠️ \e[33m${*}\e[0m"
}
tcp_port_check() {
local host="$1" port="$2" timeout="$3"
timeout "$timeout" bash -c "echo > /dev/tcp/${host}/${port}" 2>/dev/null
}
if ! (return 0 2>/dev/null)
then
RC=0
PORTS=()
TIMEOUT=3
PIDS=()
while [[ -n "$1" ]]
do
case "$1" in
-h|--help|-\?|help)
usage
exit 0
;;
-p|--port)
PORTS+=("$2")
shift 2
;;
-t|--timeout)
TIMEOUT="$2"
shift 2
;;
*)
break
;;
esac
done
HOST="$1"
shift
if [[ -z "${PORTS[*]}" ]]
then
PORTS=("$@")
fi
if [[ -z "$HOST" ]] || [[ -z "${PORTS[*]}" ]]
then
usage >&2
exit 2
fi
for PORT in "${PORTS[@]}"
do
(
MESSAGE="${HOST}:${PORT}"
if tcp_port_check "$HOST" "$PORT" "$TIMEOUT"
then
echo_success "$MESSAGE OPEN"
else
echo_error "$MESSAGE CLOSED"
exit 1
fi
) &
PIDS[$!]="$!"
done
if has_wait_p
then
while [[ "${#PIDS[@]}" -gt 0 ]]
do
if ! wait -n -p ENDED_PID
then
RC=1
fi
unset "PIDS[${ENDED_PID}]"
done
else
echo_warning "wait does not support -p, the return code will be wrong" >&2
wait
RC="$?"
fi
exit $RC
fi