List LAN IP Addresses Host Names MAC addresses and Owners
Linux script to discover active hosts and resolve IP, .local hostnames, MAC addresses, and owners
Install nmap and avahi-utils (if not already installed):
sudo apt update sudo apt install nmap avahi-utils
Short nmap command to list IP, hosts, etc. but omits .local devices
sudo nmap -sn -R 192.168.1.0/24
# Terminal script to list IP Addresses, Host Names, MAC addresses and vendors
resolve_device() {
local ip=$1
echo "Resolving IP: $ip"
local hostname=$(avahi-resolve -4 -a "$ip" 2>/dev/null | awk '{print $2}')
if [ -z "$hostname" ]; then
hostname="Unknown"
fi
local mac_address=$(arp -n | grep "$ip" | awk '{print $3}')
if [ -z "$mac_address" ]; then
mac_address="Unknown"
manufacturer="Unknown"
else
manufacturer=$(resolve_manufacturer "$mac_address")
if [ -z "$manufacturer" ]; then
manufacturer="Unknown"
fi
fi
echo "IP: $ip, Hostname: $hostname, MAC Address: $mac_address, Manufacturer: $manufacturer"
}
# Function to resolve the manufacturer name from the MAC address
resolve_manufacturer() {
local mac_address=$1
local retry_count=3
local delay=1
for (( i=0; i<retry_count; i++ )); do local response=$(curl -s "https://api.macvendors.com/$mac_address" 2>/dev/null)
if [ -n "$response" ] && [[ "$response" != *"{\"errors"* ]]; then
echo "$response"
return
fi
sleep $delay
done
echo "Unknown"
}
# Get the gateway IP address
local gateway=$(ip route | grep default | awk '{print $3}')
# Resolve and print the gateway information
resolve_device "$gateway"
# Resolve and print the information for other devices
sudo nmap -sn 192.168.1.0/24 | grep "Nmap scan" | awk '{print $5}' | while read ip; do
if [ "$ip" != "$gateway" ]; then
resolve_device "$ip"
fi
done
Explanation
-
sudo apt updateandsudo apt install nmap avahi-utils:-
Ensures
nmapandavahi-utilsare installed.
-
-
resolve_devicefunction:-
A function to resolve and print the IP, hostname, MAC address, and manufacturer name for a given IP address.
-
Uses
avahi-resolveto get the hostname. -
Uses
arpto get the MAC address. -
Uses
resolve_manufacturerto get the manufacturer name from the MAC address.
-
-
resolve_manufacturerfunction:-
A function to resolve the manufacturer name from the MAC address.
-
Uses a retry mechanism with a delay to handle rate limits and transient errors.
-
Tries up to 3 times with a 1-second delay between retries.
-
Returns “Unknown” if the manufacturer name cannot be resolved after retries.
-
-
gateway=$(ip route | grep default | awk '{print $3}'):-
Retrieves the gateway IP address using the
ip routecommand.
-
-
resolve_device "$gateway":-
Calls the
resolve_devicefunction with the gateway IP address to ensure the gateway is included in the output.
-
-
sudo nmap -sn 192.168.1.0/24 | grep "Nmap scan" | awk '{print $5}' | while read ip; do ... done:-
Scans the specified subnet to discover active hosts.
-
Filters the
nmapoutput to get lines containing “Nmap scan”. -
Extracts the IP addresses from the filtered
nmapoutput. -
Loops through each IP address and calls the
resolve_devicefunction for each IP address, excluding the gateway.
-