export list of MikroTik only devices from /ip neighbours

Hi all,
I have script on linux server which is executing backup on mk routers and downloading thems:
(Script is done because I found lot of very useful informations here, in this forum, thx for help.)

LOGIN=admin
PASSWORD=password
SSHPORT=222
FTPPORT=211
IPLISTFILE=ips.list
SUBDIR=mkbackup
LOGFILE=mkbackup.log

index=0
while read line ; do
IPLIST[$index]="$line"
index=$(($index+1))
done < $IPLISTFILE
echo ${iplist[@]}

for (( i=0; i<${#IPLIST[@]}; i++ ));
do
HOSTIP=${IPLIST[$i]}
echo  >>$LOGFILE
echo $HOSTIP `date +%Y%m%d%H%M%S` >>$LOGFILE
sshpass -p $PASSWORD ssh $LOGIN@$HOSTIP -p $SSHPORT -o StrictHostKeyChecking=no ':global zalohafile ( [/ system identity get name] . "-" . [:pick [/ sys clock get date] 7 11] . [:pick [/ sys clock get date] 0 3] . [:pick [/ sys clock get date] 4 6] . "-" . [:pick [/ sys clock get time] 0 2] . [:pick [/ sys clock get time] 3 5] . [:pick [/ sys clock get time] 6 8]) ; / system backup save name=$zalohafile ; / export file=$zalohafile' >>$LOGFILE  2>>$LOGFILE
wget -N -nv -P $SUBDIR/$HOSTIP ftp://$HOSTIP:$FTPPORT/* --ftp-user=$LOGIN --ftp-password=$PASSWORD >>$LOGFILE  2>>$LOGFILE
done

Script read list of ip address from file.
I put addresses to file manualy, but now I want make it by some script. I need to export ip addresses of items with “Platform=MikroTik” from [/ip neighbors] to some file.

# I can print only mikrotik devices:
:put [/ ip neighbour print where platform="MikroTik"]
# But with print I cannot store them into variable/array.
# I try to do it with "get" but without succes.
# Somehow extract item numbers of devices with platform=mikrotik
# / ip neighbor print where platform="MikroTik" get address ---> something like this but working and storing numbers to array
# Add each one to array:
:set bbb (:put $bbb , $nmbr) ; :put $bbb 
# And then extract ip address using item numbers
:put [/ip neighbor get number=$i address]

Pls could someone help me? Thanks

Try this:

:global MTdevices [/ip neighbor find platform=MikroTik];
:foreach i in=$MTdevices do={
    :put [/ip neighbor get $i address];
}

my suggestion would be to go away from scripting and use API, to retrieve data, then filter out what you need. With version 4.x and newer you also can look for values and get only those, using proplist, you can limit returned fields to bare minimum you need.

As i understood, you already use some host to execute what you have, so it is better to use API as we try to change existing features as little as possible an only new things are added to expand capabilities.

Great, it works good. Thank you.

Looks good, but:

  • lot of rbs in network is older version (2.9, 3.3, …)
  • Im doing it “on the fly”, have problem with scripting, dont have enough time to learning api right now :frowning:
  • api wants some advanced language (cpp, python), script is just easy bash script executed via ssh, 20s to prepare script .)
  • bash script is easily executed a scheduled natively in linux server

Mayby sometime in future.

Now I have last problem, generated backup files remaining on rbs disk and making them full. I must erase them with script but carefuly to leave there last one.

This will be some kind of central linux backup tool for routeros .)

http://wiki.mikrotik.com/wiki/Automatic_Centralized_Backup_from_Linux
Done, but page with code is … wide.
And writen with engrish.