add name=LndkSmsPower policy=\ ftp,reboot,read,write,policy,test,password,sniff,sensitive source="#######\ ##########################################################################\ ######\ \n# Script RouterOS LndkSmsPower \ \_ #\ \n# Managing outlets of Aviosys PDU 9258S, 9258HP and 9258DS via SMS \ \_ #\ \n# Send state, switch or reinit outlet \ \_ #\ \n# Web page : http://blog.lekermeur.net/\?p=2531 \ \_ #\ \n# Marc Dilasser, Le Net du Kermeur, Fevrier 2015 \ \_ #\ \n########################################################################\ ###############\ \n\ \n:global DV\ \n:global PR\ \n:global OP\ \n:local ADRIP \"\"\ \n:local HPORT\ \n:local NETWORK \"LNDK\"\ \n:local DEVNAME \"\"\ \n:local MODEL \"\"\ \n:local PRISE -1\ \n:local NBJOB\ \n:local NBSMS\ \n:local VER\ \n:local NBOPER\ \n:local URL\ \n:local ERR\ \n:local OUTFILE \"LndkPowerState\"\ \n:local IDF\ \n:local CONTENT\ \n:local MSG\ \n:local HHMMSS\ \n:local IJ\ \n:local IK\ \n:local IL\ \n:local ISTAT\ \n:local IOSTATE\ \n:local ITEM\ \n:local PORT\ \n:local NUMTEL\ \n:local DEBUG 0\ \n:local ADMIN\ \n:local PASSWORD\ \n:local OPER\ \n:local AROPERS\ \n:local LANG\ \n:local ONOFF [:toarray (\"OFF,ON \")]\ \n:local HEXA [:toarray (\"0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f\")]\ \n:local ARLANGS [:toarray (\"en,fr\")]\ \n:local AAR {\ \n \"address\"={ en=\"address\"; fr=\"adresse\ \" };\ \n \"outlet\"={ en=\"outlet\"; fr=\"prise\" \ \_ };\ \n \"device\"={ en=\"device\"; fr=\"equipeme\ nt\"};\ \n \"model\"={ en=\"model\"; fr=\"modele\"\ \_ };\ \n \"state\"={ en=\" state of \"; fr=\" etat de\ \_\" };\ \n \"at\"={ en=\" at \"; fr=\" a \" \ \_ };\ \n \"err200\"={ en=\"sending sms error\"; fr=\"erreur e\ nvoi SMS\" };\ \n \"err220\"={ en=\"http error\"; fr=\"erreur a\ cces http\" };\ \n \"err230\"={ en=\"Url non defined\"; fr=\"Url non \ definie\" };\ \n \"err240\"={ en=\"script already running\"; fr=\"script d\ eja en cours\" };\ \n }\ \n\ \n:global ChangeToMajOrMinCase do={\ \n :local S1\ \n :local S2 \"\"\ \n :local CH \"\"\ \n :local I1\ \n :local I2\ \n :local I3\ \n :local MINMAJ {\ \n \"min\"=\"abcdefghijklmnopqrstuvwxy\ z\";\ \n \"maj\"=\"ABCDEFGHIJKLMNOPQRSTUVWXY\ Z\";\ \n }\ \n :local SOURCE \"min\"\ \n :local TARGET \"maj\"\ \n :set S1 \$1\ \n :set I2 [:len \$S1]\ \n :if (\$2 = 0) do={\ \n :set SOURCE \"maj\"; set TARGET \"min\";\ \n }\ \n :if (\$I2 > 0) do={\ \n for I1 from=0 to=(\$I2 - 1) do={\ \n :set CH [:pick \$S1 \$I1 (\$I1 + 1)]\ \n :set I3 [:find (\$MINMAJ->\$SOURCE) \$CH]\ \n :if (\$I3 >= 0) do={\ \n :set CH [:pick (\$MINMAJ->\$TARGET) \$I3 (\ \$I3 + 1)] \ \n } \ \n :set S2 (\$S2 . \$CH)\ \n }\ \n }\ \n :return \$S2\ \n }\ \n:local ToUpper do={\ \n :global ChangeToMajOrMinCase\ \n :return [\$ChangeToMajOrMinCase \$1 1]\ \n }\ \n:local ToLower do={\ \n :global ChangeToMajOrMinCase\ \n :return [\$ChangeToMajOrMinCase \$1 0]\ \n }\ \n# if (\$VER >= 6.2)\ \n:local DoHttp do={\ \n :local LERR 0\ \n :do {\ \n /tool fetch url=\"\$1\" dst-path=\"\$2\"\ \n } on-error={ \ \n :set LERR 1 \ \n }\ \n :return \$LERR\ \n }\ \n:local DoSendSms do={\ \n :local LERR 0\ \n :local MESS\ \n :set MESS [:pick \$3 0 158]\ \n :do {\ \n /tool sms send \"\$1\" \"\$2\" message=\"\$MESS\ \"\ \n } on-error={ \ \n :set LERR 1 \ \n }\ \n :return \$LERR\ \n }\ \n#endif\ \n########################################################################\ ###############\ \n# Valeurs a configurer suivant exploitation : \ \_ #\ \n# - user et mot de passe Aviosys \ \_ #\ \n# - adresse ip \ \_ #\ \n# - nom de device \ \_ #\ \n# - modele \ \_ #\ \n# - numero telephone destinataire du SMS, par defaut allowed-number d\ e /tool sms #\ \n# - le device par defaut \ \_ #\ \n# La variable DV du SMS, si presente, donne le device \ \_ #\ \n# La variable PR du SMS, si presente, donne la prise a basculer \ \_ #\ \n# La variable OP du SMS, si presente, donne le type d'operation \ #\ \n########################################################################\ ###############\ \n:if ([:len \$DV] = 0) do={:set DV \"pwr001\"; }\ \n\ \n:set ADMIN \"admin\"\ \n:set PASSWORD \"password\"\ \n\ \n# Default lang is en\ \n# :set LANG \"fr\"\ \n\ \n:if (\$DV = \"pwr001\") do={\ \n :set ADRIP \"10.6.7.8\"\ \n :set DEVNAME \"pwr001\"\ \n :set MODEL \"9258S\"\ \n}\ \n\ \n:if (\$DV = \"pwr009\") do={\ \n :set ADRIP \"10.6.2.9\"\ \n :set DEVNAME \"pwr009\"\ \n :set MODEL \"9258DSv1\"\ \n :set PASSWORD \"motdepasse\"\ \n}\ \n\ \n:if (\$DV = \"pwr028\") do={\ \n :set ADRIP \"192.168.1.100\"\ \n :set DEVNAME \"pwr028\"\ \n :set MODEL \"9258DSv4\"\ \n :set DEBUG 1\ \n}\ \n\ \n########################################################################\ ###############\ \n# END OF CONFIG \ \_ #\ \n########################################################################\ ###############\ \n\ \n########################################################################\ ###############\ \n# CGI commands examples on different Aviosys models and responses \ \_ #\ \n# 9258S : \ \_ #\ \n# > /tool fetch url=\"http://admin:password@192.168.1.100/Set.cmd\\\?CM\ D=GetPower\" #\ \n# < p61=0,p62=1,p63=0,p64=0 \ \_ #\ \n# 9258DSv1 : (firmware version 1.x) \ \_ #\ \n# > wget -qO- http://192.168.1.100/Set.cmd\?user=admin+pass=password+CM\ D=GetSysIO #\ \n# < IOVALUE:fb \ \_ #\ \n# 9258DSv4 : (firmware version 4.x) \ \_ #\ \n# > wget -qO- http://192.168.8.100/set.cmd\?user=admin+pass=password+cm\ d=GetPower #\ \n# < P61=0,P62=1,P63=1,P64=0,P65=1,P66=1,P67=1,P68=1 \ \_ #\ \n# 9258HP : \ \_ #\ \n# > wget -qO- http://admin:password@192.168.1.100/SetPower.cgi\?p4=1 \ \_ #\ \n# < Set Power ControlPower Control = P4:1,P3:0,P2:\ 1,P1:1;

