Last active
March 1, 2017 17:35
-
-
Save FredLackeyOfficial/2fca24f4237b9417c1887a5ba0c9fb40 to your computer and use it in GitHub Desktop.
Simple bash function to find all active IP address, their MAC address, and the adapter manufacturer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmd_exists() { | |
command -v "$1" &> /dev/null | |
} | |
ips(){ | |
local usage="ips [%NETWORK_BASE_IP%] [%BIT_DEPTH%] [ip-only | no-sudo]"$'\n'"Default IP: 192.168.1.0"$'\n'"Default Mask: 24" | |
local addr="$1"; | |
local mask="$2"; | |
local prefix=""; | |
local suffix=""; | |
# Ensure nmap is installed | |
if ! cmd_exists "nmap"; then | |
printf "nmap is required, please install it!\n" | |
exit 1 | |
fi | |
# display help if needed | |
if [[ "$@" =~ "help" ]]; then | |
echo "$usage"; | |
return 0; | |
fi | |
# filter out details if only ips are needed | |
if [[ "$@" =~ "ip-only" ]]; then | |
suffix=" | grep report | awk '{print \$5}'"; | |
fi | |
# remove sudo if is to be run without it | |
if [[ "$@" =~ "no-sudo" ]]; then | |
prefix=""; | |
else | |
prefix="sudo "; | |
fi | |
# ensure the subnet mask is between 4 and 30 bits (default to 24) | |
if [[ "$mask" =~ ^[0-9]+$ ]] && [ "$mask" -ge 4 -a "$mask" -le 30 ]; then | |
mask="$mask"; | |
else | |
echo "Invalid mask supplied. Defaulting to 24 bits." | |
mask="24"; | |
fi | |
# proceed if the first value is a valid IP address | |
if [[ ! $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then | |
echo "Invalid IP address supplied. Defaulting to 192.168.1.0." | |
addr="192.168.1.0"; | |
fi | |
eval "${prefix}nmap $addr/$mask -n -sP${suffix}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment