Get mac
Jump to navigation
Jump to search
#!/bin/bash
# get MAC address for input IP address
# or exit if not on network
default_subnet="123.45.67"
function usage {
echo "Usage: $0 <ip-address>"
}
# parse inputs
if (( $# < 1 )); then
echo "Error: Not enough arguments"
usage
exit 1;
fi
ip=$1
# if input IP is just a single number, append it to default subnet
ip_period=`echo $ip | grep "\."`
#echo "ip_period is $ip_period"
if [[ $ip_period == "" ]]; then
ip=${default_subnet}.$ip
fi
#echo "IP address is $ip"
ping_string=`ping -c 1 $ip | grep icmp_seq`
#echo "ping string is $ping_string"
unreachable_string=`echo $ping_string | grep Unreachable`
unreachable=${#unreachable_string}
#echo "unreachable is $unreachable"
if (( "$unreachable" <= 0 )); then
mac=`/usr/sbin/arp -a $ip | awk '{print $4}'`
#echo "mac is $mac"
if [[ "$mac" == "entries" ]]; then
thisip=`/sbin/ifconfig eth0 | grep "inet addr:" | awk '{print $2}' | awk -F: '{print $2}'`
#echo "thisip is $thisip"
if [[ "$ip" == "$thisip" ]]; then
mac=`/sbin/ifconfig eth0 | grep HWaddr | awk '{print $5}'`
echo $mac
fi
else
echo $mac
fi
fi
exit 0