#!/bin/bash # Check if an interface name is provided if [ -z "$1" ]; then echo "Usage: $0 " exit 1 fi INTERFACE=$1 # Get the IP subnet for the provided network interface IP_SUBNET=$(ip route show dev "$INTERFACE" | grep -v default | awk '{print $1}') # Check if the IP information was found if [ -z "$IP_SUBNET" ]; then echo "No IP address found for interface $INTERFACE." exit 1 fi # Scan the subnet using nmap, running as root echo "Scanning the subnet $IP_SUBNET..." OUTPUT=$(sudo nmap -sP "$IP_SUBNET") # Parse the nmap output and present it in a table #echo "$OUTPUT" | awk '/Nmap scan report for/{ # if ($5 ~ /^\(/) { ip=$5; name="Unknown"; } # else if ($6 ~ /^\(/) { name=$5; ip=$6; } # else { name="Unknown"; ip=$5; } # # ip=gensub(/\(|\)/, "", "g", ip); # Remove parentheses from IP # # getline; getline; mac=$3; brand=""; # # # Capture the entire remainder as brand, remove parentheses # for (i=4; i<=NF; i++) brand = brand $i " "; # brand=gensub(/^\(|\)$/, "", "g", brand); # Clean brand formatting # print name, ip, mac, brand #}' | column -t -s ' ' -o ' | ' | awk 'BEGIN {print "Name | IP Address | MAC Address | Brand\n-----------------------------------------------------------------"} {print}' # Parse the nmap output and present it in a table echo "$OUTPUT" | awk '/Nmap scan report for/{ if ($5 ~ /^\(/) { ip=$5; name="Unknown"; } else if ($6 ~ /^\(/) { name=$5; ip=$6; } else { name="Unknown"; ip=$5; } ip=gensub(/\(|\)/, "", "g", ip); # Remove parentheses from IP getline; getline; mac=$3; brand=$4; # Skip status line and move to MAC and Brand gsub(/\(|\)/, "", brand); # Clean brand formatting print name, ip, mac, brand }' | column -t -s ' ' -o ' | ' | awk 'BEGIN {print "Name | IP Address | MAC Address | Brand\n-----------------------------------------------------------------"} {print}' ## Parse the nmap output and present it in a table #echo "$OUTPUT" | awk '/Nmap scan report for/{ # if ($5 ~ /^\(/) { ip=$5; name="Unknown"; } # else if ($6 ~ /^\(/) { name=$5; ip=$6; } # else { name="Unknown"; ip=$5; } # # ip=gensub(/\(|\)/, "", "g", ip); # Remove parentheses from IP # # getline; getline; mac=$3; brand=""; # # # Capture the entire remainder as brand # if ($(NF-1) ~ /^\(/) { # Check if the second last field starts with ( # for (i=4; i<=NF; i++) brand = brand $i " "; # sub(/\s+$/, "", brand); # Trim trailing space # brand=gensub(/\((.*)\)/, "\\1", "g", brand); # Remove outer parentheses # } # print name, ip, mac, brand #}' | column -t -s ' ' -o ' | ' | awk 'BEGIN {print "Name | IP Address | MAC Address | Brand\n--------------------------------------------------------------------------------"} {print}'