Skip to content

Instantly share code, notes, and snippets.

@elico
Last active April 10, 2024 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elico/49f22bf496f13928e1bbc960c313d2e7 to your computer and use it in GitHub Desktop.
Save elico/49f22bf496f13928e1bbc960c313d2e7 to your computer and use it in GitHub Desktop.
A demo script to detect by url if a specific interface is up.
{
:do {
:local srcInterface1 "ether1-WAN1";
:local srcInterface2 "ether2-WAN2";
:local srcInterface3 "ether3-LTE";
:local srcInterface4 "pppoe-WAN1";
:local srcInterface5 "pppoe-WAN2";
:local srcInterface6 "pppoe-LTE1";
:local flagInterface "";
:set flagInterface $flagInterface3;
:local srcAddressCidr [/ip/address/get [find where interface=ether3-LTE ] address ];
:local srcAddress "";
:local urlString1 "https://ipconfig.io/country";
:local urlString2 "http://www.msftncsi.com/ncsi.txt";
:local urlString3 "https://connectivitycheck.gstatic.com";
:local urlString4 "https://www.apple.com/library/test/success.html";
:local result "";
:local flagInterface1 "lte1-flag1";
:local flagInterface2 "lte1-flag2";
:local flagInterface3 "lte1-flag3";
:local flagInterface "";
:set flagInterface $flagInterface1;
:do {
if ([/interface/bridge/print count-only where name=$flagInterface ] = 0) do={
[/interface/bridge/add name=$flagInterface disabled=yes];
}
} on-error={
:log info "Error creating the flag interface";
}
:local currentFlagDisabledState [/interface/bridge/get [find where name=$flagInterface ] value-name=disabled ];
:for i from=( [:len $srcAddressCidr] - 1) to=0 do={
:if ( [:pick $srcAddressCidr $i] = "/") do={
:set srcAddress [:pick $srcAddressCidr 0 $i];
}
}
:do {
:set result [/tool/fetch src-address=$srcAddress url=$urlString1 as-value output=none];
if ($currentFlagDisabledState = true) do={
[/interface/bridge/set [find where name=$flagInterface ] disabled=no ];
}
} on-error={
:log info ("Error downloading url: " . $urlString1);
if ($currentFlagDisabledState = false) do={
[/interface/bridge/set [find where name=$flagInterface ] disabled=yes];
}
}
:log info "success";
} on-error={
:log info "Runtime Error";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment