Community discussions

MikroTik App
 
luberfly
just joined
Topic Author
Posts: 13
Joined: Fri Jun 02, 2017 12:31 am
Location: Italy

A problem with this script: enable/disable CAPsMAN

Thu Jun 24, 2021 10:34 am

Hi Friends,
I'm tryng to do a script for a faill over CAPsMAN.
I Have 3 possible CAPsMAN called 1, 2 and 3.
Ping CAPsMAN1 if it doesn't respond, swithc to CAPsMAN2 and so on.
I cheched the script on Terminal (Cut-Past) and it works.
I copyed it into script, run Script but do not run. No error appear.
I read the guide, but don't understand why,

I copy here the script.
Someone can help me?
I need to schedule this script every 30 seconds.

#Check CAPsMAN Priority
#CAPsMAN (1) - 192.168.1.10 - RB952_Living_Manager
#CAPsMAN (2) - 192.168.1.11 - RB952_Studio
#CAPsMAN (3) - 192.168.1.14 - RB952_Backup
:global vIPCAPsMAN1 192.168.1.10
:global vIPCAPsMAN2 192.168.1.11
:global vIPCAPsMAN3 192.168.1.14
:global NameCAPsMAN1 “RB952_Living”
:global NameCAPsMAN2 “RB952_Studio”
:global NameCAPsMAN3 “RB952_Backup”
:global NameID [/system identity get name]

#Se CAPsMAN1 risponde e mi trovo in uno degli altri CAPsMAN, allora spengo il CAPsMAN di dove mi trovo.
:log warning (" Run Script ")
:if ( [/ping $vIPCAPsMAN1 interval=1 count=10 size=28]>=5 ) do={
:if ($NameID=$NameCAPsMAN1) do={
:if [caps-man manager get enabled] do={:log warning ($NameID . " 1 - CAPsMAN Already ON")} else={/caps-man manager set enabled=yes
:log warning ($NameID . " 1 - CAPsMAN - ON")}
} else={
/caps-man manager set enabled=no
:log warning ("Attivato CAPsMAN IP: " . $vIPCAPsMAN1 . " – " . $NameCAPsMAN1)
:log warning ($NameID . " 2 - CAPsMAN - OFF")
}
} else={
#Se CAPsMAN2 risponde e mi trovo in uno degli altri CAPsMAN, allora spengo il CAPsMAN di dove mi trovo.
:if ( [/ping $vIPCAPsMAN2 interval=1 count=10 size=28]>=5 ) do={
:if ($NameID=$NameCAPsMAN2) do={
:if [caps-man manager get enabled] do={:log warning ($NameID . " 3 - CAPsMAN Already ON")} else={/caps-man manager set enabled=yes
:log warning ($NameID . " 3 - CAPsMAN - ON")}
} else={
/caps-man manager set enabled=no
:log warning ("Attivato CAPsMAN IP: " . $vIPCAPsMAN2 . " – " . $NameCAPsMAN2)
:log warning ($NameID . " 4 - CAPsMAN - OFF")
}
} else={
#Se CAPsMAN3 risponde e mi trovo in uno degli altri CAPsMAN, allora spengo il CAPsMAN di dove mi trovo.
:if ( [/ping $vIPCAPsMAN3 interval=1 count=10 size=28]>=5 ) do={
:if ($NameID=$NameCAPsMAN3) do={
:if [caps-man manager get enabled] do={:log warning ($NameID . " 5 - CAPsMAN Already ON")} else={/caps-man manager set enabled=yes
:log warning ($NameID . " 5 - CAPsMAN - ON")}
} else={
/caps-man manager set enabled=no
:log warning ("Attivato CAPsMAN IP: " . $vIPCAPsMAN3 . " – " . $NameCAPsMAN3)}
:log warning ($NameID . " 6 - CAPsMAN - OFF")
}
}
}


and this is the code...
/system scheduler
add interval=30s name=schedule1 on-event=SetCAPsManager policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
start-date=jun/23/2021 start-time=00:00:00
/system script
add dont-require-permissions=no name=SetCAPsManager owner=admin policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="#\
Check CAPsMAN Priority\r\
\n#CAPsMAN (1) - 192.168.1.10 - RB952_Living_Manager\r\
\n#CAPsMAN (2) - 192.168.1.11 - RB952_Studio\r\
\n#CAPsMAN (3) - 192.168.1.14 - RB952_Backup\r\
\n:global vIPCAPsMAN1 192.168.1.10\r\
\n:global vIPCAPsMAN2 192.168.1.11\r\
\n:global vIPCAPsMAN3 192.168.1.14\r\
\n:global NameCAPsMAN1 \93RB952_Living\94\r\
\n:global NameCAPsMAN2 \93RB952_Studio\94\r\
\n:global NameCAPsMAN3 \93RB952_Backup\94\r\
\n:global NameID [/system identity get name]\r\
\n\r\
\n#Se CAPsMAN1 risponde e mi trovo in uno degli altri CAPsMAN, allora spen\
go il CAPsMAN di dove mi trovo.\r\
\n:log warning (\" Run Script \")\r\
\n:if ( [/ping \$vIPCAPsMAN1 interval=1 count=10 size=28]>=5 ) do={\r\
\n\t:if (\$NameID=\$NameCAPsMAN1) do={\r\
\n\t:if [caps-man manager get enabled] do={:log warning (\$NameID . \" 1 -\
\_CAPsMAN Already ON\")} else={/caps-man manager set enabled=yes\r\
\n\t:log warning (\$NameID . \" 1 - CAPsMAN - ON\")}\r\
\n\t} else={\r\
\n\t/caps-man manager set enabled=no\r\
\n\t:log warning (\"Attivato CAPsMAN IP: \" . \$vIPCAPsMAN1 . \" \96 \" . \
\$NameCAPsMAN1)\r\
\n\t:log warning (\$NameID . \" 2 - CAPsMAN - OFF\")\r\
\n\t} \r\
\n} else={\r\
\n#Se CAPsMAN2 risponde e mi trovo in uno degli altri CAPsMAN, allora spen\
go il CAPsMAN di dove mi trovo.\r\
\n:if ( [/ping \$vIPCAPsMAN2 interval=1 count=10 size=28]>=5 ) do={\r\
\n\t:if (\$NameID=\$NameCAPsMAN2) do={\r\
\n\t:if [caps-man manager get enabled] do={:log warning (\$NameID . \" 3 -\
\_CAPsMAN Already ON\")} else={/caps-man manager set enabled=yes\r\
\n\t:log warning (\$NameID . \" 3 - CAPsMAN - ON\")}\r\
\n\t} else={\r\
\n\t/caps-man manager set enabled=no\r\
\n\t:log warning (\"Attivato CAPsMAN IP: \" . \$vIPCAPsMAN2 . \" \96 \" . \
\$NameCAPsMAN2)\r\
\n\t:log warning (\$NameID . \" 4 - CAPsMAN - OFF\")\r\
\n\t} \r\
\n} else={\r\
\n#Se CAPsMAN3 risponde e mi trovo in uno degli altri CAPsMAN, allora spen\
go il CAPsMAN di dove mi trovo.\r\
\n:if ( [/ping \$vIPCAPsMAN3 interval=1 count=10 size=28]>=5 ) do={\r\
\n\t:if (\$NameID=\$NameCAPsMAN3) do={\r\
\n\t:if [caps-man manager get enabled] do={:log warning (\$NameID . \" 5 -\
\_CAPsMAN Already ON\")} else={/caps-man manager set enabled=yes\r\
\n\t:log warning (\$NameID . \" 5 - CAPsMAN - ON\")}\r\
\n\t} else={\r\
\n\t/caps-man manager set enabled=no\r\
\n\t:log warning (\"Attivato CAPsMAN IP: \" . \$vIPCAPsMAN3 . \" \96 \" . \
\$NameCAPsMAN3)}\r\
\n\t:log warning (\$NameID . \" 6 - CAPsMAN - OFF\")\r\
\n}\r\
\n}\r\
\n}\r\
\n"
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: A problem with this script: enable/disable CAPsMAN

Thu Jun 24, 2021 3:14 pm

I cannot see anything directly wrong with your script.
But I guess you could use local instead of global variable if you do not need to use them in other scripts.

Do use code tag when posing code and use tab in script to make it more readable.
Here is a reformatted version.
#Check CAPsMAN Priority
#CAPsMAN (1) - 192.168.1.10 - RB952_Living_Manager
#CAPsMAN (2) - 192.168.1.11 - RB952_Studio
#CAPsMAN (3) - 192.168.1.14 - RB952_Backup
:global vIPCAPsMAN1 192.168.1.10
:global vIPCAPsMAN2 192.168.1.11
:global vIPCAPsMAN3 192.168.1.14
:global NameCAPsMAN1 “RB952_Living”
:global NameCAPsMAN2 “RB952_Studio”
:global NameCAPsMAN3 “RB952_Backup”
:global NameID [/system identity get name]

#Se CAPsMAN1 risponde e mi trovo in uno degli altri CAPsMAN, allora spengo il CAPsMAN di dove mi trovo.
:log warning (" Run Script ")
:if ( [/ping $vIPCAPsMAN1 interval=1 count=10 size=28]>=5 ) do={
	:if ($NameID=$NameCAPsMAN1) do={
		:if [caps-man manager get enabled] do={
			:log warning ($NameID . " 1 - CAPsMAN Already ON")
		} else={
			/caps-man manager set enabled=yes
			:log warning ($NameID . " 1 - CAPsMAN - ON")
		}
	} else={
	/caps-man manager set enabled=no
	:log warning ("Attivato CAPsMAN IP: " . $vIPCAPsMAN1 . " – " . $NameCAPsMAN1)
	:log warning ($NameID . " 2 - CAPsMAN - OFF")
	}
} else={
	#Se CAPsMAN2 risponde e mi trovo in uno degli altri CAPsMAN, allora spengo il CAPsMAN di dove mi trovo.
	:if ( [/ping $vIPCAPsMAN2 interval=1 count=10 size=28]>=5 ) do={
		:if ($NameID=$NameCAPsMAN2) do={
			:if [caps-man manager get enabled] do={
				:log warning ($NameID . " 3 - CAPsMAN Already ON")
			} else={
				/caps-man manager set enabled=yes
				:log warning ($NameID . " 3 - CAPsMAN - ON")
			}
		} else={
		/caps-man manager set enabled=no
		:log warning ("Attivato CAPsMAN IP: " . $vIPCAPsMAN2 . " – " . $NameCAPsMAN2)
		:log warning ($NameID . " 4 - CAPsMAN - OFF")
		}
	} else={
		#Se CAPsMAN3 risponde e mi trovo in uno degli altri CAPsMAN, allora spengo il CAPsMAN di dove mi trovo.
		:if ( [/ping $vIPCAPsMAN3 interval=1 count=10 size=28]>=5 ) do={
			:if ($NameID=$NameCAPsMAN3) do={
				:if [caps-man manager get enabled] do={
					:log warning ($NameID . " 5 - CAPsMAN Already ON")
				} else={
					/caps-man manager set enabled=yes
					:log warning ($NameID . " 5 - CAPsMAN - ON")
				}
			} else={
				/caps-man manager set enabled=no
				:log warning ("Attivato CAPsMAN IP: " . $vIPCAPsMAN3 . " – " . $NameCAPsMAN3)
			}
			:log warning ($NameID . " 6 - CAPsMAN - OFF")
		}
	}
}
Since the main body are 3 nearly equal part, you may run same code three times:
:foreach i in=[:toarray "1,2,3"] do={:put $i}
:if ($NameID=$NameCAPsMAN$i) do={
 
luberfly
just joined
Topic Author
Posts: 13
Joined: Fri Jun 02, 2017 12:31 am
Location: Italy

Re: A problem with this script: enable/disable CAPsMAN

Thu Jun 24, 2021 11:18 pm

Hi, many thanks!
I copied the code into a script then click apply and run script, but it do not start...
The open Terminal, past it and run...
I don't understand why don't run the script
Regards
Luca
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: A problem with this script: enable/disable CAPsMAN

Fri Jun 25, 2021 2:35 pm

The script is full of errors.

RouterOS non supporta , e neanche

per esempio:
:global NameCAPsMAN1 RB952_Living ( “ start cite, ” end cite)
deve essere
:global NameCAPsMAN1 "RB952_Living" ( " quote)

e ancora:
$vIPCAPsMAN1 . " " . $NameCAPsMAN1 ( – special separator character)
deve essere
$vIPCAPsMAN1 . " - " . $NameCAPsMAN1 ( - minus sign)

Non usare WORD per scrivere script, ma NOTEPAD piuttosto...

Non te lo correggo, non mi piace quell'approccio.

Use NETWATCH instead.
Last edited by rextended on Fri Jun 25, 2021 2:47 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: A problem with this script: enable/disable CAPsMAN

Fri Jun 25, 2021 2:47 pm

I cannot see anything directly wrong with your script.
nooo... :(
you didn't notice, I hope ...
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: A problem with this script: enable/disable CAPsMAN

Fri Jun 25, 2021 6:39 pm

It may be a copy/past using different writing tools that has changed the " and -
You are correct that this will totally break the script :)
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: A problem with this script: enable/disable CAPsMAN

Fri Jun 25, 2021 6:48 pm

One member of MikroTik staff, last week, suggest me to use "/sys scri print" for see errors on script ;)

On 15 year i never notice that is possible!!!

Very useful when you read \93 \94 and \96 evidenced... ;)))
 
luberfly
just joined
Topic Author
Posts: 13
Joined: Fri Jun 02, 2017 12:31 am
Location: Italy

Re: A problem with this script: enable/disable CAPsMAN

Sun Jun 27, 2021 5:54 pm

Many thanks for suggestions.
Now I'm using Notepad++ with language RouterOS_ULD Addon installed. I found some syntax errors that I have fixed.
Now it works!
Best Regards
Luca

Who is online

Users browsing this forum: No registered users and 21 guests