Skip to content

Instantly share code, notes, and snippets.

@koonix
Created September 10, 2022 20:34
Show Gist options
  • Save koonix/ea4f3d9a9850454479e09319957af98d to your computer and use it in GitHub Desktop.
Save koonix/ea4f3d9a9850454479e09319957af98d to your computer and use it in GitHub Desktop.
detect and show IP Address, MAC Address and OS of all computers on local network.
#!/bin/sh
# detect and show IP Address, MAC Address and OS of local network computers.
# requires nmap and ip(iproute2).
gateway=$(ip route); gateway=${gateway#*via }; gateway=${gateway%% *}
localip=$(ip route get 1); localip=${localip#*src }; localip=${localip%% *}
scan=$(sudo nmap -sT -n -O --host-timeout 1m "${gateway:?}"/24) || exit 1
printf '%s\n' "${scan:?}" | while IFS= read -r line; do case $line in 'Nmap scan report for '*)
unset ip mac os maker
ip=${line##* }
[ "$ip" = "$gateway" ] || [ "$ip" = "$localip" ] && continue
while IFS= read -r line; do case $line in
Skipping*) break ;;
'MAC Address:'*) line=${line#*: }; mac=${line%% *}; maker=${line#* }; maker=${maker#*(}; maker=${maker%)*} ;;
'OS details:'*|*'OS guesses'*) os=${line#*: }; os=${os%%,*} ;;
'Network Distance'*) printf '%s\t%s\t%s\t%s\n' "$ip" "$mac" "$os" "$maker"; break ;;
esac; done
;; esac; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment