Search code examples
phpmikrotik

(Mikrotik Script). How to set coding "/print" only column we want?


We need some information from my Router that my devices already active. Sometime we have to ping (macaddress's device) not ip, beacause it's Dynamic.

So we create file like as export then compare them. Now we got some trouble like these when we want print file only specific column

/print ip arp .......

any body can help these or maybe any method ?

/print ip arp file=someip.txt 

/print ip arp (only column we want) file=someip.txt

Solution

  • First of all maybe you can set an static IP to the thing you want. If you are using DHCP you can add static leases with /ip dhcp-server lease add .... And you don't need to script anything.

    Mikrotik also has an API (https://wiki.mikrotik.com/wiki/Manual:API) there are binding for some languages. You can search for Mikrotik API .

    Finally a dirty hack which is easy and may be enough for some scenarios is to SSH the router and the filter the output using your shell (if you are using a unix like environment)

    ssh YOUR-DEVICE /ip arp print where mac-address=11:22:33:44:55:66 \
                    | grep 11:22:33:44:55:66 | awk '{ print $3 }'
    

    This will ssh into the device and get the IP Address (column 3) of the device with the mac 11:22:33:44:55:66.

    The first line of the command is executed on the Mikrotik, the pipes with the grep and awk in your unix enviroment with all the power of your shell.

    If you copy your SSH key to the device the command won't require password.