Skip to content

Instantly share code, notes, and snippets.

@roman01la
Last active February 6, 2026 14:43
Show Gist options
  • Select an option

  • Save roman01la/377fdb4c40eb5a006c741a2311e6b008 to your computer and use it in GitHub Desktop.

Select an option

Save roman01la/377fdb4c40eb5a006c741a2311e6b008 to your computer and use it in GitHub Desktop.
MikroTik Metal 52 ac: Stable 2.4 GHz Link
/system script add name=auto-channel-boot policy=read,write,test source={
:local iface "wlan1";
:local cand {2412;2437;2462};
:local bestFreq 2437;
:local bestScore 999999;
:global autoChanDone;
:if ([:len $autoChanDone] > 0 && $autoChanDone = true) do={
:log info "[auto-chan] already done, skipping";
:return;
}
:local gotClient false;
:for i from=1 to=120 do={
:local n [/interface wireless registration-table print count-only];
:if ($n > 0) do={ :set gotClient true; :set i 120; } else={ :delay 1; }
}
:if ($gotClient) do={
:log warning "[auto-chan] client detected, scanning (brief disconnect possible)...";
} else={
:log warning "[auto-chan] no client after 120s, scanning anyway...";
}
:set autoChanDone true;
:local res [/interface wireless scan $iface duration=5];
:foreach f in=$cand do={
:local score 0;
:foreach r in=$res do={
:local ch ($r->"channel");
:if ([:len $ch] = 0) do={ :set ch ($r->"CHANNEL"); }
:local slash [:find $ch "/"];
:if ($slash != nil) do={
:local rf [:tonum [:pick $ch 0 $slash]];
:local d ($rf - $f); :if ($d < 0) do={ :set d (0 - $d); }
:if ($d < 25) do={
:local sig ($r->"sig");
:if ([:len $sig] = 0) do={ :set sig ($r->"signal-strength"); }
:if ([:len $sig] = 0) do={ :set sig -100; }
:set score ($score + 10);
:local p (90 + $sig);
:if ($p < 0) do={ :set p 0; }
:set score ($score + $p);
}
}
}
:log info ("[auto-chan] freq " . $f . " score " . $score);
:if ($score < $bestScore) do={ :set bestScore $score; :set bestFreq $f; }
}
:log warning ("[auto-chan] choosing " . $bestFreq . " score=" . $bestScore);
/interface wireless set $iface frequency=$bestFreq;
}
{paste above script}

# run at boot
/system scheduler add name=run-auto-chan-after-client start-time=startup on-event=auto-channel-after-client policy=read,write,test

# test run
/system script run auto-channel-after-client
/system script remove [find name="auto-channel-after-client"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment