final edit fixes

This commit is contained in:
Wifinigel 2023-02-19 13:53:15 +00:00
parent b10b99bd87
commit f3beb2ed9b
41 changed files with 164 additions and 150 deletions

View file

@ -1,4 +1,4 @@
:global Filename "ch10-04-bad-script.rsc" :local Filename "ch10-04-bad-script.rsc"
# A simple script to perform a series of tests on a # A simple script to perform a series of tests on a
# list of web sites. # list of web sites.
@ -17,9 +17,10 @@
:global LogMessageFunc; :global LogMessageFunc;
# check arg type str of correct length passed # check arg type str of correct length passed
if (([:typeof $WebSiteName]!="str") or ([:len value=$WebSiteName] < 6)) do={ if (([:typeof $WebSiteName]!="str") or \
$LogMessageFunc ("DnsResolveFunc: arg value $WebSiteName not a valid \ ([:len value=$WebSiteName] < 6)) do={
string!"); $LogMessageFunc ("DnsResolveFunc: arg value $WebSiteName not \
a valid string!");
} }
return [/resolve $WebSiteName]; return [/resolve $WebSiteName];
@ -32,8 +33,8 @@
# check arg type ip is passed # check arg type ip is passed
if ([:typeof $IpAddress] != "ip") do={ if ([:typeof $IpAddress] != "ip") do={
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not an IP \ $LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not \
address!"); an IP address!");
} }
return [/ping $IpAddress count=3]; return [/ping $IpAddress count=3];
@ -45,13 +46,14 @@
:global LogMessageFunc; :global LogMessageFunc;
# check arg type str of correct length passed # check arg type str of correct length passed
if (([:typeof $SiteName] != "str") or ([:len value=$SiteName] < 6)) do={ if (([:typeof $SiteName] != "str") or \
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a valid \ ([:len value=$SiteName] < 6)) do={
string!"); $LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a \
valid string!");
} }
return [/tool fetch url=("https://$SiteName") mode=https http-method=get \ return [/tool fetch url=("https://$SiteName") mode=https \
as-value keep-result=no]; http-method=get as-value keep-result=no];
} }
########## ##########
@ -83,7 +85,6 @@
} }
# cleanup global namespace # cleanup global namespace
:set Filename;
:set LogMessageFunc; :set LogMessageFunc;
:set DnsResolveFunc; :set DnsResolveFunc;
:set PingIpAddressFunc; :set PingIpAddressFunc;

View file

@ -4,7 +4,7 @@
:do { :do {
:local SiteIpAddress [/resolve $SiteName]; :local SiteIpAddress [/resolve $SiteName];
:put "Site IP for $SiteName is $SiteIpAddress"; :put "Site IP for $SiteName is $SiteIpAddress";
} on-error { } on-error={
:put "Name lookup failed for $SiteName" :put "Name lookup failed for $SiteName"
} }
} }

View file

@ -1,4 +1,4 @@
:global Filename "ch10-07-bad-script.rsc" :local Filename "ch10-07-bad-script.rsc"
# A script to perform a series of tests on a # A script to perform a series of tests on a
# list of web sites. # list of web sites.
@ -19,8 +19,8 @@
# check arg type str of correct length passed # check arg type str of correct length passed
:if (([:typeof $WebSiteName] != "str") or \ :if (([:typeof $WebSiteName] != "str") or \
([:len value=$WebSiteName] < 6)) do={ ([:len value=$WebSiteName] < 6)) do={
$LogMessageFunc ("DnsResolveFunc: arg value $WebSiteName not a valid \ $LogMessageFunc ("DnsResolveFunc: arg value $WebSiteName not \
string!"); a valid string!");
:return "*** test failed ***"; :return "*** test failed ***";
} }
@ -29,8 +29,8 @@
:do { :do {
return [/resolve $WebSiteName]; return [/resolve $WebSiteName];
} on-error={ } on-error={
$LogMessageFunc ("DnsResolveFunc: name resolution failed for site: \ $LogMessageFunc ("DnsResolveFunc: name resolution failed for \
$WebSiteName!"); site: $WebSiteName!");
:return "*** test failed ***"; :return "*** test failed ***";
} }
} }
@ -42,16 +42,17 @@
# check arg type ip is passed # check arg type ip is passed
:if ([:typeof $IpAddress] != "ip") do={ :if ([:typeof $IpAddress] != "ip") do={
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not an \ $LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not \
IP address!"); an IP address!");
:return "*** test failed ***"; :return "*** test failed ***";
} }
# try a ping # try a ping
:do { :do {
return [/ping $IpAddress count=3]; return [/ping $IpAddress count=3];
} on-error { } on-error={
$LogMessageFunc ("PingIpAddressFunc: ping test failed: $IpAddress !"); $LogMessageFunc ("PingIpAddressFunc: ping test failed: \
$IpAddress !");
:return "*** test failed ***"; :return "*** test failed ***";
} }
} }
@ -62,9 +63,10 @@
:global LogMessageFunc; :global LogMessageFunc;
# check arg type str of correct length passed # check arg type str of correct length passed
:if (([:typeof $SiteName] != "str") or ([:len value=$SiteName] < 6)) do={ :if (([:typeof $SiteName] != "str") or \
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a valid \ ([:len value=$SiteName] < 6)) do={
string!"); $LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a \
valid string!");
:return "*** test failed ***"; :return "*** test failed ***";
} }
@ -72,7 +74,7 @@
:do { :do {
:return [/tool fetch url=("https://$SiteName") mode=https \ :return [/tool fetch url=("https://$SiteName") mode=https \
http-method=get as-value keep-result=no]; http-method=get as-value keep-result=no];
} on-error { } on-error={
$LogMessageFunc ("GetWebPageFunc: unable to retrieve site: \ $LogMessageFunc ("GetWebPageFunc: unable to retrieve site: \
$SiteName !"); $SiteName !");
:return { "duration"="*** test failed ***" } :return { "duration"="*** test failed ***" }
@ -108,7 +110,6 @@
} }
# cleanup global namespace # cleanup global namespace
:set Filename;
:set LogMessageFunc; :set LogMessageFunc;
:set DnsResolveFunc; :set DnsResolveFunc;
:set PingIpAddressFunc; :set PingIpAddressFunc;

View file

@ -1,4 +1,4 @@
:global Filename "ch10-08-bad-script.rsc" :local Filename "ch10-08-bad-script.rsc"
# A script to perform a series of tests on a # A script to perform a series of tests on a
# list of web sites. # list of web sites.
@ -9,7 +9,7 @@
# debug function # debug function
:global DebugFunc do={ :global DebugFunc do={
:global DEBUG; :global DEBUG;
:if ($DEBUG) do={ :put "**Debug: $1"; } :if ($DEBUG) do={ :put "**Debug: $1"; }
} }
# function to log error messages # function to log error messages
@ -35,8 +35,8 @@
:if (([:typeof $WebSiteName] != "str") or \ :if (([:typeof $WebSiteName] != "str") or \
([:len value=$WebSiteName] < 6)) do={ ([:len value=$WebSiteName] < 6)) do={
:local ErrorMsg "DnsResolveFunc: arg value $WebSiteName not a valid \ :local ErrorMsg "DnsResolveFunc: arg value $WebSiteName \
string!"; not a valid string!";
$LogMessageFunc $ErrorMsg; $LogMessageFunc $ErrorMsg;
$DebugFunc $ErrorMsg; $DebugFunc $ErrorMsg;
:return "*** test failed ***"; :return "*** test failed ***";
@ -51,8 +51,8 @@
:return $SiteIpAddr; :return $SiteIpAddr;
} on-error={ } on-error={
$DebugFunc "DNS lookup failed!"; $DebugFunc "DNS lookup failed!";
$LogMessageFunc ("DnsResolveFunc: name resolution failed for site: \ $LogMessageFunc ("DnsResolveFunc: name resolution failed for \
$WebSiteName!"); site: $WebSiteName!");
:return "*** test failed ***"; :return "*** test failed ***";
} }
} }
@ -64,16 +64,16 @@
# check arg type ip is passed # check arg type ip is passed
:if ([:typeof $IpAddress] != "ip") do={ :if ([:typeof $IpAddress] != "ip") do={
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not an IP \ $LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not \
address!"); an IP address!");
:return "*** test failed ***"; :return "*** test failed ***";
} }
:do { :do {
return [/ping $IpAddress count=3]; return [/ping $IpAddress count=3];
} on-error { } on-error {
$LogMessageFunc ("PingIpAddressFunc: ping test to IP address failed: \ $LogMessageFunc ("PingIpAddressFunc: ping test to IP address \
$IpAddress !"); failed: $IpAddress !");
:return "*** test failed ***"; :return "*** test failed ***";
} }
} }
@ -84,17 +84,19 @@
:global LogMessageFunc; :global LogMessageFunc;
# check arg type str of correct length passed # check arg type str of correct length passed
if (([:typeof $SiteName] != "str") or ([:len value=$SiteName] < 6)) do={ if (([:typeof $SiteName] != "str") or \
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a valid \ ([:len value=$SiteName] < 6)) do={
string!"); $LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a \
valid string!");
:return { "duration"="*** test failed ***" }; :return { "duration"="*** test failed ***" };
} }
do { do {
return [/tool fetch url=("https://$SiteName") mode=https http-method=get \ return [/tool fetch url=("https://$SiteName") mode=https \
as-value keep-result=no]; http-method=get as-value keep-result=no];
} on-error { } on-error {
$LogMessageFunc ("GetWebPageFunc: unable to retrieve site: $SiteName !"); $LogMessageFunc ("GetWebPageFunc: unable to retrieve site: \
$SiteName !");
return { "duration"="*** test failed ***" }; return { "duration"="*** test failed ***" };
} }
} }
@ -128,7 +130,6 @@
} }
# cleanup global namespace # cleanup global namespace
:set Filename;
:set LogMessageFunc; :set LogMessageFunc;
:set DnsResolveFunc; :set DnsResolveFunc;
:set PingIpAddressFunc; :set PingIpAddressFunc;

View file

@ -1,9 +1,9 @@
# ch10-10-add-remove-vlans.rsc # ch10-10-add-remove-vlans.rsc
# a script to add and remove 50 VLANs to/from # a script to add and remove 50 VLANs to/fromon-error={
# a Mikrotik router interface # a Mikrotik router interface
: for VlanId from 200 to 249 do={ :for VlanId from 200 to 249 do={
# find LAN interface ID # find LAN interface ID
:local LanInterface "ether2"; :local LanInterface "ether2";
@ -15,7 +15,7 @@
interface=$LanInterfaceId; interface=$LanInterfaceId;
} }
: for VlanId from 200 to 249 do={ :for VlanId from 200 to 249 do={
# find LAN interface ID # find LAN interface ID
:local LanInterface "ether2"; :local LanInterface "ether2";

View file

@ -7,7 +7,7 @@
:local LanInterface "ether2"; :local LanInterface "ether2";
:local LanInterfaceId [/interface find default-name=$LanInterface]; :local LanInterfaceId [/interface find default-name=$LanInterface];
: for VlanId from 200 to 249 do={ :for VlanId from 200 to 249 do={
# add VLAN to LAN interface # add VLAN to LAN interface
:local VlanName "VLAN$VlanId"; :local VlanName "VLAN$VlanId";
@ -15,7 +15,7 @@
interface=$LanInterfaceId; interface=$LanInterfaceId;
} }
: for VlanId from 200 to 249 do={ :for VlanId from 200 to 249 do={
# add VLAN to LAN interface # add VLAN to LAN interface
:local VlanName "VLAN$VlanId"; :local VlanName "VLAN$VlanId";

View file

@ -8,7 +8,7 @@
:local LanInterfaceId [/interface find default-name=$LanInterface]; :local LanInterfaceId [/interface find default-name=$LanInterface];
:local AddLoopTime [:time { :local AddLoopTime [:time {
: for VlanId from 200 to 249 do={ :for VlanId from 200 to 249 do={
# add VLAN to LAN interface # add VLAN to LAN interface
:local VlanName "VLAN$VlanId"; :local VlanName "VLAN$VlanId";
@ -20,7 +20,7 @@
:put "Add loop time: $AddLoopTime"; :put "Add loop time: $AddLoopTime";
:local RemoveLoopTime [:time { :local RemoveLoopTime [:time {
: for VlanId from 200 to 249 do={ :for VlanId from 200 to 249 do={
# add VLAN to LAN interface # add VLAN to LAN interface
:local VlanName "VLAN$VlanId"; :local VlanName "VLAN$VlanId";

View file

@ -1,6 +1,6 @@
# ch5-05-indentation.rsc # ch5-05-indentation.rsc
# Create an if-else statement with no additional indentation # Create an if-else statement with indentation
:local DayOfWeek "Monday"; :local DayOfWeek "Monday";
if ($DayOfWeek = "Monday") do={ if ($DayOfWeek = "Monday") do={

View file

@ -7,13 +7,13 @@
:put ("Variable contents: $SmallNumber"); :put ("Variable contents: $SmallNumber");
# Assign some data to the variable # Assign some data to the variable
:set SmallNumber 2 :set SmallNumber 2;
# Print its value again # Print its value again
:put ("Variable contents: $SmallNumber"); :put ("Variable contents: $SmallNumber");
# Let's change the variable value # Let's change the variable value
:set SmallNumber 3 :set SmallNumber 3;
# Print its value again # Print its value again
:put ("Variable contents: $SmallNumber"); :put ("Variable contents: $SmallNumber");

View file

@ -15,7 +15,8 @@ if ( [:typeof $WanInterfaces] = "nothing") do={
# Is the variable an array data type? # Is the variable an array data type?
if ( [:typeof $WanInterfaces] != "array") do={ if ( [:typeof $WanInterfaces] != "array") do={
:error "The WanInterfaces global variable is not an array data type. Exiting." :error "The WanInterfaces global variable is not an array data \
type. Exiting."
} }
# Let's step through the interfaces in the array # Let's step through the interfaces in the array

View file

@ -24,6 +24,6 @@ if ( $WanPingCount = 0) do={
} }
if ( ($WanPingCount < $PingCount) and ($WanPingCount > 0) ) do={ if ( ($WanPingCount < $PingCount) and ($WanPingCount > 0) ) do={
:log warning "The Internet connection may be degraded. (Ping result: \ :log warning "The Internet connection may be degraded. (Ping \
$WanPingCount/$PingCount)"; result: $WanPingCount/$PingCount)";
} }

View file

@ -5,7 +5,7 @@
:put "The current time is : $CurrentTime"; :put "The current time is : $CurrentTime";
# declare the time of day variable # declare the time of day variable
:local TimeOfDay :local TimeOfDay;
# Check if it's very early # Check if it's very early
:if (($CurrentTime >= 00:00) and ($CurrentTime < 06:00)) do={ :if (($CurrentTime >= 00:00) and ($CurrentTime < 06:00)) do={

View file

@ -5,7 +5,7 @@
:put "The current time is : $CurrentTime"; :put "The current time is : $CurrentTime";
# declare the time of day variable # declare the time of day variable
:local TimeOfDay :local TimeOfDay;
# Check if it's before noon # Check if it's before noon
:if ($CurrentTime < 12:00) do={ :if ($CurrentTime < 12:00) do={
@ -13,7 +13,8 @@
# it's before noon, let's see what time of the morning # it's before noon, let's see what time of the morning
# it is using a nested if-else statement # it is using a nested if-else statement
if ($CurrentTime < 06:00) do={ if ($CurrentTime < 06:00) do={
:set TimeOfDay "It's very early in the morning...shouldn't you be in bed?"; :set TimeOfDay "It's very early in the morning...shouldn't \
you be in bed?";
} else={ } else={
# it must be between 06:01 and 11:59 to reach here # it must be between 06:01 and 11:59 to reach here
:set TimeOfDay "Good morning!"; :set TimeOfDay "Good morning!";

View file

@ -3,12 +3,12 @@
# define WAN interface name # define WAN interface name
:local WanInterfaceName "ether1-WAN"; :local WanInterfaceName "ether1-WAN";
# find the interface index # find the interface ID
:local InterfaceIndex [/interface ethernet find name=$WanInterfaceName]; :local InterfaceId [/interface ethernet find name=$WanInterfaceName];
:local UpDown; :local UpDown;
if ([:interface ethernet get $InterfaceIndex]->"running") do={ if ([:interface ethernet get $InterfaceId]->"running") do={
:set UpDown "up"; :set UpDown "up";
} else={ } else={
:set UpDown "down"; :set UpDown "down";

View file

@ -4,10 +4,10 @@
:local WanInterfaceName "ether1-WAN"; :local WanInterfaceName "ether1-WAN";
# find the interface index # find the interface index
:local InterfaceIndex [/interface ethernet find name=$WanInterfaceName]; :local InterfaceId [/interface ethernet find name=$WanInterfaceName];
:local UpDown "down"; :local UpDown "down";
if ([:interface ethernet get $InterfaceIndex]->"running") do={ if ([:interface ethernet get $InterfaceId]->"running") do={
:set UpDown "up" :set UpDown "up"
} }

View file

@ -4,7 +4,7 @@
# the 12 times table # the 12 times table
:for LoopCount from=1 to=5 do={ :for LoopCount from=1 to=5 do={
:put ("Current loop counter is: $LoopCount"); :put "Current loop counter is: $LoopCount";
:put ("12 x $LoopCount is: " . (12 * $LoopCount)); :put ("12 x $LoopCount is: " . (12 * $LoopCount));
} }

View file

@ -12,9 +12,8 @@
:local PingResult [/ping $Site count=3 ]; :local PingResult [/ping $Site count=3 ];
:if ($PingResult > 0) do { :if ($PingResult > 0) do {
:put ("\nWeb site $Site reached OK\n"); :put "\nWeb site $Site reached OK\n";
} else={ } else={
:put ("\nNo response from web site: $Site\n"); :put "\nNo response from web site: $Site\n";
} }
} }

View file

@ -8,8 +8,8 @@
:for VlanId from=100 to=150 step=10 do={ :for VlanId from=100 to=150 step=10 do={
:put ("Creating VLAN $VlanId on interface $InterfaceName"); :put ("Creating VLAN $VlanId on interface $InterfaceName");
:local VlanName ("vlan" . $VlanId); :local VlanName ("vlan" . $VlanId);
/interface vlan add name=$VlanName vlan-id=$VlanId interface=$InterfaceName; /interface vlan add name=$VlanName vlan-id=$VlanId \
interface=$InterfaceName;
} }

View file

@ -28,12 +28,14 @@
} }
# data type checks passed, print the interface data # data type checks passed, print the interface data
:put "$InterfaceName: IP address = $InterfaceIp, speed = $InterfaceSpeed"; :put "$InterfaceName: IP address = $InterfaceIp, speed = \
$InterfaceSpeed";
} }
# --- end of function --- # --- end of function ---
# Let's call the function with some sample values in an array # Let's call the function with some sample values in an array
:local Args { ifname="WAN1"; ifspeed="100Mbps"; ifip="192.168.99.1/24" }; :local Args { ifname="WAN1"; ifspeed="100Mbps";
ifip="192.168.99.1/24" };
$PrintInterfaceAttrsFunc $Args; $PrintInterfaceAttrsFunc $Args;
# Let's call the function with some other values in an array # Let's call the function with some other values in an array

View file

@ -16,7 +16,7 @@
# function to remove last char from a string # function to remove last char from a string
# #
# e.g. :local ChoppedVar [$chopStrFunc "string with extra char,"] # e.g. :local ChoppedVar [$ChopStrFunc "string with extra char,"]
# #
:global ChopStrFunc do={ :global ChopStrFunc do={
:return [ :pick $1 0 ( [ :len $1 ] -1 ) ]; :return [ :pick $1 0 ( [ :len $1 ] -1 ) ];

View file

@ -1,6 +1,7 @@
# filename: ch9-07-use-str-module.rsc # filename: ch9-07-use-str-module.rsc
# import the functions we need and declare the function names we wish to use # import the functions we need and declare the function
# names we wish to use
/import "ch9-06-str-funcs-mod.rsc" /import "ch9-06-str-funcs-mod.rsc"
:global RemoveSpaceFunc; :global RemoveSpaceFunc;
:global ChopStrFunc; :global ChopStrFunc;

View file

@ -1,4 +1,4 @@
:global Filename "ch10-04-bad-script.rsc" :local Filename "ch10-04-bad-script.rsc"
# A simple script to perform a series of tests on a # A simple script to perform a series of tests on a
# list of web sites. # list of web sites.
@ -17,9 +17,10 @@
:global LogMessageFunc; :global LogMessageFunc;
# check arg type str of correct length passed # check arg type str of correct length passed
if (([:typeof $WebSiteName]!="str") or ([:len value=$WebSiteName] < 6)) do={ if (([:typeof $WebSiteName]!="str") or \
$LogMessageFunc ("DnsResolveFunc: arg value $WebSiteName not a valid \ ([:len value=$WebSiteName] < 6)) do={
string!"); $LogMessageFunc ("DnsResolveFunc: arg value $WebSiteName not \
a valid string!");
} }
return [/resolve $WebSiteName]; return [/resolve $WebSiteName];
@ -32,8 +33,8 @@
# check arg type ip is passed # check arg type ip is passed
if ([:typeof $IpAddress] != "ip") do={ if ([:typeof $IpAddress] != "ip") do={
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not an IP \ $LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not \
address!"); an IP address!");
} }
return [/ping $IpAddress count=3]; return [/ping $IpAddress count=3];
@ -45,13 +46,14 @@
:global LogMessageFunc; :global LogMessageFunc;
# check arg type str of correct length passed # check arg type str of correct length passed
if (([:typeof $SiteName] != "str") or ([:len value=$SiteName] < 6)) do={ if (([:typeof $SiteName] != "str") or \
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a valid \ ([:len value=$SiteName] < 6)) do={
string!"); $LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a \
valid string!");
} }
return [/tool fetch url=("https://$SiteName") mode=https http-method=get \ return [/tool fetch url=("https://$SiteName") mode=https \
as-value keep-result=no]; http-method=get as-value keep-result=no];
} }
########## ##########
@ -83,7 +85,6 @@
} }
# cleanup global namespace # cleanup global namespace
:set Filename;
:set LogMessageFunc; :set LogMessageFunc;
:set DnsResolveFunc; :set DnsResolveFunc;
:set PingIpAddressFunc; :set PingIpAddressFunc;

View file

@ -4,7 +4,7 @@
:do { :do {
:local SiteIpAddress [/resolve $SiteName]; :local SiteIpAddress [/resolve $SiteName];
:put "Site IP for $SiteName is $SiteIpAddress"; :put "Site IP for $SiteName is $SiteIpAddress";
} on-error { } on-error={
:put "Name lookup failed for $SiteName" :put "Name lookup failed for $SiteName"
} }
} }

View file

@ -1,4 +1,4 @@
:global Filename "ch10-07-bad-script.rsc" :local Filename "ch10-07-bad-script.rsc"
# A script to perform a series of tests on a # A script to perform a series of tests on a
# list of web sites. # list of web sites.
@ -19,8 +19,8 @@
# check arg type str of correct length passed # check arg type str of correct length passed
:if (([:typeof $WebSiteName] != "str") or \ :if (([:typeof $WebSiteName] != "str") or \
([:len value=$WebSiteName] < 6)) do={ ([:len value=$WebSiteName] < 6)) do={
$LogMessageFunc ("DnsResolveFunc: arg value $WebSiteName not a valid \ $LogMessageFunc ("DnsResolveFunc: arg value $WebSiteName not \
string!"); a valid string!");
:return "*** test failed ***"; :return "*** test failed ***";
} }
@ -29,8 +29,8 @@
:do { :do {
return [/resolve $WebSiteName]; return [/resolve $WebSiteName];
} on-error={ } on-error={
$LogMessageFunc ("DnsResolveFunc: name resolution failed for site: \ $LogMessageFunc ("DnsResolveFunc: name resolution failed for \
$WebSiteName!"); site: $WebSiteName!");
:return "*** test failed ***"; :return "*** test failed ***";
} }
} }
@ -42,16 +42,17 @@
# check arg type ip is passed # check arg type ip is passed
:if ([:typeof $IpAddress] != "ip") do={ :if ([:typeof $IpAddress] != "ip") do={
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not an \ $LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not \
IP address!"); an IP address!");
:return "*** test failed ***"; :return "*** test failed ***";
} }
# try a ping # try a ping
:do { :do {
return [/ping $IpAddress count=3]; return [/ping $IpAddress count=3];
} on-error { } on-error={
$LogMessageFunc ("PingIpAddressFunc: ping test failed: $IpAddress !"); $LogMessageFunc ("PingIpAddressFunc: ping test failed: \
$IpAddress !");
:return "*** test failed ***"; :return "*** test failed ***";
} }
} }
@ -62,9 +63,10 @@
:global LogMessageFunc; :global LogMessageFunc;
# check arg type str of correct length passed # check arg type str of correct length passed
:if (([:typeof $SiteName] != "str") or ([:len value=$SiteName] < 6)) do={ :if (([:typeof $SiteName] != "str") or \
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a valid \ ([:len value=$SiteName] < 6)) do={
string!"); $LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a \
valid string!");
:return "*** test failed ***"; :return "*** test failed ***";
} }
@ -72,7 +74,7 @@
:do { :do {
:return [/tool fetch url=("https://$SiteName") mode=https \ :return [/tool fetch url=("https://$SiteName") mode=https \
http-method=get as-value keep-result=no]; http-method=get as-value keep-result=no];
} on-error { } on-error={
$LogMessageFunc ("GetWebPageFunc: unable to retrieve site: \ $LogMessageFunc ("GetWebPageFunc: unable to retrieve site: \
$SiteName !"); $SiteName !");
:return { "duration"="*** test failed ***" } :return { "duration"="*** test failed ***" }
@ -108,7 +110,6 @@
} }
# cleanup global namespace # cleanup global namespace
:set Filename;
:set LogMessageFunc; :set LogMessageFunc;
:set DnsResolveFunc; :set DnsResolveFunc;
:set PingIpAddressFunc; :set PingIpAddressFunc;

View file

@ -1,4 +1,4 @@
:global Filename "ch10-08-bad-script.rsc" :local Filename "ch10-08-bad-script.rsc"
# A script to perform a series of tests on a # A script to perform a series of tests on a
# list of web sites. # list of web sites.
@ -9,7 +9,7 @@
# debug function # debug function
:global DebugFunc do={ :global DebugFunc do={
:global DEBUG; :global DEBUG;
:if ($DEBUG) do={ :put "**Debug: $1"; } :if ($DEBUG) do={ :put "**Debug: $1"; }
} }
# function to log error messages # function to log error messages
@ -35,8 +35,8 @@
:if (([:typeof $WebSiteName] != "str") or \ :if (([:typeof $WebSiteName] != "str") or \
([:len value=$WebSiteName] < 6)) do={ ([:len value=$WebSiteName] < 6)) do={
:local ErrorMsg "DnsResolveFunc: arg value $WebSiteName not a valid \ :local ErrorMsg "DnsResolveFunc: arg value $WebSiteName \
string!"; not a valid string!";
$LogMessageFunc $ErrorMsg; $LogMessageFunc $ErrorMsg;
$DebugFunc $ErrorMsg; $DebugFunc $ErrorMsg;
:return "*** test failed ***"; :return "*** test failed ***";
@ -51,8 +51,8 @@
:return $SiteIpAddr; :return $SiteIpAddr;
} on-error={ } on-error={
$DebugFunc "DNS lookup failed!"; $DebugFunc "DNS lookup failed!";
$LogMessageFunc ("DnsResolveFunc: name resolution failed for site: \ $LogMessageFunc ("DnsResolveFunc: name resolution failed for \
$WebSiteName!"); site: $WebSiteName!");
:return "*** test failed ***"; :return "*** test failed ***";
} }
} }
@ -64,16 +64,16 @@
# check arg type ip is passed # check arg type ip is passed
:if ([:typeof $IpAddress] != "ip") do={ :if ([:typeof $IpAddress] != "ip") do={
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not an IP \ $LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not \
address!"); an IP address!");
:return "*** test failed ***"; :return "*** test failed ***";
} }
:do { :do {
return [/ping $IpAddress count=3]; return [/ping $IpAddress count=3];
} on-error { } on-error {
$LogMessageFunc ("PingIpAddressFunc: ping test to IP address failed: \ $LogMessageFunc ("PingIpAddressFunc: ping test to IP address \
$IpAddress !"); failed: $IpAddress !");
:return "*** test failed ***"; :return "*** test failed ***";
} }
} }
@ -84,17 +84,19 @@
:global LogMessageFunc; :global LogMessageFunc;
# check arg type str of correct length passed # check arg type str of correct length passed
if (([:typeof $SiteName] != "str") or ([:len value=$SiteName] < 6)) do={ if (([:typeof $SiteName] != "str") or \
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a valid \ ([:len value=$SiteName] < 6)) do={
string!"); $LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a \
valid string!");
:return { "duration"="*** test failed ***" }; :return { "duration"="*** test failed ***" };
} }
do { do {
return [/tool fetch url=("https://$SiteName") mode=https http-method=get \ return [/tool fetch url=("https://$SiteName") mode=https \
as-value keep-result=no]; http-method=get as-value keep-result=no];
} on-error { } on-error {
$LogMessageFunc ("GetWebPageFunc: unable to retrieve site: $SiteName !"); $LogMessageFunc ("GetWebPageFunc: unable to retrieve site: \
$SiteName !");
return { "duration"="*** test failed ***" }; return { "duration"="*** test failed ***" };
} }
} }
@ -128,7 +130,6 @@
} }
# cleanup global namespace # cleanup global namespace
:set Filename;
:set LogMessageFunc; :set LogMessageFunc;
:set DnsResolveFunc; :set DnsResolveFunc;
:set PingIpAddressFunc; :set PingIpAddressFunc;

