DNS Change script help please

If there is a better way to do this I am all ears, err eyes. I am trying to change dns servers under /ip/dns depending based on if a ping response fails. I am new to mikrotik scripting and I tried this, I get the 5 responses from the target and it stays the same. If I get 5 fails the do line does not change the string to 4.2.2.2. Do i need to incorporate variables for the string value that i want to change to?

Any help with this would be greatly appreciated.

:if ([ping 5.5.5.5 count=5]=false) do=[ip dns set servers=4.2.2.2]

The ping command returns the number of successful pings. So, if you run this in the terminal, you can see the result. Total failure results in a returned value of 0 (zero).
:put [ping 5.5.5.5 count=5]So, your script should look like this:
:if ([ping 5.5.5.5 count=5]=0) do={ip dns set servers=4.2.2.2}

That is awesome it works. Now I want to switch the dns server on successful ping.

I tried this

:if ([ping 8.8.8.8 count=5]=1) do={ip dns set servers=8.8.8.8}

I was thinking =1 is a check on true. It did not work for me

What should I try next?

Disregard that, it must have hiccuped. Because it worked fine the next time I tried it. :slight_smile:

The ping command will return the number of successful pings, not a 0=false or 1=true. So, in your case, it will return the number 5 if all pings are successful, 0 if they all fail, or somewhere in between if you lose any pings.