... #\ \n########################################################################\ ###############\ \n:set NBJOB [/system script job print count-only where script=LndkSmsPowe\ r]\ \n:if (\$NBJOB > 1) do={\ \n :log error (\$AAR->\"err240\"->\$LANG)\ \n return 240\ \n}\ \n\ \n########################################################################\ ###############\ \n# List of operations \ \_ #\ \n# Default operation is ST, only send STATE \ \_ #\ \n# If PR (outlet) is set, defaut OP is SW, to switch this outlet \ \_ #\ \n# If OP is RE (reinit) and PR is set, operations are STATE,SWITCH,DELAY,\ SWITCH,STATE #\ \n# If OP is LI (list names of outlets), operation is LIST (not implemente\ d yet) #\ \n########################################################################\ ###############\ \n\ \n:if (([:len [:find \$ARLANGS \$LANG]]) = 0) do={ :set LANG \"en\"; }\ \n\ \n:if ([:len \$OP] = 0) do={\ \n :set OP \"ST\";\ \n :if ([:len \$PR] = 1) do={ :set OP \"SW\"; }\ \n} else={\ \n :set OP [\$ToUpper \$OP]\ \n}\ \n\ \n:set AROPERS [:toarray (\"STATE\")]\ \n:if (\$OP = \"SW\") do={\ \n :if ([:len \$PR] = 1) do={\ \n :set AROPERS [:toarray (\"STATE,SWITCH,STATE\")]\ \n }\ \n}\ \n:if (\$OP = \"RE\") do={\ \n :if ([:len \$PR] = 1) do={\ \n :set AROPERS [:toarray (\"STATE,SWITCH,STATE,DELAY20,SWITCH,STATE\")]\ \n } \ \n}\ \n:if (\$OP = \"LI\") do={\ \n :set AROPERS [:toarray (\"LIST\")]\ \n}\ \n:if ([:len \$PR] > 0) do={ \ \n :set PRISE ([:tonum \$PR] - 1); \ \n} \ \n:if ([:len \$OP] = 0) do={ :set OP \"\"; }\ \n:set VER [/system resource get version]\ \n:set MSG ((\$AAR->\"device\"->\$LANG) . \" \" . \$DEVNAME . \", \")\ \n:set MSG (\$MSG . (\$AAR->\"model\"->\$LANG) . \" \" . \$MODEL . \", \")\ \n:set MSG (\$MSG . (\$AAR->\"address\"->\$LANG) . \" \" . \$ADRIP)\ \n:log info \$MSG\ \n\ \n########################################################################\ ###############\ \n# Do while some operation to do \ \_ #\ \n########################################################################\ ###############\ \nforeach OPER in (\$AROPERS) do={\ \n :if (\$OPER = \"STATE\") do={\ \n :set URL \"\" \ \n :if (\$MODEL = \"9258S\") do={\ \n :set URL (\"http://\" . \$ADMIN . \":\" . \$PASSWORD . \"@\" . \ \$ADRIP )\ \n :if ([:len \$HPORT] > 0) do={ :set URL (\$URL . \":\" . \$HPORT\ ); }\ \n :set URL (\$URL . \"/Set.cmd\\\?CMD=GetPower\")\ \n }\ \n :if (\$MODEL = \"9258DSv1\") do={\ \n :set URL (\"http://\" . \$ADRIP);\ \n :if ([:len \$HPORT] > 0) do={ :set URL (\$URL . \":\" . \$HPORT\ ); }\ \n :set URL (\$URL . \"/Set.cmd\\\?user=\" . \$ADMIN )\ \n :set URL (\$URL . \"+pass=\" . \$PASSWORD . \"+CMD=GetSysIO\")\ \n }\ \n :if (\$MODEL = \"9258DSv4\") do={\ \n :set URL (\"http://\" . \$ADRIP)\ \n :if ([:len \$HPORT] > 0) do={ :set URL (\$URL . \":\" . \$HPORT\ ); }\ \n :set URL (\$URL . \"/set.cmd\\\?user=\" . \$ADMIN )\ \n :set URL (\$URL . \"+pass=\" . \$PASSWORD . \"+cmd=GetPower\")\ \n }\ \n :if (\$MODEL = \"9258HP\") do={\ \n :set URL (\"http://\" . \$ADRIP)\ \n :if ([:len \$HPORT] > 0) do={ :set URL (\$URL . \":\" . \$HPORT\ ); }\ \n :set URL (\$URL . \"/GetPower.cgi\")\ \n }\ \n :if ([:len \$URL] = 0) do={\ \n :return 250\ \n } else={ \ \n :if (\$DEBUG > 0) do={ :put \"Url : \$URL\"; }\ \n }\ \n# if (\$VER >= 6.2)\ \n :set ERR [\$DoHttp \$URL \$OUTFILE]\ \n# else\ \n# :set ERR 0\ \n# /tool fetch url=\$URL dst-path=\$OUTFILE\ \n# endif\ \n :if (\$ERR < 1) do={\ \n :set IDF [/file find where name=\$OUTFILE]\ \n :set CONTENT [ /file get \$IDF contents]\ \n :if (\$DEBUG > 0) do={ :put \$CONTENT; }\ \n :set HHMMSS [/system clock get time]\ \n :set MSG (\$NETWORK . \" :\" . (\$AAR->\"state\"->\$LANG))\ \n :set MSG (\$MSG . \$DEVNAME . (\$AAR->\"at\"->\$LANG) . \$HHMMS\ S . \"\\n\")\ \n\ \n :if (\$MODEL = \"9258DSv1\") do={\ \n :set ISTAT [:tonum (\"0x\" . [:pick \$CONTENT 9 11])]\ \n :set IOSTATE \$ISTAT\ \n :set IJ 0\ \n while (\$IJ < 8) do={\ \n :set IK (\$ISTAT % 2)\ \n :set MSG (\$MSG . (\$AAR->\"outlet\"->\$LANG) . \" \" . [\ :tostr (\$IJ + 1)] )\ \n :set MSG (\$MSG . \": \" . [:pick \$ONOFF \$IK])\ \n :if ((\$IJ % 2) = 1) do={ \ \n :set MSG (\$MSG . \"\\n\"); \ \n } else={ \ \n :set MSG (\$MSG . \", \"); \ \n }\ \n :set ISTAT (\$ISTAT / 2)\ \n :set IJ (\$IJ + 1)\ \n }\ \n }\ \n :if ((\$MODEL = \"9258S\") || (\$MODEL = \"9258DSv4\")) do={\ \n :if (\$MODEL = \"9258S\") do={ \ \n :set IJ 10; :set IK 4;\ \n } else={ \ \n :set IJ 5; :set IK 8;\ \n } \ \n :if (([:pick \$CONTENT 0 6] = \"\") || ([:pick \$CONTE\ NT 0 5] = \" P61=\")) do={\ \n :for IL from=1 to=\$IK do={\ \n :set MSG (\$MSG . \" \" . (\$AAR->\"outlet\"->\$LANG\ ) . \" \" . ([:tostr \$IL]) . \": \")\ \n :set MSG (\$MSG . ([:pick \$ONOFF [:pick \$CONTENT (\$\ IJ + ((\$IL - 1) * 6)) ]]))\ \n :if (((\$IL % 2) = 0) && (\$IL < \$IK)) do={\ \n :set MSG (\$MSG . \"\\n\")\ \n }\ \n }\ \n :if ( \$PRISE > -1 ) do={\ \n :set IOSTATE [:pick \$CONTENT (\$IJ + (6 * \$PRISE))]\ \n :log info ((\$AAR->\"outlet\"->\$LANG) . \" \" . (\$PR\ ISE + 1) . \", iostate : \$IOSTATE\")\ \n }\ \n }\ \n }\ \n :if (\$MODEL = \"9258HP\") do={\ \n :set IJ [:find \$CONTENT \"Control = P4\"]\ \n :if ([:len \$IJ] > 0) do={\ \n :set IJ (\$IJ + 13)\ \n :set MSG (\$MSG . \" \" . (\$AAR->\"outlet\"->\$LANG) .\ \_\" 1: \")\ \n :set MSG (\$MSG . ([:pick \$ONOFF [:pick \$CONTENT (\$IJ \ + 15) ]]))\ \n :set MSG (\$MSG . \", \" . (\$AAR->\"outlet\"->\$LANG) . \ \" 2: \")\ \n :set MSG (\$MSG . ([:pick \$ONOFF [:pick \$CONTENT (\$IJ \ + 10) ]]) . \"\\n\")\ \n :set MSG (\$MSG . \" \" . (\$AAR->\"outlet\"->\$LANG) .\ \_\" 3: \")\ \n :set MSG (\$MSG . ([:pick \$ONOFF [:pick \$CONTENT (\$IJ \ + 5) ]]))\ \n :set MSG (\$MSG . \", \" . (\$AAR->\"outlet\"->\$LANG) . \ \" 4: \")\ \n :set MSG (\$MSG . ([:pick \$ONOFF [:pick \$CONTENT \$IJ ]\ ]) . \"\\n\")\ \n :if ( \$PRISE > -1 ) do={\ \n :set IOSTATE [:pick \$CONTENT (\$IJ + (5 * \$PRISE))]\ \n :log info ((\$AAR->\"outlet\"->\$LANG) . \" \" . (\$PR\ ISE + 1) . \", iostate : \$IOSTATE\")\ \n }\ \n }\ \n }\ \n :if ([:len \$MSG] > 60) do={\ \n :if (\$DEBUG = 0) do={\ \n :set PORT [/tool sms get port]\ \n :if ([:len \$NUMTEL] = 0) do={\ \n :set NUMTEL [/tool sms get allowed-number]\ \n }\ \n :if (!([/tool sms get receive-enabled] ~ \"true\")) do={\ \n :set MSG (\$MSG . \"** SMS inactif **\")\ \n }\ \n :log info (\$MSG . \"(taille:\" . [:len (\$MSG)] . \")\")\ \n# if (\$VER >= 6.2)\ \n :set ERR [\$DoSendSms \$PORT \$NUMTEL \$MSG]\ \n :if (\$ERR > 0) do={\ \n :log error (\$AAR->\"err200\"->\$LANG) \ \n return 200\ \n }\ \n# else\ \n# /tool sms send ...\ \n# endif\ \n } else={\ \n :put (\$MSG)\ \n :log info (\$MSG)\ \n }\ \n }\ \n /file remove \$IDF\ \n } else={\ \n :log error (\$AAR->\"err220\"->\$LANG) \ \n return 220; \ \n }\ \n }\ \n\ \n :if (\$OPER = \"SWITCH\") do={\ \n :if (([:len \$PRISE] = 1) && ([:len \$IOSTATE] > 0)) do={\ \n :set URL \"\" \ \n :if (\$MODEL = \"9258S\") do={\ \n :set URL (\"http://\" . \$ADMIN . \":\" . \$PASSWORD . \"@\"\ )\ \n :set URL (\$URL . \$ADRIP)\ \n :if ([:len \$HPORT] > 0) do={ :set URL (\$URL . \":\" . \$HP\ ORT); }\ \n :set URL (\$URL . \"/Set.cmd\\\?CMD=SetPower\")\ \n :set URL (\$URL . \"+P6\" . [:tostr (\$PRISE + 1)] . \"=\")\ \n :set URL (\$URL . [:tostr ((\$IOSTATE + 1) % 2)])\ \n }\ \n :if (\$MODEL = \"9258DSv4\") do={\ \n :set URL (\"http://\" . \$ADMIN . \":\" . \$PASSWORD . \"@\"\ )\ \n :set URL (\$URL . \$ADRIP)\ \n :if ([:len \$HPORT] > 0) do={ :set URL (\$URL . \":\" . \$HP\ ORT); }\ \n :set URL (\$URL . \"/set.cmd\\\?cmd=setpower\")\ \n :set URL (\$URL . \"+p6\" . [:tostr (\$PRISE + 1)] . \"=\")\ \n :set URL (\$URL . [:tostr ((\$IOSTATE + 1) % 2)])\ \n }\ \n :if (\$MODEL = \"9258DSv1\") do={\ \n :set URL (\"http://\" . \$ADRIP)\ \n :if ([:len \$HPORT] > 0) do={ :set URL (\$URL . \":\" . \$HP\ ORT); }\ \n :set URL (\$URL . \"/Set.cmd\\\?user=\" . \$ADMIN )\ \n :set URL (\$URL . \"+pass=\" . \$PASSWORD . \"+CMD=SetSysIO+\ IO=\")\ \n :set IJ 8\ \n :set IK 0\ \n while (\$IJ > 0) do={\ \n :set IK (\$IK * 2)\ \n :if (\$IJ = (\$PRISE + 1)) do={ :set IK (\$IK + 1); }\ \n :set IJ (\$IJ - 1)\ \n }\ \n :set IJ ( \$IOSTATE ^ \$IK)\ \n :set URL (\$URL . [:pick \$HEXA (\$IJ / 16)])\ \n :set URL (\$URL . [:pick \$HEXA (\$IJ % 16)])\ \n :set URL (\$URL . \"+Delay=1\")\ \n }\ \n :if (\$MODEL = \"9258HP\") do={\ \n :set URL (\"http://\" . \$ADMIN . \":\" . \$PASSWORD . \"@\"\ )\ \n :set URL (\$URL . \$ADRIP)\ \n :if ([:len \$HPORT] > 0) do={ :set URL (\$URL . \":\" . \$HP\ ORT); }\ \n :set URL (\$URL . \"/SetPower.cgi\\\?p\")\ \n :set URL (\$URL . [:tostr (\$PRISE + 1)] . \"=\")\ \n :set URL (\$URL . [:tostr ((\$IOSTATE + 1) % 2)])\ \n }\ \n :if ([:len \$URL] = 0) do={\ \n :log error (\$AAR->\"err230\"->\$LANG)\ \n :return 230\ \n } \ \n :if (\$DEBUG > 0) do={\ \n :put \"Url : \$URL\"\ \n :log info \"Url : \$URL\"\ \n } else={\ \n# if (\$VER >= 6.2)\ \n :set ERR [\$DoHttp \$URL \$OUTFILE]\ \n# else\ \n# :set ERR 0\ \n# /tool fetch url=\$URL dst-path=\$OUTFILE\ \n# endif\ \n :if (\$ERR < 1) do={\ \n :set IDF [/file find where name=\$OUTFILE]\ \n /file remove \$IDF\ \n }\ \n } \ \n } else={\ \n :log error (\$AAR->\"err220\"->\$LANG)\ \n :return 220\ \n }\ \n }\ \n\ \n :if (\$OPER = \"DELAY20\") do={\ \n :delay 20\ \n }\ \n\ \n :if (\$OPER = \"LIST\") do={\ \n :delay 1\ \n }\ \n}\ \n\ \n########################################################################\ ###############\ \n# Some cleanings \ \_ #\ \n########################################################################\ ###############\ \n:foreach ITEM in (\"DV\", \"OP\", \"PR\") do={\ \n /system script environment remove [find where name=\$ITEM]\ \n}\ \n\ \n:set NBSMS [/tool sms inbox print count-only]\ \n:if (\$NBSMS >= 30) do={\ \n /tool sms inbox remove 0,1,2,3,4,5,6,7,8,9\ \n}\ \n############# End of LndkSmsPower ############# That's All, Folks \ \_ #############"