mirror of
https://github.com/wifinigel/MikrotikScripting.git
synced 2025-06-27 03:48:59 +02:00
final edit fixes
This commit is contained in:
parent
b10b99bd87
commit
f3beb2ed9b
41 changed files with 164 additions and 150 deletions
|
@ -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
|
||||
# list of web sites.
|
||||
|
||||
|
@ -17,9 +17,10 @@
|
|||
:global LogMessageFunc;
|
||||
|
||||
# check arg type str of correct length passed
|
||||
if (([:typeof $WebSiteName]!="str") or ([:len value=$WebSiteName] < 6)) do={
|
||||
$LogMessageFunc ("DnsResolveFunc: arg value $WebSiteName not a valid \
|
||||
string!");
|
||||
if (([:typeof $WebSiteName]!="str") or \
|
||||
([:len value=$WebSiteName] < 6)) do={
|
||||
$LogMessageFunc ("DnsResolveFunc: arg value $WebSiteName not \
|
||||
a valid string!");
|
||||
}
|
||||
|
||||
return [/resolve $WebSiteName];
|
||||
|
@ -32,8 +33,8 @@
|
|||
|
||||
# check arg type ip is passed
|
||||
if ([:typeof $IpAddress] != "ip") do={
|
||||
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not an IP \
|
||||
address!");
|
||||
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not \
|
||||
an IP address!");
|
||||
}
|
||||
|
||||
return [/ping $IpAddress count=3];
|
||||
|
@ -45,13 +46,14 @@
|
|||
:global LogMessageFunc;
|
||||
|
||||
# check arg type str of correct length passed
|
||||
if (([:typeof $SiteName] != "str") or ([:len value=$SiteName] < 6)) do={
|
||||
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a valid \
|
||||
string!");
|
||||
if (([:typeof $SiteName] != "str") or \
|
||||
([:len value=$SiteName] < 6)) do={
|
||||
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a \
|
||||
valid string!");
|
||||
}
|
||||
|
||||
return [/tool fetch url=("https://$SiteName") mode=https http-method=get \
|
||||
as-value keep-result=no];
|
||||
return [/tool fetch url=("https://$SiteName") mode=https \
|
||||
http-method=get as-value keep-result=no];
|
||||
}
|
||||
|
||||
##########
|
||||
|
@ -83,7 +85,6 @@
|
|||
}
|
||||
|
||||
# cleanup global namespace
|
||||
:set Filename;
|
||||
:set LogMessageFunc;
|
||||
:set DnsResolveFunc;
|
||||
:set PingIpAddressFunc;
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
:do {
|
||||
:local SiteIpAddress [/resolve $SiteName];
|
||||
:put "Site IP for $SiteName is $SiteIpAddress";
|
||||
} on-error {
|
||||
} on-error={
|
||||
:put "Name lookup failed for $SiteName"
|
||||
}
|
||||
}
|
|
@ -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
|
||||
# list of web sites.
|
||||
|
||||
|
@ -19,8 +19,8 @@
|
|||
# check arg type str of correct length passed
|
||||
:if (([:typeof $WebSiteName] != "str") or \
|
||||
([:len value=$WebSiteName] < 6)) do={
|
||||
$LogMessageFunc ("DnsResolveFunc: arg value $WebSiteName not a valid \
|
||||
string!");
|
||||
$LogMessageFunc ("DnsResolveFunc: arg value $WebSiteName not \
|
||||
a valid string!");
|
||||
|
||||
:return "*** test failed ***";
|
||||
}
|
||||
|
@ -29,8 +29,8 @@
|
|||
:do {
|
||||
return [/resolve $WebSiteName];
|
||||
} on-error={
|
||||
$LogMessageFunc ("DnsResolveFunc: name resolution failed for site: \
|
||||
$WebSiteName!");
|
||||
$LogMessageFunc ("DnsResolveFunc: name resolution failed for \
|
||||
site: $WebSiteName!");
|
||||
:return "*** test failed ***";
|
||||
}
|
||||
}
|
||||
|
@ -42,16 +42,17 @@
|
|||
|
||||
# check arg type ip is passed
|
||||
:if ([:typeof $IpAddress] != "ip") do={
|
||||
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not an \
|
||||
IP address!");
|
||||
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not \
|
||||
an IP address!");
|
||||
:return "*** test failed ***";
|
||||
}
|
||||
|
||||
# try a ping
|
||||
:do {
|
||||
return [/ping $IpAddress count=3];
|
||||
} on-error {
|
||||
$LogMessageFunc ("PingIpAddressFunc: ping test failed: $IpAddress !");
|
||||
} on-error={
|
||||
$LogMessageFunc ("PingIpAddressFunc: ping test failed: \
|
||||
$IpAddress !");
|
||||
:return "*** test failed ***";
|
||||
}
|
||||
}
|
||||
|
@ -62,9 +63,10 @@
|
|||
:global LogMessageFunc;
|
||||
|
||||
# check arg type str of correct length passed
|
||||
:if (([:typeof $SiteName] != "str") or ([:len value=$SiteName] < 6)) do={
|
||||
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a valid \
|
||||
string!");
|
||||
:if (([:typeof $SiteName] != "str") or \
|
||||
([:len value=$SiteName] < 6)) do={
|
||||
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a \
|
||||
valid string!");
|
||||
:return "*** test failed ***";
|
||||
}
|
||||
|
||||
|
@ -72,7 +74,7 @@
|
|||
:do {
|
||||
:return [/tool fetch url=("https://$SiteName") mode=https \
|
||||
http-method=get as-value keep-result=no];
|
||||
} on-error {
|
||||
} on-error={
|
||||
$LogMessageFunc ("GetWebPageFunc: unable to retrieve site: \
|
||||
$SiteName !");
|
||||
:return { "duration"="*** test failed ***" }
|
||||
|
@ -108,7 +110,6 @@
|
|||
}
|
||||
|
||||
# cleanup global namespace
|
||||
:set Filename;
|
||||
:set LogMessageFunc;
|
||||
:set DnsResolveFunc;
|
||||
:set PingIpAddressFunc;
|
||||
|
|
|
@ -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
|
||||
# list of web sites.
|
||||
|
||||
|
@ -9,7 +9,7 @@
|
|||
# debug function
|
||||
:global DebugFunc do={
|
||||
:global DEBUG;
|
||||
:if ($DEBUG) do={ :put "**Debug: $1"; }
|
||||
:if ($DEBUG) do={ :put "**Debug: $1"; }
|
||||
}
|
||||
|
||||
# function to log error messages
|
||||
|
@ -35,8 +35,8 @@
|
|||
|
||||
:if (([:typeof $WebSiteName] != "str") or \
|
||||
([:len value=$WebSiteName] < 6)) do={
|
||||
:local ErrorMsg "DnsResolveFunc: arg value $WebSiteName not a valid \
|
||||
string!";
|
||||
:local ErrorMsg "DnsResolveFunc: arg value $WebSiteName \
|
||||
not a valid string!";
|
||||
$LogMessageFunc $ErrorMsg;
|
||||
$DebugFunc $ErrorMsg;
|
||||
:return "*** test failed ***";
|
||||
|
@ -51,8 +51,8 @@
|
|||
:return $SiteIpAddr;
|
||||
} on-error={
|
||||
$DebugFunc "DNS lookup failed!";
|
||||
$LogMessageFunc ("DnsResolveFunc: name resolution failed for site: \
|
||||
$WebSiteName!");
|
||||
$LogMessageFunc ("DnsResolveFunc: name resolution failed for \
|
||||
site: $WebSiteName!");
|
||||
:return "*** test failed ***";
|
||||
}
|
||||
}
|
||||
|
@ -64,16 +64,16 @@
|
|||
|
||||
# check arg type ip is passed
|
||||
:if ([:typeof $IpAddress] != "ip") do={
|
||||
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not an IP \
|
||||
address!");
|
||||
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not \
|
||||
an IP address!");
|
||||
:return "*** test failed ***";
|
||||
}
|
||||
|
||||
:do {
|
||||
return [/ping $IpAddress count=3];
|
||||
} on-error {
|
||||
$LogMessageFunc ("PingIpAddressFunc: ping test to IP address failed: \
|
||||
$IpAddress !");
|
||||
$LogMessageFunc ("PingIpAddressFunc: ping test to IP address \
|
||||
failed: $IpAddress !");
|
||||
:return "*** test failed ***";
|
||||
}
|
||||
}
|
||||
|
@ -84,17 +84,19 @@
|
|||
:global LogMessageFunc;
|
||||
|
||||
# check arg type str of correct length passed
|
||||
if (([:typeof $SiteName] != "str") or ([:len value=$SiteName] < 6)) do={
|
||||
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a valid \
|
||||
string!");
|
||||
if (([:typeof $SiteName] != "str") or \
|
||||
([:len value=$SiteName] < 6)) do={
|
||||
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a \
|
||||
valid string!");
|
||||
:return { "duration"="*** test failed ***" };
|
||||
}
|
||||
|
||||
do {
|
||||
return [/tool fetch url=("https://$SiteName") mode=https http-method=get \
|
||||
as-value keep-result=no];
|
||||
return [/tool fetch url=("https://$SiteName") mode=https \
|
||||
http-method=get as-value keep-result=no];
|
||||
} on-error {
|
||||
$LogMessageFunc ("GetWebPageFunc: unable to retrieve site: $SiteName !");
|
||||
$LogMessageFunc ("GetWebPageFunc: unable to retrieve site: \
|
||||
$SiteName !");
|
||||
return { "duration"="*** test failed ***" };
|
||||
}
|
||||
}
|
||||
|
@ -128,7 +130,6 @@
|
|||
}
|
||||
|
||||
# cleanup global namespace
|
||||
:set Filename;
|
||||
:set LogMessageFunc;
|
||||
:set DnsResolveFunc;
|
||||
:set PingIpAddressFunc;
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
# 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
|
||||
|
||||
: for VlanId from 200 to 249 do={
|
||||
:for VlanId from 200 to 249 do={
|
||||
|
||||
# find LAN interface ID
|
||||
:local LanInterface "ether2";
|
||||
|
@ -15,7 +15,7 @@
|
|||
interface=$LanInterfaceId;
|
||||
}
|
||||
|
||||
: for VlanId from 200 to 249 do={
|
||||
:for VlanId from 200 to 249 do={
|
||||
|
||||
# find LAN interface ID
|
||||
:local LanInterface "ether2";
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
:local LanInterface "ether2";
|
||||
: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
|
||||
:local VlanName "VLAN$VlanId";
|
||||
|
@ -15,7 +15,7 @@
|
|||
interface=$LanInterfaceId;
|
||||
}
|
||||
|
||||
: for VlanId from 200 to 249 do={
|
||||
:for VlanId from 200 to 249 do={
|
||||
|
||||
# add VLAN to LAN interface
|
||||
:local VlanName "VLAN$VlanId";
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
:local LanInterfaceId [/interface find default-name=$LanInterface];
|
||||
|
||||
:local AddLoopTime [:time {
|
||||
: for VlanId from 200 to 249 do={
|
||||
:for VlanId from 200 to 249 do={
|
||||
|
||||
# add VLAN to LAN interface
|
||||
:local VlanName "VLAN$VlanId";
|
||||
|
@ -20,7 +20,7 @@
|
|||
:put "Add loop time: $AddLoopTime";
|
||||
|
||||
:local RemoveLoopTime [:time {
|
||||
: for VlanId from 200 to 249 do={
|
||||
:for VlanId from 200 to 249 do={
|
||||
|
||||
# add VLAN to LAN interface
|
||||
:local VlanName "VLAN$VlanId";
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# ch5-05-indentation.rsc
|
||||
|
||||
# Create an if-else statement with no additional indentation
|
||||
# Create an if-else statement with indentation
|
||||
:local DayOfWeek "Monday";
|
||||
|
||||
if ($DayOfWeek = "Monday") do={
|
||||
|
|
|
@ -7,13 +7,13 @@
|
|||
:put ("Variable contents: $SmallNumber");
|
||||
|
||||
# Assign some data to the variable
|
||||
:set SmallNumber 2
|
||||
:set SmallNumber 2;
|
||||
|
||||
# Print its value again
|
||||
:put ("Variable contents: $SmallNumber");
|
||||
|
||||
# Let's change the variable value
|
||||
:set SmallNumber 3
|
||||
:set SmallNumber 3;
|
||||
|
||||
# Print its value again
|
||||
:put ("Variable contents: $SmallNumber");
|
|
@ -15,7 +15,8 @@ if ( [:typeof $WanInterfaces] = "nothing") do={
|
|||
|
||||
# Is the variable an array data type?
|
||||
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
|
||||
|
|
|
@ -24,6 +24,6 @@ if ( $WanPingCount = 0) do={
|
|||
}
|
||||
|
||||
if ( ($WanPingCount < $PingCount) and ($WanPingCount > 0) ) do={
|
||||
:log warning "The Internet connection may be degraded. (Ping result: \
|
||||
$WanPingCount/$PingCount)";
|
||||
:log warning "The Internet connection may be degraded. (Ping \
|
||||
result: $WanPingCount/$PingCount)";
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
:put "The current time is : $CurrentTime";
|
||||
|
||||
# declare the time of day variable
|
||||
:local TimeOfDay
|
||||
:local TimeOfDay;
|
||||
|
||||
# Check if it's very early
|
||||
:if (($CurrentTime >= 00:00) and ($CurrentTime < 06:00)) do={
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
:put "The current time is : $CurrentTime";
|
||||
|
||||
# declare the time of day variable
|
||||
:local TimeOfDay
|
||||
:local TimeOfDay;
|
||||
|
||||
# Check if it's before noon
|
||||
:if ($CurrentTime < 12:00) do={
|
||||
|
@ -13,7 +13,8 @@
|
|||
# it's before noon, let's see what time of the morning
|
||||
# it is using a nested if-else statement
|
||||
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={
|
||||
# it must be between 06:01 and 11:59 to reach here
|
||||
:set TimeOfDay "Good morning!";
|
||||
|
|
|
@ -3,12 +3,12 @@
|
|||
# define WAN interface name
|
||||
:local WanInterfaceName "ether1-WAN";
|
||||
|
||||
# find the interface index
|
||||
:local InterfaceIndex [/interface ethernet find name=$WanInterfaceName];
|
||||
# find the interface ID
|
||||
:local InterfaceId [/interface ethernet find name=$WanInterfaceName];
|
||||
|
||||
:local UpDown;
|
||||
|
||||
if ([:interface ethernet get $InterfaceIndex]->"running") do={
|
||||
if ([:interface ethernet get $InterfaceId]->"running") do={
|
||||
:set UpDown "up";
|
||||
} else={
|
||||
:set UpDown "down";
|
||||
|
|
|
@ -4,10 +4,10 @@
|
|||
:local WanInterfaceName "ether1-WAN";
|
||||
|
||||
# find the interface index
|
||||
:local InterfaceIndex [/interface ethernet find name=$WanInterfaceName];
|
||||
:local InterfaceId [/interface ethernet find name=$WanInterfaceName];
|
||||
|
||||
:local UpDown "down";
|
||||
if ([:interface ethernet get $InterfaceIndex]->"running") do={
|
||||
if ([:interface ethernet get $InterfaceId]->"running") do={
|
||||
:set UpDown "up"
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
# the 12 times table
|
||||
: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));
|
||||
}
|
||||
|
||||
|
|
|
@ -12,9 +12,8 @@
|
|||
|
||||
:local PingResult [/ping $Site count=3 ];
|
||||
:if ($PingResult > 0) do {
|
||||
:put ("\nWeb site $Site reached OK\n");
|
||||
:put "\nWeb site $Site reached OK\n";
|
||||
} else={
|
||||
:put ("\nNo response from web site: $Site\n");
|
||||
:put "\nNo response from web site: $Site\n";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -8,8 +8,8 @@
|
|||
:for VlanId from=100 to=150 step=10 do={
|
||||
|
||||
:put ("Creating VLAN $VlanId on interface $InterfaceName");
|
||||
|
||||
:local VlanName ("vlan" . $VlanId);
|
||||
/interface vlan add name=$VlanName vlan-id=$VlanId interface=$InterfaceName;
|
||||
/interface vlan add name=$VlanName vlan-id=$VlanId \
|
||||
interface=$InterfaceName;
|
||||
}
|
||||
|
||||
|
|
|
@ -28,12 +28,14 @@
|
|||
}
|
||||
|
||||
# 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 ---
|
||||
|
||||
# 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;
|
||||
|
||||
# Let's call the function with some other values in an array
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
# 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={
|
||||
:return [ :pick $1 0 ( [ :len $1 ] -1 ) ];
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
# 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"
|
||||
:global RemoveSpaceFunc;
|
||||
:global ChopStrFunc;
|
||||
|
|
|
@ -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
|
||||
# list of web sites.
|
||||
|
||||
|
@ -17,9 +17,10 @@
|
|||
:global LogMessageFunc;
|
||||
|
||||
# check arg type str of correct length passed
|
||||
if (([:typeof $WebSiteName]!="str") or ([:len value=$WebSiteName] < 6)) do={
|
||||
$LogMessageFunc ("DnsResolveFunc: arg value $WebSiteName not a valid \
|
||||
string!");
|
||||
if (([:typeof $WebSiteName]!="str") or \
|
||||
([:len value=$WebSiteName] < 6)) do={
|
||||
$LogMessageFunc ("DnsResolveFunc: arg value $WebSiteName not \
|
||||
a valid string!");
|
||||
}
|
||||
|
||||
return [/resolve $WebSiteName];
|
||||
|
@ -32,8 +33,8 @@
|
|||
|
||||
# check arg type ip is passed
|
||||
if ([:typeof $IpAddress] != "ip") do={
|
||||
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not an IP \
|
||||
address!");
|
||||
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not \
|
||||
an IP address!");
|
||||
}
|
||||
|
||||
return [/ping $IpAddress count=3];
|
||||
|
@ -45,13 +46,14 @@
|
|||
:global LogMessageFunc;
|
||||
|
||||
# check arg type str of correct length passed
|
||||
if (([:typeof $SiteName] != "str") or ([:len value=$SiteName] < 6)) do={
|
||||
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a valid \
|
||||
string!");
|
||||
if (([:typeof $SiteName] != "str") or \
|
||||
([:len value=$SiteName] < 6)) do={
|
||||
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a \
|
||||
valid string!");
|
||||
}
|
||||
|
||||
return [/tool fetch url=("https://$SiteName") mode=https http-method=get \
|
||||
as-value keep-result=no];
|
||||
return [/tool fetch url=("https://$SiteName") mode=https \
|
||||
http-method=get as-value keep-result=no];
|
||||
}
|
||||
|
||||
##########
|
||||
|
@ -83,7 +85,6 @@
|
|||
}
|
||||
|
||||
# cleanup global namespace
|
||||
:set Filename;
|
||||
:set LogMessageFunc;
|
||||
:set DnsResolveFunc;
|
||||
:set PingIpAddressFunc;
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
:do {
|
||||
:local SiteIpAddress [/resolve $SiteName];
|
||||
:put "Site IP for $SiteName is $SiteIpAddress";
|
||||
} on-error {
|
||||
} on-error={
|
||||
:put "Name lookup failed for $SiteName"
|
||||
}
|
||||
}
|
|
@ -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
|
||||
# list of web sites.
|
||||
|
||||
|
@ -19,8 +19,8 @@
|
|||
# check arg type str of correct length passed
|
||||
:if (([:typeof $WebSiteName] != "str") or \
|
||||
([:len value=$WebSiteName] < 6)) do={
|
||||
$LogMessageFunc ("DnsResolveFunc: arg value $WebSiteName not a valid \
|
||||
string!");
|
||||
$LogMessageFunc ("DnsResolveFunc: arg value $WebSiteName not \
|
||||
a valid string!");
|
||||
|
||||
:return "*** test failed ***";
|
||||
}
|
||||
|
@ -29,8 +29,8 @@
|
|||
:do {
|
||||
return [/resolve $WebSiteName];
|
||||
} on-error={
|
||||
$LogMessageFunc ("DnsResolveFunc: name resolution failed for site: \
|
||||
$WebSiteName!");
|
||||
$LogMessageFunc ("DnsResolveFunc: name resolution failed for \
|
||||
site: $WebSiteName!");
|
||||
:return "*** test failed ***";
|
||||
}
|
||||
}
|
||||
|
@ -42,16 +42,17 @@
|
|||
|
||||
# check arg type ip is passed
|
||||
:if ([:typeof $IpAddress] != "ip") do={
|
||||
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not an \
|
||||
IP address!");
|
||||
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not \
|
||||
an IP address!");
|
||||
:return "*** test failed ***";
|
||||
}
|
||||
|
||||
# try a ping
|
||||
:do {
|
||||
return [/ping $IpAddress count=3];
|
||||
} on-error {
|
||||
$LogMessageFunc ("PingIpAddressFunc: ping test failed: $IpAddress !");
|
||||
} on-error={
|
||||
$LogMessageFunc ("PingIpAddressFunc: ping test failed: \
|
||||
$IpAddress !");
|
||||
:return "*** test failed ***";
|
||||
}
|
||||
}
|
||||
|
@ -62,9 +63,10 @@
|
|||
:global LogMessageFunc;
|
||||
|
||||
# check arg type str of correct length passed
|
||||
:if (([:typeof $SiteName] != "str") or ([:len value=$SiteName] < 6)) do={
|
||||
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a valid \
|
||||
string!");
|
||||
:if (([:typeof $SiteName] != "str") or \
|
||||
([:len value=$SiteName] < 6)) do={
|
||||
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a \
|
||||
valid string!");
|
||||
:return "*** test failed ***";
|
||||
}
|
||||
|
||||
|
@ -72,7 +74,7 @@
|
|||
:do {
|
||||
:return [/tool fetch url=("https://$SiteName") mode=https \
|
||||
http-method=get as-value keep-result=no];
|
||||
} on-error {
|
||||
} on-error={
|
||||
$LogMessageFunc ("GetWebPageFunc: unable to retrieve site: \
|
||||
$SiteName !");
|
||||
:return { "duration"="*** test failed ***" }
|
||||
|
@ -108,7 +110,6 @@
|
|||
}
|
||||
|
||||
# cleanup global namespace
|
||||
:set Filename;
|
||||
:set LogMessageFunc;
|
||||
:set DnsResolveFunc;
|
||||
:set PingIpAddressFunc;
|
||||
|
|
|
@ -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
|
||||
# list of web sites.
|
||||
|
||||
|
@ -9,7 +9,7 @@
|
|||
# debug function
|
||||
:global DebugFunc do={
|
||||
:global DEBUG;
|
||||
:if ($DEBUG) do={ :put "**Debug: $1"; }
|
||||
:if ($DEBUG) do={ :put "**Debug: $1"; }
|
||||
}
|
||||
|
||||
# function to log error messages
|
||||
|
@ -35,8 +35,8 @@
|
|||
|
||||
:if (([:typeof $WebSiteName] != "str") or \
|
||||
([:len value=$WebSiteName] < 6)) do={
|
||||
:local ErrorMsg "DnsResolveFunc: arg value $WebSiteName not a valid \
|
||||
string!";
|
||||
:local ErrorMsg "DnsResolveFunc: arg value $WebSiteName \
|
||||
not a valid string!";
|
||||
$LogMessageFunc $ErrorMsg;
|
||||
$DebugFunc $ErrorMsg;
|
||||
:return "*** test failed ***";
|
||||
|
@ -51,8 +51,8 @@
|
|||
:return $SiteIpAddr;
|
||||
} on-error={
|
||||
$DebugFunc "DNS lookup failed!";
|
||||
$LogMessageFunc ("DnsResolveFunc: name resolution failed for site: \
|
||||
$WebSiteName!");
|
||||
$LogMessageFunc ("DnsResolveFunc: name resolution failed for \
|
||||
site: $WebSiteName!");
|
||||
:return "*** test failed ***";
|
||||
}
|
||||
}
|
||||
|
@ -64,16 +64,16 @@
|
|||
|
||||
# check arg type ip is passed
|
||||
:if ([:typeof $IpAddress] != "ip") do={
|
||||
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not an IP \
|
||||
address!");
|
||||
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not \
|
||||
an IP address!");
|
||||
:return "*** test failed ***";
|
||||
}
|
||||
|
||||
:do {
|
||||
return [/ping $IpAddress count=3];
|
||||
} on-error {
|
||||
$LogMessageFunc ("PingIpAddressFunc: ping test to IP address failed: \
|
||||
$IpAddress !");
|
||||
$LogMessageFunc ("PingIpAddressFunc: ping test to IP address \
|
||||
failed: $IpAddress !");
|
||||
:return "*** test failed ***";
|
||||
}
|
||||
}
|
||||
|
@ -84,17 +84,19 @@
|
|||
:global LogMessageFunc;
|
||||
|
||||
# check arg type str of correct length passed
|
||||
if (([:typeof $SiteName] != "str") or ([:len value=$SiteName] < 6)) do={
|
||||
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a valid \
|
||||
string!");
|
||||
if (([:typeof $SiteName] != "str") or \
|
||||
([:len value=$SiteName] < 6)) do={
|
||||
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a \
|
||||
valid string!");
|
||||
:return { "duration"="*** test failed ***" };
|
||||
}
|
||||
|
||||
do {
|
||||
return [/tool fetch url=("https://$SiteName") mode=https http-method=get \
|
||||
as-value keep-result=no];
|
||||
return [/tool fetch url=("https://$SiteName") mode=https \
|
||||
http-method=get as-value keep-result=no];
|
||||
} on-error {
|
||||
$LogMessageFunc ("GetWebPageFunc: unable to retrieve site: $SiteName !");
|
||||
$LogMessageFunc ("GetWebPageFunc: unable to retrieve site: \
|
||||
$SiteName !");
|
||||
return { "duration"="*** test failed ***" };
|
||||
}
|
||||
}
|
||||
|
@ -128,7 +130,6 @@
|
|||
}
|
||||
|
||||
# cleanup global namespace
|
||||
:set Filename;
|
||||
:set LogMessageFunc;
|
||||
:set DnsResolveFunc;
|
||||
:set PingIpAddressFunc;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
# a script to add and remove 50 VLANs to/from
|
||||
# a Mikrotik router interface
|
||||
|
||||
: for VlanId from 200 to 249 do={
|
||||
:for VlanId from 200 to 249 do={
|
||||
|
||||
# find LAN interface ID
|
||||
:local LanInterface "ether2";
|
||||
|
@ -15,7 +15,7 @@
|
|||
interface=$LanInterfaceId;
|
||||
}
|
||||
|
||||
: for VlanId from 200 to 249 do={
|
||||
:for VlanId from 200 to 249 do={
|
||||
|
||||
# find LAN interface ID
|
||||
:local LanInterface "ether2";
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
:local LanInterface "ether2";
|
||||
: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
|
||||
:local VlanName "VLAN$VlanId";
|
||||
|
@ -15,7 +15,7 @@
|
|||
interface=$LanInterfaceId;
|
||||
}
|
||||
|
||||
: for VlanId from 200 to 249 do={
|
||||
:for VlanId from 200 to 249 do={
|
||||
|
||||
# add VLAN to LAN interface
|
||||
:local VlanName "VLAN$VlanId";
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
:local LanInterfaceId [/interface find default-name=$LanInterface];
|
||||
|
||||
:local AddLoopTime [:time {
|
||||
: for VlanId from 200 to 249 do={
|
||||
:for VlanId from 200 to 249 do={
|
||||
|
||||
# add VLAN to LAN interface
|
||||
:local VlanName "VLAN$VlanId";
|
||||
|
@ -20,7 +20,7 @@
|
|||
:put "Add loop time: $AddLoopTime";
|
||||
|
||||
:local RemoveLoopTime [:time {
|
||||
: for VlanId from 200 to 249 do={
|
||||
:for VlanId from 200 to 249 do={
|
||||
|
||||
# add VLAN to LAN interface
|
||||
:local VlanName "VLAN$VlanId";
|
||||
|
|
|
@ -7,13 +7,13 @@
|
|||
:put ("Variable contents: $SmallNumber");
|
||||
|
||||
# Assign some data to the variable
|
||||
:set SmallNumber 2
|
||||
:set SmallNumber 2;
|
||||
|
||||
# Print its value again
|
||||
:put ("Variable contents: $SmallNumber");
|
||||
|
||||
# Let's change the variable value
|
||||
:set SmallNumber 3
|
||||
:set SmallNumber 3;
|
||||
|
||||
# Print its value again
|
||||
:put ("Variable contents: $SmallNumber");
|
|
@ -15,7 +15,8 @@ if ( [:typeof $WanInterfaces] = "nothing") do={
|
|||
|
||||
# Is the variable an array data type?
|
||||
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
|
||||
|
|
|
@ -24,6 +24,6 @@ if ( $WanPingCount = 0) do={
|
|||
}
|
||||
|
||||
if ( ($WanPingCount < $PingCount) and ($WanPingCount > 0) ) do={
|
||||
:log warning "The Internet connection may be degraded. (Ping result: \
|
||||
$WanPingCount/$PingCount)";
|
||||
:log warning "The Internet connection may be degraded. (Ping \
|
||||
result: $WanPingCount/$PingCount)";
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
:put "The current time is : $CurrentTime";
|
||||
|
||||
# declare the time of day variable
|
||||
:local TimeOfDay
|
||||
:local TimeOfDay;
|
||||
|
||||
# Check if it's very early
|
||||
:if (($CurrentTime >= 00:00) and ($CurrentTime < 06:00)) do={
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
:put "The current time is : $CurrentTime";
|
||||
|
||||
# declare the time of day variable
|
||||
:local TimeOfDay
|
||||
:local TimeOfDay;
|
||||
|
||||
# Check if it's before noon
|
||||
:if ($CurrentTime < 12:00) do={
|
||||
|
@ -13,7 +13,8 @@
|
|||
# it's before noon, let's see what time of the morning
|
||||
# it is using a nested if-else statement
|
||||
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={
|
||||
# it must be between 06:01 and 11:59 to reach here
|
||||
:set TimeOfDay "Good morning!";
|
||||
|
|
|
@ -3,12 +3,12 @@
|
|||
# define WAN interface name
|
||||
:local WanInterfaceName "ether1-WAN";
|
||||
|
||||
# find the interface index
|
||||
:local InterfaceIndex [/interface/ethernet find name=$WanInterfaceName];
|
||||
# find the interface ID
|
||||
:local InterfaceId [/interface/ethernet find name=$WanInterfaceName];
|
||||
|
||||
:local UpDown;
|
||||
|
||||
if ([:interface ethernet get $InterfaceIndex]->"running") do={
|
||||
if ([:interface ethernet get $InterfaceId]->"running") do={
|
||||
:set UpDown "up";
|
||||
} else={
|
||||
:set UpDown "down";
|
||||
|
|
|
@ -4,10 +4,10 @@
|
|||
:local WanInterfaceName "ether1-WAN";
|
||||
|
||||
# find the interface index
|
||||
:local InterfaceIndex [/interface/ethernet find name=$WanInterfaceName];
|
||||
:local InterfaceId [/interface/ethernet find name=$WanInterfaceName];
|
||||
|
||||
:local UpDown "down";
|
||||
if ([:interface/ethernet get $InterfaceIndex]->"running") do={
|
||||
if ([:interface/ethernet get $InterfaceId]->"running") do={
|
||||
:set UpDown "up"
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
# the 12 times table
|
||||
: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));
|
||||
}
|
||||
|
||||
|
|
|
@ -12,9 +12,8 @@
|
|||
|
||||
:local PingResult [/ping $Site count=3 ];
|
||||
:if ($PingResult > 0) do {
|
||||
:put ("\nWeb site $Site reached OK\n");
|
||||
:put "\nWeb site $Site reached OK\n";
|
||||
} else={
|
||||
:put ("\nNo response from web site: $Site\n");
|
||||
:put "\nNo response from web site: $Site\n";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -8,8 +8,8 @@
|
|||
:for VlanId from=100 to=150 step=10 do={
|
||||
|
||||
:put ("Creating VLAN $VlanId on interface $InterfaceName");
|
||||
|
||||
:local VlanName ("vlan" . $VlanId);
|
||||
/interface vlan add name=$VlanName vlan-id=$VlanId interface=$InterfaceName;
|
||||
/interface vlan add name=$VlanName vlan-id=$VlanId \
|
||||
interface=$InterfaceName;
|
||||
}
|
||||
|
||||
|
|
|
@ -28,7 +28,8 @@
|
|||
}
|
||||
|
||||
# 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 ---
|
||||
|
||||
|
@ -37,7 +38,8 @@
|
|||
$PrintInterfaceAttrsFunc $Args;
|
||||
|
||||
# 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;
|
||||
|
||||
# Cleanup global namespace
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
# 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={
|
||||
:return [ :pick $1 0 ( [ :len $1 ] -1 ) ];
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
# 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"
|
||||
:global RemoveSpaceFunc;
|
||||
:global ChopStrFunc;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue