Month: January 2018

  • Synology: how to get MAC address from IP address and vice versa

    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]

    Loading

  • Synology: System internal service [apparmor] failed to start

    Since I have upgraded my DSM 6.1.4 to the latest update, I have this error : “System internal service [apparmor] failed to start.”

    I did contact Synology ans the only solution is to reintall the DSM

    Click to Read More

    To confirm the problem, open a SSH console and enter the root mode as explained here.

    In that console, type: synoservice –is-all-up

    You should see that the apparmor service didn’t start properly after the reboot:

    root@YourNas:~# synoservice –is-all-up
    service [ “apparmor” ] failed on bootup.

     

    Here is the answer from Synology

    Good evening,

     

    thanks for contacting the Synology technical support and sorry for the late reply, we’re having an high volume of ticket.

    We’re aware of this issue, did you recently update your DSM?

    Anyway, I’m sorry but you will have to reinstall DSM to fix this issue, please take a full backup of your Data using “Hyper backup” or “Cloud Sync“.

    After that navigate to Control Panel -> Update and Restore -> Configuration Backup -> Backup Configuration-> Reset -> Erase all Data

    Reinstall DSM and restore Configuration and Data.
    Take in mind that the response time might be delayed a bit because of the high amount of Ticket that we received.

    Thanks for your understanding and for your patience.
    If you have any other question or doubt, don’t hesitate to contact me back.

    Best regards,
    xxxxxxxx

    Technical support FR

    Loading