Script - check configuration before apply it again

Hi,

I’m using batch commands to send a script to all my routers via SSH (with Plink and PSCP). Plink executes a script inside script.something file.

I noticed that /system backup save name=$filename; and /export file=$rsc; overwrite the files already existing and create updated files every time I run the batch file (perfect!).

Now, I need to append to this script some other funcionalities like enable logging, change default IP services, create administrators users and remove default admin user, set ntp client, etc.

The question is:

  • How do I can check if the configuration already exists in the Mikrotik and skip to the next command? (to avoid duplicating lines, like it happens when I run the logging script)

Thank you!

:smiley:

For example - check with if there exists user with name “adm
:if ([:len [user find name~“adm”]]>0) do={:put “exists”}

Or for those setting where you are not allowed to add same records - catch run-time errors
:do {user add group=full name=admin} on-error={:put “can’t add user admin”}

Thank you for your reply.

It seems the following script has worked to search logging rules and IF no rules THEN add them to the system: :smiley:

:global name;

:set name [/system identity get name];

system logging action set remote remote=ip.ip.ip.ip remote-port=514

:if ([:len [system logging find topics~"warning" prefix~"$name" action~"remote"]]<1) do={:put [system logging add topics=warning prefix=$name action=remote]}

:if ([:len [system logging find topics~"error" prefix~"$name" action~"remote"]]<1) do={:put [system logging add topics=error prefix=$name action=remote]}

:if ([:len [system logging find topics~"critical" prefix~"$name" action~"remote"]]<1) do={:put [system logging add topics=critical prefix=$name action=remote]}

:if ([:len [system logging find topics~"info" topics~"interface" prefix~"$name" action~"remote"]]<1) do={:put [system logging add topics=info,interface prefix=$name action=remote]}

:if ([:len [system logging find topics~"account" topics~"!pppoe" prefix~"$name" action~"remote"]]<1) do={:put [system logging add topics=account,!pppoe prefix=$name action=remote]}

Thanks