wifinigel.MikrotikScripting/scripts/v6/ch10-08-bad-script.rsc

140 lines
3.9 KiB
Text
Raw Normal View History

:global Filename "ch10-08-bad-script.rsc"
2023-01-19 21:27:15 +00:00
# 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;
2023-02-19 13:53:15 +00:00
:if ($DEBUG) do={ :put "**Debug: $1"; }
2023-01-19 21:27:15 +00:00
}
# 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={
2023-02-19 13:53:15 +00:00
:local ErrorMsg "DnsResolveFunc: arg value $WebSiteName \
not a valid string!";
2023-01-19 21:27:15 +00:00
$LogMessageFunc $ErrorMsg;
$DebugFunc $ErrorMsg;
:return "*** test failed ***";
} else={
$DebugFunc "Data type looks good";
}
:do {
$DebugFunc "Looking up IP of site name...";
2023-02-26 14:05:58 +00:00
:local SiteIpAddr [:resolve $WebSiteName];
2023-01-19 21:27:15 +00:00
$DebugFunc $SiteIpAddr;
:return $SiteIpAddr;
} on-error={
$DebugFunc "DNS lookup failed!";
2023-02-19 13:53:15 +00:00
$LogMessageFunc ("DnsResolveFunc: name resolution failed for \
site: $WebSiteName!");
2023-01-19 21:27:15 +00:00
: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={
2023-02-19 13:53:15 +00:00
$LogMessageFunc ("PingIpAddressFunc: arg value $IpAddress not \
an IP address!");
2023-01-19 21:27:15 +00:00
:return "*** test failed ***";
}
:do {
return [/ping $IpAddress count=3];
} on-error {
2023-02-19 13:53:15 +00:00
$LogMessageFunc ("PingIpAddressFunc: ping test to IP address \
failed: $IpAddress !");
2023-01-19 21:27:15 +00:00
: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
2023-02-19 13:53:15 +00:00
if (([:typeof $SiteName] != "str") or \
([:len value=$SiteName] < 6)) do={
$LogMessageFunc ("GetWebPageFunc: arg value $SiteName not a \
valid string!");
2023-01-19 21:27:15 +00:00
:return { "duration"="*** test failed ***" };
}
do {
2023-02-19 13:53:15 +00:00
return [/tool fetch url=("https://$SiteName") mode=https \
http-method=get as-value keep-result=no];
2023-01-19 21:27:15 +00:00
} on-error {
2023-02-19 13:53:15 +00:00
$LogMessageFunc ("GetWebPageFunc: unable to retrieve site: \
$SiteName !");
2023-01-19 21:27:15 +00:00
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 LogMessageFunc;
:set DnsResolveFunc;
:set PingIpAddressFunc;
:set GetWebPageFunc;
:set DEBUG;
:set DebugFunc;
:set Filename;