-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping_scan
executable file
·105 lines (86 loc) · 2.85 KB
/
ping_scan
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
#! /usr/bin/env bash
# Send an ICMP echo request to each of the IPs given.
#
# Copyright (C) 2011-2024 Erik Auerswald <[email protected]>
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.
#
# Requires the ping program of Solaris or an output-compatible one.
VERSION='2024-09-22-01'
PROG="$(basename "$0")"
PING="${PING_WRAPPER:-ping}"
version()
{
echo "$PROG version $VERSION"
echo "Copyright (c) 2011-2024 by Erik Auerswald <[email protected]>"
}
usage() { echo "Usage: $PROG [-h] [-L] [-V] [-t TIMEOUT] [-v] [-u] [IP...]"; }
help()
{
version
usage
cat <<-EOH
Options:
-h print this help and exit
-t TIMEOUT specify the timeout value used for ping (default ${TIMEOUT}s)
-v print additional runtime info
-u print not reachable hosts instead
-L print license and exit
-V print version information and exit
$PROG sends ICMP echo requests to the hosts (IP addresses) specified
and prints every host that answers with an ICMP echo reply.
Specify IP addresses as command line arguments. If no IP addresses are given
as arguments, IP addresses are read from STDIN, one IP address per line.
With the -u option, only hosts / IP addresses that do not answer are printed.
EOH
}
license()
{
cat <<-EOL
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved. This file is offered as-is,
without any warranty.
EOL
}
TIMEOUT=1
VERB=0
UNREACHABLE=0
while getopts ':t:hvuLV' OPT; do
case "$OPT" in
't') TIMEOUT=${OPTARG:-2};;
'h') help; exit 0;;
'v') VERB=1;;
'u') UNREACHABLE=1;;
'L') version; license; exit 0;;
'V') version; exit 0;;
'?') echo "$PROG: error: unknown option '-$OPTARG'"; usage; exit 1;;
':') echo "$PROG: error: argument required for option '-$OPTARG'";
usage; exit 1;;
*) echo "$PROG: error: getopts() failure"; exit 1;;
esac
done
shift $((OPTIND - 1))
test "$#" -eq 0 && test "${VERB}" -eq 1 &&
echo 'reading IP addresses from STDIN, one IP address per line'
test "${VERB}" -eq 1 &&
echo "Sending ICMP echo requests and waiting for results..." \
"(timeout ${TIMEOUT}s)"
test \( "${VERB}" -eq 1 \) -a \( "${UNREACHABLE}" -eq 1 \) &&
echo "Printing not reachable hosts."
PATTERN='is alive'
test "${UNREACHABLE}" -eq 1 && PATTERN='no answer from'
if test "$#" -gt 0; then
while test $# -ge 1; do
{ $PING -n "$1" "$TIMEOUT" | grep -F "$PATTERN"; } &
shift
done
else
while read -r IP; do
{ $PING -n "$IP" "$TIMEOUT" | grep -F "$PATTERN"; } &
done
fi | sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n -u
exit 0