On Mon, Jul 29, 2019 at 01:44:51PM +0100, 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
My script that uses nmap feeds the output into awk for processing, for straightforward selection and formmatting awk is sometimes the best approach. If more logic and decisions are involved then I move on to Python.
My script just lists IP, host name, MAC and manufacturer. If not run as root then it just reports IP and host name.
#!/bin/bash nmap -sP 192.168.1.0/24 | awk ' /Nmap scan report for/\ {if (NF == 6) printf("\n%-15.15s %-26.26s", substr(substr($6, 2), 1, length($6)-2), $5) else printf("\n%-43.43s", $5)} /MAC Address/\ {printf(" %s %s %s %s", $3, $4, $5, $6)} END {printf("\n")}'