Here is how to retrieve the IP address of devices in the same LAN as your Synology, based on their a MAC addresses, and vice-versa.
Click to Read More
I am using a php script to executes various bash scripts. it requires the package inetutils that can be installed using ipkg. Type in a ssh console run as root: /opt/bin/ipkg install inetutils
[php] <?php//Clean all MAC addresses from ARP table
function FlushArpTable()
{
//only work if run as root
$flush = exec("ip -s -s neigh flush all");
}
//Return 1 if the given $ip is a local one
function IsLocalIp($ip)
{
//Get all local IP address from ifconfig
//$localIp = `ifconfig | grep -Eo ‘inet (addr:)?([0-9]*\.){3}[0-9]*’ | grep -Eo ‘([0-9]*\.){3}[0-9]*’ | grep -v ‘127.0.0.1’`;
$localIp = exec("ifconfig | grep -o $ip");
return ($localIp == $ip);
}
//Ping $ip using inetutils (only this one works when not run as root)
//return -1 if package inetutils is not installed (can be done using ipkg)
function Ping($ip)
{
$ping = exec("if [ -f /opt/bin/ping ] ; then /opt/bin/ping -c1 $ip; echo 1; else echo -1; fi");
return $ping;
}
//Return MAC address related to the given $ip address in ARP table or ifconfig
//Ping the $ip if MAC is not found and $force=1
//NB.: Synology local ip are not added in the ARP table, hence the use of ifconfig
function GetMacFromArp($ip, $force = 1)
{
if (filter_var($ip, FILTER_VALIDATE_IP)) {
// Look into the arp table for the MAC address linked to the given ip address
$mac = exec("arp -a $ip | grep -o -E ‘([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}’");
$mac = trim($mac);
if ($mac == "") {
//if the ip is not in the ARP table, look for it via ifconfig
$mac = `ifconfig | grep -E "HWaddr |inet |^$" | awk ‘$1=$1’ ORS="\n\n" RS="\n\n" | grep $ip | grep -o -E ‘([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}’`;
$mac = trim($mac);
}
if ($mac == "" && $force == 1) {
echo "? ";
Ping($ip);
$mac = GetMacFromArp($ip, $force = 0);
} else if ($force == 1) {
echo "! ";
}
}
return $mac;
}
//Return ip address related to the given $mac address in ARP table or ifconfig
//NB.: Synology local ip are not added in the ARP table, hence the use of ifconfig
function GetIpFromArp($mac)
{
if (filter_var($mac, FILTER_VALIDATE_MAC)) {
// Look into the arp table for the ip linked to the given mac address
$ip = exec("arp -a | grep $mac | grep -o -E ‘\b([0-9]{1,3}\.){3}[0-9]{1,3}\b’");
if ($ip == "") {
// if no ip found in the arp table, look for it into the ifconfig
$ip = exec("ifconfig | grep -E ‘HWaddr |inet |^$’ | awk ‘$1=$1′ ORS=’\n\n’ RS=’\n\n’ | grep $mac | grep -o -E ‘addr:\b([0-9]{1,3}\.){3}[0-9]{1,3}\b’ | grep -o -E ‘\b([0-9]{1,3}\.){3}[0-9]{1,3}\b’");
}
}
return $ip;
}
function DisplayMac($ip)
{
if (filter_var($ip, FILTER_VALIDATE_IP)) {
$mac = GetMacFromArp($ip);
if (filter_var($mac, FILTER_VALIDATE_MAC)) {
echo "MAC of $ip is " . $mac;
if ($ip != GetIpFromArp($mac)) {
echo " (? " . GetIpFromArp($mac) . " ?)";
}
} else {
echo "MAC of $ip is unknown";
}
if (IsLocalIp($ip)) {
echo " (a local IP)";
}
echo "<br/>";
}
}
// Check if ping works
if (Ping("127.0.0.1") == -1) {
echo "You have to install inetutils. Ex.: using ipkg";
} else {
//clean the arp table (only work when run as root)
FlushArpTable();
//Display the mac address of an ip in the LAN
DisplayMac(‘192.168.0.19’);
//get ip of all local interfaces
$localIps = split("\n", `ifconfig | awk ‘/inet addr/{print substr($2,6)}’`);
foreach ($localIps as $ip) {
DisplayMac($ip);
}
DisplayMac(‘192.168.0.47’);
}
?>
[/php]
Leave a Reply