View file

@ -3,7 +3,7 @@
# a script to add and remove 50 VLANs to/from # a script to add and remove 50 VLANs to/from
# a Mikrotik router interface # a Mikrotik router interface
: for VlanId from 200 to 249 do={ :for VlanId from 200 to 249 do={
# find LAN interface ID # find LAN interface ID
:local LanInterface "ether2"; :local LanInterface "ether2";
@ -15,7 +15,7 @@
interface=$LanInterfaceId; interface=$LanInterfaceId;
} }
: for VlanId from 200 to 249 do={ :for VlanId from 200 to 249 do={
# find LAN interface ID # find LAN interface ID
:local LanInterface "ether2"; :local LanInterface "ether2";

View file

@ -7,7 +7,7 @@
:local LanInterface "ether2"; :local LanInterface "ether2";
:local LanInterfaceId [/interface find default-name=$LanInterface]; :local LanInterfaceId [/interface find default-name=$LanInterface];
: for VlanId from 200 to 249 do={ :for VlanId from 200 to 249 do={
# add VLAN to LAN interface # add VLAN to LAN interface
:local VlanName "VLAN$VlanId"; :local VlanName "VLAN$VlanId";
@ -15,7 +15,7 @@
interface=$LanInterfaceId; interface=$LanInterfaceId;
} }
: for VlanId from 200 to 249 do={ :for VlanId from 200 to 249 do={
# add VLAN to LAN interface # add VLAN to LAN interface
:local VlanName "VLAN$VlanId"; :local VlanName "VLAN$VlanId";

View file

@ -8,7 +8,7 @@
:local LanInterfaceId [/interface find default-name=$LanInterface]; :local LanInterfaceId [/interface find default-name=$LanInterface];
:local AddLoopTime [:time { :local AddLoopTime [:time {
: for VlanId from 200 to 249 do={ :for VlanId from 200 to 249 do={
# add VLAN to LAN interface # add VLAN to LAN interface
:local VlanName "VLAN$VlanId"; :local VlanName "VLAN$VlanId";
@ -20,7 +20,7 @@
:put "Add loop time: $AddLoopTime"; :put "Add loop time: $AddLoopTime";
:local RemoveLoopTime [:time { :local RemoveLoopTime [:time {
: for VlanId from 200 to 249 do={ :for VlanId from 200 to 249 do={
# add VLAN to LAN interface # add VLAN to LAN interface
:local VlanName "VLAN$VlanId"; :local VlanName "VLAN$VlanId";

View file

@ -7,13 +7,13 @@
:put ("Variable contents: $SmallNumber"); :put ("Variable contents: $SmallNumber");
# Assign some data to the variable # Assign some data to the variable
:set SmallNumber 2 :set SmallNumber 2;
# Print its value again # Print its value again
:put ("Variable contents: $SmallNumber"); :put ("Variable contents: $SmallNumber");
# Let's change the variable value # Let's change the variable value
:set SmallNumber 3 :set SmallNumber 3;
# Print its value again # Print its value again
:put ("Variable contents: $SmallNumber"); :put ("Variable contents: $SmallNumber");

View file

@ -15,7 +15,8 @@ if ( [:typeof $WanInterfaces] = "nothing") do={
# Is the variable an array data type? # Is the variable an array data type?
if ( [:typeof $WanInterfaces] != "array") do={ if ( [:typeof $WanInterfaces] != "array") do={
:error "The WanInterfaces global variable is not an array data type. Exiting." :error "The WanInterfaces global variable is not an array data \
type. Exiting."
} }
# Let's step through the interfaces in the array # Let's step through the interfaces in the array

View file

@ -24,6 +24,6 @@ if ( $WanPingCount = 0) do={
} }
if ( ($WanPingCount < $PingCount) and ($WanPingCount > 0) ) do={ if ( ($WanPingCount < $PingCount) and ($WanPingCount > 0) ) do={
:log warning "The Internet connection may be degraded. (Ping result: \ :log warning "The Internet connection may be degraded. (Ping \
$WanPingCount/$PingCount)"; result: $WanPingCount/$PingCount)";
} }

View file

@ -5,7 +5,7 @@
:put "The current time is : $CurrentTime"; :put "The current time is : $CurrentTime";
# declare the time of day variable # declare the time of day variable
:local TimeOfDay :local TimeOfDay;
# Check if it's very early # Check if it's very early
:if (($CurrentTime >= 00:00) and ($CurrentTime < 06:00)) do={ :if (($CurrentTime >= 00:00) and ($CurrentTime < 06:00)) do={

View file

@ -5,7 +5,7 @@
:put "The current time is : $CurrentTime"; :put "The current time is : $CurrentTime";
# declare the time of day variable # declare the time of day variable
:local TimeOfDay :local TimeOfDay;
# Check if it's before noon # Check if it's before noon
:if ($CurrentTime < 12:00) do={ :if ($CurrentTime < 12:00) do={
@ -13,7 +13,8 @@
# it's before noon, let's see what time of the morning # it's before noon, let's see what time of the morning
# it is using a nested if-else statement # it is using a nested if-else statement
if ($CurrentTime < 06:00) do={ if ($CurrentTime < 06:00) do={
:set TimeOfDay "It's very early in the morning...shouldn't you be in bed?"; :set TimeOfDay "It's very early in the morning...shouldn't \
you be in bed?";
} else={ } else={
# it must be between 06:01 and 11:59 to reach here # it must be between 06:01 and 11:59 to reach here
:set TimeOfDay "Good morning!"; :set TimeOfDay "Good morning!";

View file

@ -3,12 +3,12 @@
# define WAN interface name # define WAN interface name
:local WanInterfaceName "ether1-WAN"; :local WanInterfaceName "ether1-WAN";
# find the interface index # find the interface ID
:local InterfaceIndex [/interface/ethernet find name=$WanInterfaceName]; :local InterfaceId [/interface/ethernet find name=$WanInterfaceName];
:local UpDown; :local UpDown;
if ([:interface ethernet get $InterfaceIndex]->"running") do={ if ([:interface ethernet get $InterfaceId]->"running") do={
:set UpDown "up"; :set UpDown "up";
} else={ } else={
:set UpDown "down"; :set UpDown "down";

View file

@ -4,10 +4,10 @@
:local WanInterfaceName "ether1-WAN"; :local WanInterfaceName "ether1-WAN";
# find the interface index # find the interface index
:local InterfaceIndex [/interface/ethernet find name=$WanInterfaceName]; :local InterfaceId [/interface/ethernet find name=$WanInterfaceName];
:local UpDown "down"; :local UpDown "down";
if ([:interface/ethernet get $InterfaceIndex]->"running") do={ if ([:interface/ethernet get $InterfaceId]->"running") do={
:set UpDown "up" :set UpDown "up"
} }

View file

@ -4,7 +4,7 @@
# the 12 times table # the 12 times table
:for LoopCount from=1 to=5 do={ :for LoopCount from=1 to=5 do={
:put ("Current loop counter is: $LoopCount"); :put "Current loop counter is: $LoopCount";
:put ("12 x $LoopCount is: " . (12 * $LoopCount)); :put ("12 x $LoopCount is: " . (12 * $LoopCount));
} }

View file

@ -12,9 +12,8 @@
:local PingResult [/ping $Site count=3 ]; :local PingResult [/ping $Site count=3 ];
:if ($PingResult > 0) do { :if ($PingResult > 0) do {
:put ("\nWeb site $Site reached OK\n"); :put "\nWeb site $Site reached OK\n";
} else={ } else={
:put ("\nNo response from web site: $Site\n"); :put "\nNo response from web site: $Site\n";
} }
} }

View file

@ -8,8 +8,8 @@
:for VlanId from=100 to=150 step=10 do={ :for VlanId from=100 to=150 step=10 do={
:put ("Creating VLAN $VlanId on interface $InterfaceName"); :put ("Creating VLAN $VlanId on interface $InterfaceName");
:local VlanName ("vlan" . $VlanId); :local VlanName ("vlan" . $VlanId);
/interface vlan add name=$VlanName vlan-id=$VlanId interface=$InterfaceName; /interface vlan add name=$VlanName vlan-id=$VlanId \
interface=$InterfaceName;
} }

View file

@ -28,7 +28,8 @@
} }
# data type checks passed, print the interface data # data type checks passed, print the interface data
:put "$InterfaceName: IP address = $InterfaceIp, speed = $InterfaceSpeed"; :put "$InterfaceName: IP address = $InterfaceIp, speed = \
$InterfaceSpeed";
} }
# --- end of function --- # --- end of function ---
@ -37,7 +38,8 @@
$PrintInterfaceAttrsFunc $Args; $PrintInterfaceAttrsFunc $Args;
# Let's call the function with some other values in an array # Let's call the function with some other values in an array
:set Args { ifname="ether2-LAN"; ifspeed="1000Mbps"; ifip="172.16.1.254/24" }; :set Args { ifname="ether2-LAN"; ifspeed="1000Mbps";
ifip="172.16.1.254/24" };
$PrintInterfaceAttrsFunc $Args; $PrintInterfaceAttrsFunc $Args;
# Cleanup global namespace # Cleanup global namespace

View file

@ -16,7 +16,7 @@
# function to remove last char from a string # function to remove last char from a string
# #
# e.g. :local ChoppedVar [$chopStrFunc "string with extra char,"] # e.g. :local ChoppedVar [$ChopStrFunc "string with extra char,"]
# #
:global ChopStrFunc do={ :global ChopStrFunc do={
:return [ :pick $1 0 ( [ :len $1 ] -1 ) ]; :return [ :pick $1 0 ( [ :len $1 ] -1 ) ];

View file

@ -1,6 +1,7 @@
# filename: ch9-07-use-str-module.rsc # filename: ch9-07-use-str-module.rsc
# import the functions we need and declare the function names we wish to use # import the functions we need and declare the function
# names we wish to use
/import "ch9-06-str-funcs-mod.rsc" /import "ch9-06-str-funcs-mod.rsc"
:global RemoveSpaceFunc; :global RemoveSpaceFunc;
:global ChopStrFunc; :global ChopStrFunc;