update for v6/v7 support

This commit is contained in:
Wifinigel 2023-01-24 20:28:40 +00:00
parent 4871e9432b
commit b10b99bd87
120 changed files with 1479 additions and 22 deletions

View file

@ -1,4 +0,0 @@
# A simple script to print your Mikrotik board type
:local boardType [/system resource get board]
:put "Your Mikrotik board type is : $boardType"

View file

@ -1,5 +0,0 @@
# A simple script to print your Mikrotik board architecture
:local architectureName [/system resource get architecture-name]
#:put "Your Mikrotik board architecture is : $architectureName"
:log info "Your Mikrotik board architecture is : $architectureName"

View file

@ -0,0 +1,6 @@
# filename: ch1-01-board-type.rsc
# A simple script to print your Mikrotik board type
:local BoardType [/system resource get board];
:put "Your Mikrotik board type is : $BoardType";

View file

@ -0,0 +1,6 @@
# filename: ch1-02-get-board-arch.rsc
# A simple script to print your Mikrotik board architecture
:local ArchitectureName [/system resource get architecture-name];
:put "Your Mikrotik board architecture is : $ArchitectureName";

View file

@ -2,5 +2,5 @@
# Hello World Script
/local Message "Hello World!";
/log info $Message;
:local Message "Hello World!";
:log info $Message;

View file

@ -24,5 +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)";
}

View file

@ -11,7 +11,7 @@
:if ($CurrentTime > 12:00) do={
:put "Good day!";
} else={
:put "Good morning!"
:put "Good morning!";
}
# Say goodbye

View file

@ -6,11 +6,11 @@
# Check if it's after noon
:if ($CurrentTime > 12:00) do={
:local TimeOfDay "Afternoon/Evening"
:local TimeOfDay "Afternoon/Evening";
} else={
# otherwise, it must be morning (00:00 to 11:59)
:local TimeOfDay "Morning"
:local TimeOfDay "Morning";
}
# Print out the time of day
:put "At the moment, it's : $TimeOfDay"
:put "At the moment, it's : $TimeOfDay";

View file

@ -5,15 +5,15 @@
:put "The current time is : $CurrentTime";
# declare the time of day variable
:local TimeOfDay
:local TimeOfDay;
# Check if it's after noon
:if ($CurrentTime > 12:00) do={
:set TimeOfDay "Afternoon/Evening"
:set TimeOfDay "Afternoon/Evening";
} else={
# otherwise, it must be morning (00:00 to 11:59)
:set TimeOfDay "Morning"
:set TimeOfDay "Morning";
}
# Print out the time of day
:put "At the moment, it's : $TimeOfDay"
:put "At the moment, it's : $TimeOfDay";

View file

@ -9,9 +9,9 @@
:local UpDown;
if ([:interface ethernet get $InterfaceIndex]->"running") do={
:set UpDown "up"
:set UpDown "up";
} else={
:set UpDown "down"
:set UpDown "down";
}
:put "Interface $WanInterfaceName is currently $UpDown";

View file

@ -0,0 +1,6 @@
# filename: ch1-01-board-type.rsc
# A simple script to print your Mikrotik board type
:local BoardType [/system/resource get board];
:put "Your Mikrotik board type is : $BoardType";

View file

@ -0,0 +1,6 @@
# filename: ch1-02-get-board-arch.rsc
# A simple script to print your Mikrotik board architecture
:local ArchitectureName [/system/resource get architecture-name];
:put "Your Mikrotik board architecture is : $ArchitectureName";

View file

@ -0,0 +1,17 @@
# filename: ch10-01-bad-script.rsc
:local w {"mikrotik.com"; "www.google.com"; "twitter.com"};
:foreach s in=$w do={
:local i [/resolve $s];
:local p [/ping $i count=3 ];
:local u [/tool fetch url=("https://$s") mode=https http-method=get \
as-value keep-result=no];
:put "==========================================";
:put ("Site: $s");
:put ("IP Address = $i");
:put ("Ping success = $p/3");
:put ("Page d/load duration: " . $u->"duration");
:put "===========================================";
}

View file

@ -0,0 +1,29 @@
# filename: ch10-02-bad-script.rsc
#
# A simple script to perform a series of tests on a
# list of web sites.
# define list of websites to test
:local w {"mikrotik.com"; "www.google.com"; "twitter.com"};
# step through each website and perform various tests
:foreach s in=$w do={
# try a DNS resolution of this site
:local i [/resolve $s];
# try pinging the IP address of this site
:local p [/ping $i count=3 ];
# try fetching the web page of this site
:local u [/tool fetch url=("https://$s") mode=https http-method=get \
as-value keep-result=no];
# print out a summary report for this site
:put "==========================================";
:put ("Site: $s");
:put ("IP Address = $i");
:put ("Ping success = $p/3");
:put ("Page d/load duration: " . $u->"duration");
:put "===========================================";
}

View file

@ -0,0 +1,29 @@
# filename: ch10-03-bad-script.rsc
#
# A simple script to perform a series of tests on a
# list of web sites.
# define list of websites to test
:local WebSites {"mikrotik.com"; "www.google.com"; "twitter.com"};
# step through each website and perform various tests
:foreach SiteName in=$WebSites do={
# try a DNS resolution of this site
:local IpAddress [/resolve $SiteName];
# try pinging the IP address of this site
:local PingResult [/ping $IpAddress count=3 ];
# try getting the web page of this site
:local UrlFetch [/tool fetch url=("https://$SiteName") mode=https \
http-method=get as-value keep-result=no];
# print out a summary report for this site
:put "==========================================";
:put ("Site: $SiteName");
:put ("IP Address = $IpAddress");
:put ("Ping success = $PingResult/3");
:put ("Page d/load duration: " . $UrlFetch->"duration");
:put "===========================================";
}

View file

@ -0,0 +1,90 @@
:global Filename "ch10-04-bad-script.rsc"
# A simple script to perform a series of tests on a
# list of web sites.
# function to log error message
:global LogMessageFunc do={
:global Filename;
:local ErrorMessage ("$Filename: $1");
:log error $ErrorMessage;
:error $ErrorMessage;
}
# function to DNS resolve a site name to IP
:global DnsResolveFunc do={
:local WebSiteName $1;
: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!");
}
return [/resolve $WebSiteName];
}
# function to ping an IP address 3 times
:global PingIpAddressFunc do={
:local IpAddress $1;
:global LogMessageFunc;
# check arg type ip is passed
if ([:typeof $IpAddress] != "ip") do={
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not an IP \
address!");
}
return [/ping $IpAddress count=3];
}
# function to return the result of fetching a web page
:global GetWebPageFunc do={
:local SiteName $1;
: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!");
}
return [/tool fetch url=("https://$SiteName") mode=https http-method=get \
as-value keep-result=no];
}
##########
# Main
##########
# define list of websites to test
:local WebSites { "mikrotik.com"; "www.google.com"; "twitter.com"};
# step through each website and perform various tests
:foreach SiteName in=$WebSites do={
# try a DNS resolution of this site
:local IpAddress [$DnsResolveFunc $SiteName];
# try pinging the IP address of this site
:local PingResult [$PingIpAddressFunc $IpAddress];
# try getting the web page of this site
:local UrlFetch [$GetWebPageFunc $SiteName];
# print out a summary report for this site
:put "==========================================";
:put ("Site: $SiteName");
:put ("IP Address = $IpAddress");
:put ("Ping success = $PingResult/3");
:put ("Page d/load duration: " . $UrlFetch->"duration");
:put "===========================================";
}
# cleanup global namespace
:set Filename;
:set LogMessageFunc;
:set DnsResolveFunc;
:set PingIpAddressFunc;
:set GetWebPageFunc;

View file

@ -0,0 +1,7 @@
# ch10-05-error-check.rsc
:local WebSites { "badname1234.com"; "google.com"};
:foreach SiteName in=$WebSites do={
:local SiteIpAddress [/resolve $SiteName];
:put "Site IP for $SiteName is $SiteIpAddress";
}

View file

@ -0,0 +1,10 @@
# ch10-06-error-check.rsc
:local WebSites { "badname1234.com"; "google.com"};
:foreach SiteName in=$WebSites do={
:do {
:local SiteIpAddress [/resolve $SiteName];
:put "Site IP for $SiteName is $SiteIpAddress";
} on-error {
:put "Name lookup failed for $SiteName"
}
}

View file

@ -0,0 +1,115 @@
:global Filename "ch10-07-bad-script.rsc"
# A script to perform a series of tests on a
# list of web sites.
# function to log error messages
:global LogMessageFunc do={
:global Filename;
:local ErrorMessage "$Filename: $1";
:log error $ErrorMessage;
:put $ErrorMessage; # print to CLI
}
# function to DNS resolve a site name to IP
:global DnsResolveFunc do={
:local WebSiteName $1;
: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!");
:return "*** test failed ***";
}
# try a name lookup
:do {
return [/resolve $WebSiteName];
} on-error={
$LogMessageFunc ("DnsResolveFunc: name resolution failed for site: \
$WebSiteName!");
:return "*** test failed ***";
}
}
# function to ping an IP address 3 times
:global PingIpAddressFunc do={
:local IpAddress $1;
:global LogMessageFunc;
# check arg type ip is passed
:if ([:typeof $IpAddress] != "ip") do={
$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 !");
:return "*** test failed ***";
}
}
# function to return the result of fetching a web page
:global GetWebPageFunc do={
:local SiteName $1;
: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!");
:return "*** test failed ***";
}
# try to get web page
:do {
: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 !");
:return { "duration"="*** test failed ***" }
}
}
##########
# Main
##########
# define list of websites to test
:local WebSites { "badsite1234.com"; "www.google.com"; "twitter.com"};
# step through each website and perform various tests
:foreach SiteName in=$WebSites do={
# try a DNS resolution of this site
:local IpAddress [$DnsResolveFunc $SiteName];
# try pinging the IP address of this site
:local PingResult [$PingIpAddressFunc $IpAddress];
# try getting the web page of this site
:local UrlFetch [$GetWebPageFunc $SiteName];
# print out a summary report for this site
:put "==========================================";
:put ("Site: $SiteName");
:put ("IP Address = $IpAddress");
:put ("Ping success = $PingResult/3");
:put ("Page d/load duration: " . $UrlFetch->"duration");
:put "===========================================";
}
# cleanup global namespace
:set Filename;
:set LogMessageFunc;
:set DnsResolveFunc;
:set PingIpAddressFunc;
:set GetWebPageFunc;

View file

@ -0,0 +1,137 @@
:global Filename "ch10-08-bad-script.rsc"
# A script to perform a series of tests on a
# list of web sites.
# set DEBUG to "true" for script debugging output,
# "false" for normal operation
:global DEBUG false;
# debug function
:global DebugFunc do={
:global DEBUG;
:if ($DEBUG) do={ :put "**Debug: $1"; }
}
# function to log error messages
:global LogMessageFunc do={
:global Filename;
:local ErrorMessage "$Filename: $1";
:log error $ErrorMessage;
:put $ErrorMessage; # print to CLI
}
# function to DNS resolve a site name to IP
:global DnsResolveFunc do={
:local WebSiteName $1;
:global LogMessageFunc;
:global DebugFunc;
$DebugFunc "Entering DnsResolveFunc";
$DebugFunc ("Arg passed to func: $WebSiteName");
# check arg type str of correct length passed
$DebugFunc "Checking arg data type is correct";
:if (([:typeof $WebSiteName] != "str") or \
([:len value=$WebSiteName] < 6)) do={
:local ErrorMsg "DnsResolveFunc: arg value $WebSiteName not a valid \
string!";
$LogMessageFunc $ErrorMsg;
$DebugFunc $ErrorMsg;
:return "*** test failed ***";
} else={
$DebugFunc "Data type looks good";
}
:do {
$DebugFunc "Looking up IP of site name...";
:local SiteIpAddr [/resolve $WebSiteName];
$DebugFunc $SiteIpAddr;
:return $SiteIpAddr;
} on-error={
$DebugFunc "DNS lookup failed!";
$LogMessageFunc ("DnsResolveFunc: name resolution failed for site: \
$WebSiteName!");
:return "*** test failed ***";
}
}
# function to ping an IP address 3 times
:global PingIpAddressFunc do={
:local IpAddress $1;
:global LogMessageFunc;
# check arg type ip is passed
:if ([:typeof $IpAddress] != "ip") do={
$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 !");
:return "*** test failed ***";
}
}
# function to return the result of fetching a web page
:global GetWebPageFunc do={
:local SiteName $1;
: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!");
:return { "duration"="*** test failed ***" };
}
do {
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 !");
return { "duration"="*** test failed ***" };
}
}
##########
# Main
##########
# define list of websites to test
:local WebSites { "badsite1234.com"; "www.google.com"; "twitter.com"};
# step through each website and perform various tests
:foreach SiteName in=$WebSites do={
# try a DNS resolution of this site
:local IpAddress [$DnsResolveFunc $SiteName];
# try pinging the IP address of this site
:local PingResult [$PingIpAddressFunc $IpAddress];
# try getting the web page of this site
:local UrlFetch [$GetWebPageFunc $SiteName];
# print out a summary report for this site
:put "==========================================";
:put ("Site: $SiteName");
:put ("IP Address = $IpAddress");
:put ("Ping success = $PingResult/3");
:put ("Page d/load duration: " . $UrlFetch->"duration");
:put "===========================================";
}
# cleanup global namespace
:set Filename;
:set LogMessageFunc;
:set DnsResolveFunc;
:set PingIpAddressFunc;
:set GetWebPageFunc;
:set DEBUG;
:set DebugFunc;

View file

@ -0,0 +1,7 @@
# ch10-09-simple-pause.rsc
# A simple script that includes a 2 second pause
:put "Hello we're going to pause now...";
:delay 2;
:put "Pause complete. We're all done now!";

View file

@ -0,0 +1,27 @@
# ch10-10-add-remove-vlans.rsc
# a script to add and remove 50 VLANs to/from
# a Mikrotik router interface
: for VlanId from 200 to 249 do={
# find LAN interface ID
:local LanInterface "ether2";
:local LanInterfaceId [/interface find default-name=$LanInterface];
# add VLAN to LAN interface
:local VlanName "VLAN$VlanId";
/interface/vlan add name="$VlanName" vlan-id=$VlanId \
interface=$LanInterfaceId;
}
: for VlanId from 200 to 249 do={
# find LAN interface ID
:local LanInterface "ether2";
:local LanInterfaceId [/interface find default-name=$LanInterface];
# add VLAN to LAN interface
:local VlanName "VLAN$VlanId";
/interface/vlan remove "$VlanName";
}

View file

@ -0,0 +1,23 @@
# ch10-11-add-remove-vlans.rsc
# a script to add and remove 50 VLANs to/from
# a Mikrotik router interface
# find LAN interface ID
:local LanInterface "ether2";
:local LanInterfaceId [/interface find default-name=$LanInterface];
: for VlanId from 200 to 249 do={
# add VLAN to LAN interface
:local VlanName "VLAN$VlanId";
/interface vlan add name="$VlanName" vlan-id=$VlanId \
interface=$LanInterfaceId;
}
: for VlanId from 200 to 249 do={
# add VLAN to LAN interface
:local VlanName "VLAN$VlanId";
/interface vlan remove "$VlanName";
}

View file

@ -0,0 +1,31 @@
# ch10-12-add-remove-vlans.rsc
# a script to add and remove 50 VLANs to/from
# a Mikrotik router interface
# find LAN interface ID
:local LanInterface "ether2";
:local LanInterfaceId [/interface find default-name=$LanInterface];
:local AddLoopTime [:time {
: for VlanId from 200 to 249 do={
# add VLAN to LAN interface
:local VlanName "VLAN$VlanId";
/interface vlan add name="$VlanName" vlan-id=$VlanId \
interface=$LanInterfaceId;
}
}];
:put "Add loop time: $AddLoopTime";
:local RemoveLoopTime [:time {
: for VlanId from 200 to 249 do={
# add VLAN to LAN interface
:local VlanName "VLAN$VlanId";
/interface vlan remove "$VlanName";
}
}];
:put "Remove loop time: $RemoveLoopTime";

View file

@ -0,0 +1,6 @@
# filename: ch2-01-hello-world.rsc
# Hello World Script
:local Message "Hello World!";
:log info $Message;

View file

@ -0,0 +1,19 @@
# ch4-01-bad-ip-data-type.rsc
# Assign an IP address to a variable
:local InterfaceIp "192.168.99.2";
# Print out the variable contents
:put "The interface IP address in \$InterfaceIp is: $InterfaceIp";
# It looks like an IP address - let's check to make sure
:put ("The variable data type of \$InterfaceIp is: ". [:typeof $InterfaceIp]);
# Hmmmm...looks like we need to fix it
:local InterfaceIp [:toip $InterfaceIp];
# Let's print the fixed variable
:put "The interface IP address in \$InterfaceIp is now: $InterfaceIp";
# Let's check its data type
:put ("The variable data type of \$InterfaceIp is now: ". [:typeof $InterfaceIp]);

View file

@ -0,0 +1,14 @@
# ch4-02-ip-prefix-slicer.rsc
# Create a function to slice up an IP prefix
:global IpPrefixSlicerFunc do={
:local InterfaceIp ([/ip/address get $1]->"address");
:local SlashPosition [:find $InterfaceIp "/"];
:local IpAddress [:pick $InterfaceIp 0 $SlashPosition];
:return [:toip $IpAddress]
}
# Run the function for ID *1 and verify data type of result
:local IpAddress [$IpPrefixSlicerFunc *1];
:put ("Result: $IpAddress");
:put ("Data type: " . [:typeof $IpAddress]);

View file

@ -0,0 +1,9 @@
# ch5-01-no-semi-colons.rsc
:local MULTIPLIER 10
:for Number from=1 to=10 do={
:local Result ($Number * $MULTIPLIER)
:put ("This is an unusually long line. Here is the result $Number times \
$MULTIPLIER is: $Result")
}

View file

@ -0,0 +1,9 @@
# ch5-02-semi-colons.rsc
:local MULTIPLIER 10;
:for Number from=1 to=10 do={
:local Result ($Number * $MULTIPLIER);
:put ("This is an unusually long line. Here is the result $Number times \
$MULTIPLIER is: $Result");
}

View file

@ -0,0 +1,10 @@
# filename: ch5-03-long-line.rsc
# Here is a long line
:put [/interface/ethernet find where name~"ether" running true rx-fcs-error=0];
# Here is same line across multiple lines
:put [/interface/ethernet find where \
name~"ether" \
running true \
rx-fcs-error=0];

View file

@ -0,0 +1,7 @@
# ch5-04-no-indentation.rsc
# Create an if-else statement with no additional indentation
:local DayOfWeek "Monday";
if ($DayOfWeek = "Monday") do={:put "Boo...it's Monday."
} else={:put "Yay! It's not Monday!"}

View file

@ -0,0 +1,10 @@
# ch5-05-indentation.rsc
# Create an if-else statement with no additional indentation
:local DayOfWeek "Monday";
if ($DayOfWeek = "Monday") do={
:put "Boo...it's Monday.";
} else={
:put "Yay! It's not Monday!";
}

View file

@ -0,0 +1,21 @@
# ch6-01-variable-value-test.rsc
# Declare a variable with no value
:local BridgeName;
# Test if the variable has a value
:if ([:typeof $BridgeName] = "nothing") do={
:put "Test 1: this variable has no value!";
} else= {
:put ("Test 1: the variable value is: $BridgeName");
}
# Set a value for the variable
:set BridgeName "Bridge1";
# Test if the variable has a value
:if ([:typeof $BridgeName] = "nothing") do={
:put "Test 2: this variable has no value!";
} else= {
:put ("Test 2: the variable value is: $BridgeName");
}

View file

@ -0,0 +1,14 @@
# ch6-02-undeclared-variable.rsc
# Declare a variable but assign no value
:local RouteDistance;
# Let's test the data type of the variable
:if ([:typeof $RouteDistance] = "nothing") do={
:put "Test 1: this variable has no value!";
}
# Let's test the data type of a non-existent variable
:if ([:typeof $RouteDist] = "nothing") do={
:put "Test 2: this variable has no value!";
}

View file

@ -0,0 +1,10 @@
# ---- global scope start ----
# filename: ch6-03-global-scope.rsc
# Let's create a variable in the global scope
:local GlobalScopeVar "I'm in the global scope!";
# Let's print it out
:put ("Global variable contents = $GlobalScopeVar");
# ---- global scope end ----

View file

@ -0,0 +1,26 @@
# ---- global scope start ----
# filename: ch6-04-local-scope.rsc
# Let's create a variable in the global scope
:local GlobalScopeVar "I'm in the global scope!";
{
# ---- start of a local scope ----
# Let's create a variable in this scope
:local LocalScopeVar "I'm in the local scope!";
# Let's print out the variable in this scope
:put ("Local scope variable contents = $LocalScopeVar");
# ---- end of local scope ----
}
# Let's print it out the global scope variable
:put ("Global scope variable contents = $GlobalScopeVar");
# Let's print out the local scope variable
# (hint: this won't work)
:put ("Local scope variable contents = $LocalScopeVar");
# ---- global scope end ----

View file

