-
Notifications
You must be signed in to change notification settings - Fork 17
/
pingsweep.sh
158 lines (116 loc) · 3.45 KB
/
pingsweep.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
IPV4REGEX="^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"
PINGCMD=$(command -v fping)
USAGE="Syntax: $0 [-h] -i <network> [-s <int32 starting port>] [-e <int32 ending port>]
OsbornePro pingsweep 2.1 ( https://osbornepro.com )
NOTE: This command is most efficient on Linux distros that have fping installed
Usage: pingsweep -i <string format is #.#.#> [[-s <start address>] [-e <end address>]]
OPTIONS:
-h : Displays the help information for the command.
-i : Set the network subnet to perform the ping sweep on
-s : Set the starting IP address to begin the scan from
-e : Set the ending IP Address to scan too. Default
EXAMPLES:
pingsweep -i 192.168.0
# This example performs a ping sweep from 192.168.0.1 to 192.168.0.254
pingsweep -i 192.168.0 -s 200
# This example performs a ping sweep from 192.168.0.200 to 192.168.0.254
pingsweep -i 192.168.0 -s 128 -e 192
# This example performs a ping sweep from 192.168.0.128 to 192.168.0.192
"
function get_cmd {
# Determines whether fping or ping will be used
if [ -z $PINGCMD ]; then
CMD="ping -s 16 -c 1 -i 1 -U -W 1 \$HOST | grep 'bytes from' &" && echo "[*] fping command NOT found and will NOT be used"
else
CMD="fping -c1 -t300 \$HOST 2> /dev/null 1> /dev/null" && echo "[*] fping command found and will be used"
fi
} # End function get_cmd
function allow_ctrlc {
# Allow Ctrl+C to kill pingsweep
trap '
trap - INT # restore default INT handler
kill -s INT "$$"
' INT
} # End function allow_ctrlc
function print_usage {
printf "$USAGE\n" >&2
exit 1
} # End function print_usage
function validate_start {
# Validate start parameter is an integer between 1 and 254
if [ -z "$start" ]; then
START=1
elif [ "$start" -lt 255 ] && [ "$start" -ge 1 ] || ERROR="Start parameter needs to be an integer between 1 and 254"; then
if [ ! $ERROR ]; then
START=$start
else
printf "$ERROR\n"
exit
fi
fi
} # End function validate_start
function validate_end {
# Validate positional parameter 3 is an integer between $2 and 254
if [ -z "$end" ]; then
END=254
elif [ "$end" -lt 255 ] && [ "$end" -gt "$start" ] || ERROR="Last ending IP address needs to be an integer between the value of $start and 254"; then
if [ ! $ERROR ]; then
END=$end
else
printf "$ERROR\n"
exit
fi
fi
} # End function validate_end
function validate_ipv4 {
echo "[*] Verifying $ipv4 value"
if [[ "$ipv4" =~ "$IPV4REGEX" ]] || IPERROR="Valid IP subnet was not defined. For more help execute 'pingsweep -h' Example 172.16.32 "; then
if [ ! "$IPERROR" ]; then
printf "[x] A valid network value was not defined, you entered $ipv4. Used -h for more info. Example: 172.16.0\n"
exit 1
fi
fi
} # End function validate_ipv4
function execute_pingsweep {
printf "[*] Starting Ping Sweep\n"
echo -e "------------\nActive Hosts\n------------"
for i in $(seq $START $END 2> /dev/null); do
HOST=$(echo $ipv4.$i)
eval $CMD
if [ "$?" = 0 ]; then
printf "$HOST\n"
fi
done
printf "[*] Ping Sweep execution completed\n"
} # End function execute_pingsweep
while [ ! -z "$1" ]; do
case "$1" in
-i)
shift
ipv4=$1
;;
-s)
shift
start=$1
;;
-e)
shift
end=$1
;;
*)
print_usage
;;
esac
shift
done
get_cmd
allow_ctrlc
validate_ipv4
validate_start
validate_end
if [ -z $PINGCMD ]; then
execute_pingsweep | grep bytes | cut -d" " -f4 | cut -d":" -f1
else
execute_pingsweep
fi