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

  1. sudo apt update and sudo apt install nmap avahi-utils:

    • Ensures nmap and avahi-utils are installed.

  2. resolve_device function:

    • A function to resolve and print the IP, hostname, MAC address, and manufacturer name for a given IP address.

    • Uses avahi-resolve to get the hostname.

    • Uses arp to get the MAC address.

    • Uses resolve_manufacturer to get the manufacturer name from the MAC address.

  3. resolve_manufacturer function:

    • 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.

  4. gateway=$(ip route | grep default | awk '{print $3}'):

    • Retrieves the gateway IP address using the ip route command.

  5. resolve_device "$gateway":

    • Calls the resolve_device function with the gateway IP address to ensure the gateway is included in the output.

  6. 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 nmap output to get lines containing “Nmap scan”.

    • Extracts the IP addresses from the filtered nmap output.

    • Loops through each IP address and calls the resolve_device function for each IP address, excluding the gateway.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out comment
Enter name