@ -0,0 +1,25 @@
# ---- global scope start ----
# filename: ch6-05-local-scope.rsc
# Let's create a variable in the global scope
:local GlobalScopeVar "I'm in the global scope!";
{
# ---- start of a local scope ----
# Let's create a variable in this scope
:local LocalScopeVar "I'm in the local scope!";
# Let's print out the variable in this scope
:put ("Local scope variable contents = $LocalScopeVar");
# Let's see if we can access the global scope variable
:put ("Global scope variable (in local scope) = $GlobalScopeVar");
# ---- end of local scope ----
}
# Let's print out the global scope variable
:put ("Global scope variable (in global scope) = $GlobalScopeVar");
# ---- global scope end ----

View file

@ -0,0 +1,33 @@
# filename: ch6-06-local-scope.rsc
# ---- start of a local scope ----
{
:local apple "green";
:local banana "yellow";
:put ("(1) The apple is: $apple");
:put ("(1) The banana is: $banana");
# ---- start of child local scope ----
{
:local apple "red";
:put ("(2) The apple is: $apple");
:put ("(2) The banana is: $banana");
}
# ---- end of child local scope ----
:put ("(3) The apple is: $apple");
:put ("(3) The banana is: $banana");
}
# ---- end of local scope ----
:put ("(4) The apple is: $apple");
:put ("(4) The banana is: $banana");
# ---- start of new local scope ----
{
:local apple "yellow";
:put ("(5) The apple is: $apple");
:put ("(5) The banana is: $banana");
}
# ---- end of new local scope ----

View file

@ -0,0 +1,13 @@
# filename: ch6-07-variable-types.rsc
# Create a local variable
:local Apple "green";
# Print its value
:put ("Local variable contents: $Apple");
# Create a global variable
:global Banana "green";
# Print its value
:put ("Global variable contents: $Banana");

View file

@ -0,0 +1,19 @@
# filename: ch6-08-local-vars.rsc
# Create a local variable
:local SmallNumber;
# Print its value
:put ("Variable contents: $SmallNumber");
# Assign some data to the variable
:set SmallNumber 2
# Print its value again
:put ("Variable contents: $SmallNumber");
# Let's change the variable value
:set SmallNumber 3
# Print its value again
:put ("Variable contents: $SmallNumber");

View file

@ -0,0 +1,20 @@
# filename: ch6-09-local-vars2.rsc
# Create a local variable that is an array of words
:local Words { "Hello, "; "how "; "are "; "you ?"};
# Create a local variable to store concatenated words in a string
:local WordString;
# Step through each word and add to the "WordString" variable
:foreach Word in=$Words do={
:put "Word from array: $Word";
:set WordString ($WordString . $Word);
}
# Print out the result
:put "\n";
:put "New string we've built:";
:put $WordString;
:put "\n";
:put ("Original array: " . [:tostr $Words]);

View file

@ -0,0 +1,36 @@
# filename: ch6-10-global-vars.rsc
# Note: this script assumes global vars $Day,
# $Month and $Year have been set via the CLI
#
# Let's get a dump of our current global variables:
:environment print;
# Let's check we can access each variable:
:put "The day today is $Day";
:put "The month today is $Month";
:put "The year today is $Year";
# Uh-oh...we had an issue, no values were printed!
:put "\nThat didn't work!\n";
# To access global variables declared outside our script
# we need to declare them again within our script, but do
# not assign a value when declaring them:
:global Day;
:global Month;
:global Year
# Let's try printing them again:
:put "The day today is $Day";
:put "The month today is $Month";
:put "The year today is $Year";
# Let's declare a new global variable to
# access outside of this script:
:global TimeOfDay "morning";
:global Breakfast "muesli";
# Lets get a dump of our current global variables:
:put "\nCurrent global variables:\n";
:environment print;

View file

@ -0,0 +1,32 @@
# filename: ch6-11-global-vars2.rsc
# Lets get a dump of our current global variables:
:environment print;
# Let's declare a new global variable
:global Beverage "coffee";
# Let's declare global vars that already exist so that
# we may access them
:global Fruit;
:global TellTheTruth;
# Let's print modify the vars if we no not
# wish to tell the truth
if ($TellTheTruth = false) do={
:set Fruit "bananas";
:set Beverage "beer";
}
# Let's see which are my favourite fruit and
# beverage
:put "My favourite fruit is: $Fruit";
:put "My favourite beverage is: $Beverage";
if ($TellTheTruth = false) do={
:put "(pssst...this is actually a lie)";
}
# Lets get a dump of our current global variables:
:put "\nCurrent global variables:\n";
:environment print;

View file

@ -0,0 +1,37 @@
# filename: ch6-12-global-vars3.rsc
# In this script, we expect to have several interface names
# in a global variable called "WanInterfaces". The variable
# should be an array for the script to work correctly
# Declare the "WanInterfaces" global variable to access
# its data (assuming it exists)
:global WanInterfaces;
# Does it have a value? If not, exit with an error
if ( [:typeof $WanInterfaces] = "nothing") do={
:error "The WanInterfaces global variable is not set. Exiting"
}
# Is the variable an array data type?
if ( [:typeof $WanInterfaces] != "array") do={
:error "The WanInterfaces global variable is not an array data type. Exiting."
}
# Let's step through the interfaces in the array
# and print out their operation status
:foreach WanInterface in=$WanInterfaces do={
:local InterfaceIndex [/interface/ethernet find name=$WanInterface];
:local UpDown "down";
if ([:interface/ethernet get $InterfaceIndex]->"running") do={
:set UpDown "up"
}
:put "Interface $WanInterface is currently $UpDown";
}
# Let's assume no-one else will need the "WanInterfaces"
# variable. Let's remove it from global variables
:set WanInterfaces;

View file

@ -0,0 +1,16 @@
# filename: ch7-01-basic-if.rsc
# Print a greeting
:put "Hello, I hope you're well.";
# Check if the time is after 6pm (get time in 21:46:04 format)
:local CurrentTime [:system clock get time];
:put "The current time is : $CurrentTime";
# Say good evening if time is after 18:00
:if ($CurrentTime > 18:00) do={
:put "Good evening!";
}
# Say goodbye
:put "Thanks for visiting, bye!";

View file

@ -0,0 +1,29 @@
# filename: ch7-02-basic-if.rsc
# Create a variable for the WAN interface name
:local WanInterface "ether1-WAN";
# Create a variable for the number of pings we'd like to send
:local PingCount 10;
# Create a variable for the destination on the Internet to ping
:local DestinationAddress 8.8.8.8;
# Let's try a ping to the Internet across the WAN interface
:local WanPingCount [/ping $DestinationAddress interface=$WanInterface \
count=$PingCount];
# Let's log the result of our Internet connection test
if ( $WanPingCount = $PingCount) do={
:log info "The Internet is up.";
}
if ( $WanPingCount = 0) do={
:log error "The Internet is down.";
}
if ( ($WanPingCount < $PingCount) and ($WanPingCount > 0) ) do={
:log warning "The Internet connection may be degraded. (Ping result: \
$WanPingCount/$PingCount)";
}

View file

@ -0,0 +1,18 @@
# filename: ch7-03-if-else.rsc
# Print a greeting
:put "Hello, I hope you're well.";
# Check if the time is after 6pm (get time in 21:46:04 format)
:local CurrentTime [:system clock get time];
:put "The current time is : $CurrentTime";
# Say good morning if time is before 12:00
:if ($CurrentTime > 12:00) do={
:put "Good day!";
} else={
:put "Good morning!";
}
# Say goodbye
:put "Thanks for visiting, bye!";

View file

@ -0,0 +1,16 @@
# filename: ch7-04-if-else-vars.rsc
# Get the current time
:local CurrentTime [:system clock get time];
:put "The current time is : $CurrentTime";
# Check if it's after noon
:if ($CurrentTime > 12:00) do={
:local TimeOfDay "Afternoon/Evening";
} else={
# otherwise, it must be morning (00:00 to 11:59)
:local TimeOfDay "Morning";
}
# Print out the time of day
:put "At the moment, it's : $TimeOfDay";

View file

@ -0,0 +1,19 @@
# filename: ch7-05-if-else-vars2.rsc
# Get the current time
:local CurrentTime [:system clock get time];
:put "The current time is : $CurrentTime";
# declare the time of day variable
:local TimeOfDay;
# Check if it's after noon
:if ($CurrentTime > 12:00) do={
:set TimeOfDay "Afternoon/Evening";
} else={
# otherwise, it must be morning (00:00 to 11:59)
:set TimeOfDay "Morning";
}
# Print out the time of day
:put "At the moment, it's : $TimeOfDay";

Some files were not shown because too many files have changed in this diff Show more