forked from framps/raspberryTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindRaspis.sh
executable file
·135 lines (105 loc) · 3.52 KB
/
findRaspis.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
#!/bin/bash
# Find all existing Raspberries in local subnet
#
# Search for reservered mac addresse for Raspberries defined on
# https://udger.com/resources/mac-address-vendor-detail?name=raspberry_pi_foundation
#
# Copyright (C) 2021 framp at linux-tips-and-tricks dot de
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
set -euo pipefail
VERSION=0.3
MYSELF="$(basename "$0")"
MYNAME=${MYSELF%.*}
# check for required commands and required bash version
if ! command -v nmap COMMAND &> /dev/null; then
echo "Missing required program nmap."
exit 255
fi
if ! command -v host COMMAND &> /dev/null; then
echo "Missing required program host."
exit 255
fi
if (( ${BASH_VERSINFO[0]} < 4 )); then
echo "Minimum bash 4.0 is required. You have $BASH_VERSION."
exit 255
fi
# define defaults
DEFAULT_SUBNETMASK="192.168.0.0/24"
DEFAULT_MAC_REGEX="b8:27:eb|dc:a6:32|e4:5f:01"
# see https://udger.com/resources/mac-address-vendor-detail?name=raspberry_pi_foundation
INI_FILENAME="./.${MYNAME}"
# help text
if (( $# >= 1 )) && [[ "$1" =~ ^(-h|--help|-\?)$ ]]; then
cat << EOH
Usage:
$MYSELF Scan subnet $DEFAULT_SUBNETMASK for Raspberries
$MYSELF <subnetmask> Scan subnet for Raspberries
$MYSELF -h | -? | --help Show this help text
Defaults:
Subnetmask: $DEFAULT_SUBNETMASK
Mac regex: $DEFAULT_MAC_REGEX
Example:
$MYSELF 192.168.179.0/24
Init file $INI_FILENAME can be used to customize the Mac Regex. Every line has to define a Mac Regex
Example file contents for $INI_FILENAME:
b8:27:eb
dc:a6:32
e4:5f:01
EOH
exit 0
fi
# read options
MY_NETWORK=${1:-$DEFAULT_SUBNETMASK}
# read property file with mac regexes
if [[ ! -f $INI_FILENAME ]]; then
MY_MAC_REGEX="$DEFAULT_MAC_REGEX"
else
echo "Using Mac Regex from $INI_FILENAME"
MY_MAC_REGEX=""
while read line; do
if [[ -n $MY_MAC_REGEX ]]; then
MY_MAC_REGEX="$MY_MAC_REGEX|"
fi
MY_MAC_REGEX="$MY_MAC_REGEX$line"
done < $INI_FILENAME
fi
MY_MAC_REGEX=" (${MY_MAC_REGEX})"
# define associative arrays for mac and hostname lookups
declare -A macAddress=()
declare -A hostName=()
echo "Scanning subnet $MY_NETWORK for Raspberries using Regex$MY_MAC_REGEX ..."
# scan subnet for Raspberry macs
# 192.168.0.12 ether dc:a6:32:8f:28:fd C wlp3s0 -
while read ip dummy mac rest; do
macAddress["$ip"]="$mac"
done < <(nmap -sP $MY_NETWORK &>/dev/null; arp -n | grep -E " $MY_MAC_REGEX")
echo "${#macAddress[@]} Raspberries found"
# retrieve and print hostnames
if (( ${#macAddress[@]} > 0 )); then
echo "Retrieving hostnames ..."
printf "%-15s %-17s %s\n" "IP address" "Mac address" "Hostname"
# 12.0.168.192.in-addr.arpa domain name pointer asterix.
for ip in "${!macAddress[@]}"; do
h="$(host "$ip")"
if (( ! $? )); then
read arpa dummy dummy dummy host rest <<< "$h"
host=${host::-1} # delete trailing "."
else
host="-Unknown-"
fi
printf "%-15s %17s %s\n" $ip ${macAddress[$ip]} $host
done
fi