On 29 Jul 13:44, Mark Rogers wrote:
I have a simple bash script for locating Raspberry Pis on the network:
#!/bin/bash PIMAC=B8:27:EB sudo nmap -sP 192.168.251.0/24 | grep -i " $PIMAC" -A0 -B2
Whilst the output gives me the answer I want I'd like to tidy up the formatting. The current result is:
Nmap scan report for 192.168.10.20 Host is up (-0.10s latency). MAC Address: B8:27:EB:22:1C:05 (Raspberry Pi Foundation) -- Nmap scan report for 192.168.10.24 Host is up (-0.10s latency). MAC Address: B8:27:EB:22:1C:05 (Raspberry Pi Foundation)
What I want out of it is something more like: MAC Address: B8:27:EB:22:1C:05 (Raspberry Pi Foundation) IP Address: 192.168.10.20 IP Address: 192.168.10.24
Well, personally, I'd do something like:
--- BEGIN --- #!/bin/bash
ip_mac_of_pi="$(sudo nmap -sP -n 192.168.1.0/24 | \ sed -ne '/Nmap scan report for / { s#Nmap scan report for ##; h; }; /MAC Address.*Raspberry Pi/ { s#MAC Address: ##; s# (.*$##; H; x; s#\n# #; p; }')"
declare -A pi_macs
while read -a ip_mac; do mac="${ip_mac[1]}" ip="${ip_mac[0]}" if [ -v "pi_macs[$mac]" ]; then pi_macs[$mac]+=" $ip" else pi_macs[$mac]="$ip" fi done <<< "$ip_mac_of_pi"
for mac in "${!pi_macs[@]}"; do echo $mac: ${pi_macs[$mac]} done --- END ---
Which will give them as
mac: list of ips
Thanks,