script array for ip+arp+interface

Hello.
I get data from mysql in format:

192.168.193.251 00:0C:42:89:DC:FD vlan23
192.168.195.240 00:0E:2E:A7:F6:CF vlan25
192.168.188.251 00:15:77:2E:B6:40 vlan18
192.168.191.252 00:15:77:2E:C4:40 vlan21

I’m using that one script for updating static entries at ccr:


if [ -e netdevices_murka_1 ]
then
for x in $(cat netdevices_murka_1 | awk ‘{ print $1 }’)
do
ssh admin@1.1.1.1 “/ip arp remove [ /ip arp find where address=$x ];”
done
a=0
b=0
c=0
for item1 in $(cat netdevices_murka_1 | awk ‘{ print $1 }’)
do
a=$a+1
array1[$a]=$item1
done
for item2 in $(cat netdevices_murka_1 | awk ‘{ print $2 }’)
do
b=$b+1
array2[$b]=$item2
done
for item3 in $(cat netdevices_murka_1 | awk ‘{ print $3 }’)
do
c=$c+1
array3[$c]=$item3
done

else
echo “nie ma pliku”
exit 0
fi

x=1
z=cat netdevices_murka_1 | wc -l
while [ $x -le $z ]; do
#echo ${array1[$x]} ${array2[$x]} ${array3[$x]}
ssh admin@1.1.1.1 “/ip arp add address=${array1[$x]} mac-address=${array2[$x]} interface=${array3[$x]}”
x=$[x+1]
done

I’m looking for a microtic script, which will parse txt file with ip,mac,interface and will set up it on board. For this momment ssh remote commands are too slow for me ;-(.

I cant find usefull microtic scripts examples with arrays ;-(.
Best regards.

I would not worry about using arrays and use bash to create a properly formatted importable .rsc script. The bash script could look something like this:

# create MikroTik script file
echo "/ip arp" > import.rsc

if [ -e netdevices_murka_1 ]
then
  a=0
  b=0
  c=0
  for item1 in $(cat netdevices_murka_1 | awk '{ print $1 }')
  do
    a=$a+1
    array1[$a]=$item1
  done
  for item2 in $(cat netdevices_murka_1 | awk '{ print $2 }')
  do
    b=$b+1
    array2[$b]=$item2
  done
  for item3 in $(cat netdevices_murka_1 | awk '{ print $3 }')
  do
    c=$c+1
    array3[$c]=$item3
  done
else
  echo "nie ma pliku"
  exit 0
fi

# add data to the rsc script
x=1
z=`cat netdevices_murka_1 | wc -l`
while [ $x -le $z ]; do
  echo "remove [ find where address=${array1[$x]} ]" >> import.rsc
  echo "add address=${array1[$x]} mac-address=${array2[$x]} interface=${array3[$x]}" >> import.rsc
  x=$[x+1]
done

# upload rsc file to router
ftp -n 1.1.1.1 <<END_OF_SESSION
user admin password
pass
put import.rsc
bye
END_OF_SESSION

# delete local file
rm import.rsc

# import rsc file on router, and then remove
ssh admin@1.1.1.1 "/import import.rsc"
ssh admin@1.1.1.1 "/file remove import.rsc"
# might be able to run them on one line? Not tested...
# ssh admin@1.1.1.1 "/import import.rsc;/file remove import.rsc"

Running the bash script creates import.rsc file from your mysql data:
/ip arp
remove [ find where address=192.168.193.251 ]
add address=192.168.193.251 mac-address=00:0C:42:89:DC:FD interface=vlan23
remove [ find where address=192.168.195.240 ]
add address=192.168.195.240 mac-address=00:0E:2E:A7:F6:CF interface=vlan25
remove [ find where address=192.168.188.251 ]
add address=192.168.188.251 mac-address=00:15:77:2E:B6:40 interface=vlan18
remove [ find where address=192.168.191.252 ]
add address=192.168.191.252 mac-address=00:15:77:2E:C4:40 interface=vlan21This file is uploaded to the MikroTik router via FTP and runs via SSH and is then deleted.

This is it, thank you ! :wink:.
Regards.