Skip to content

Instantly share code, notes, and snippets.

@karthikcru
Created January 16, 2020 09:11
Show Gist options
  • Save karthikcru/bf455b60d65e6a81ae8478e4d01f4868 to your computer and use it in GitHub Desktop.
Save karthikcru/bf455b60d65e6a81ae8478e4d01f4868 to your computer and use it in GitHub Desktop.
Get all MAC addresses in default network
#!/usr/bin/env bash
#getting the default interface and ping the broadcast address to get all network IPs
if [ "$(uname)" == "Darwin" ]; then
interface=$(route -n get default | grep 'interface:' | grep -o '[^ ]*$')
broadcast=$(ifconfig | grep -A 2 $interface | tail -n 1 | cut -d\ -f6)
ping -c 4 -W 2000 $broadcast
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
interface=$(route | grep '^default' | grep -o '[^ ]*$')
broadcast=$(/sbin/ifconfig $interface | grep 'inet addr' | cut -d: -f3 | awk '{print $1}')
ping -c 4 -W 2 -b $broadcast
fi
arp -a | cut -d\ -f4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment