Community discussions

MikroTik App
 
jonah1810
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 62
Joined: Tue Jul 30, 2019 10:19 pm

A Better Wireless Auto Frequency Selection

Mon May 10, 2021 5:26 am

Hello, since Mikrotik's frequency=auto is so terrible (it chooses Channel with least amount of AP's Regardless of Signal) I have created one that selects channel based off signal.

My original 2.4ghz script that i posted here was incomplete and as such I have taken it down. In its stead is a 5Ghz script that I am pretty sure has only one flaw and that is if another 5ghz router has channels with 5mhz or 10mhz channel width it wont read through it properly. as it is not possible to set 5mhz or 10mhz channel width in mikrotik routers I can not write it to look for them. I know I have seen 5mhz channels before on a scan with one of my outdoor antennas before though.

so if you are able to get a scan with 5mhz or 10mhz widths please post the file on here so i can alter the script to accommodate for them.

by default the channel exclusion list found three times(one for 20mhz, one for 40mhz, one for 80mhz) in the end section is set for canada country. you must change this setting to comply with your countries frequency regulations

this script requires you to have 5ghz interface named as "wlan2" for people with triband audiences this will only work on 1 of your 5ghz interfaces.

How to use:
1. Select Desired Channel width on wlan2 interface. must be ce or ceee for 40mhz or 80mhz channels respectively
2. /interface wireless wlan2 scan duration=30s save-file=Scan5.rsc
3. Edit Script Exclusions (last part of script) to match your countries frequency regulations.
3. Execute Script.



tested 6.48 (Hapac3)
{
	:local Scan
	:local ScanLine
	:local LineEnd
	:local Scans [:toarray ""]
	:local ScansSignal
	:local CurrentChannel value=0
	:local Channel [:toarray {5180;5200;5220;5240;5260;5280;5300;5320;5340;5360;5380;5400;5420;5440;5480;5500;5520;5540;5560;5580;5600;5620;5640;5660;5680;5700;5720;5745;5765;5785;5805;5825}]
	:local HighestSignal [:toarray {-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100}]
	:local BestChannel
	:local z value=0
	:local i value=0
	#Set Array Of Scanned AP's CurrentChannel and signal
	:set $Scan value=[/file get Scan5-test.rsc contents]


	while ($z = 0)  do={
		:set $LineEnd value=[find $Scan "\n"]
		:set $ScanLine value=[pick $Scan 0 $LineEnd]
			foreach y in={"802.11";"nstreme";"nv2"} do={
				foreach x in={"/ac";"/an/";"/a/";"/a("} do={
					if ($ScanLine~"$y" && $ScanLine~"$x") do={
						:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "$x"]]
						:set $ScansSignal value=[:pick $ScanLine [find $ScanLine ",-"] ([find $ScanLine "$y"] -1)]
					}
				}
			}
		:set ($Scans->$i) value=[($Scans->($i) . ($ScansSignal))]
		:set $Scan value=[:pick $Scan ($LineEnd +1) 999999999]
			if ([:len $Scan] > 20) do={
				:set $i value=($i + 1)
			} else={
				:set $z value=1
			}
		}

		#Find HighestSignal On each channel based on channel width, loops twice to account for newly added channels

	:set $z value=(0)
	while ($z < 2) do={
		foreach k,v in=$Scans do={
	
			#For Ceee Channels
	
			if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 8 12] = "Ceee")) do={
				if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 1))) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 2))) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 3))) \
				) do={
					:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel + 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel + 2)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel + 3)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				}
			}

			#for eCee
			if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 8 12] = "eCee")) do={
				if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 1))) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 1))) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 2))) \
				) do={
					:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel - 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel + 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel + 2)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				}
			}
			#for eeCe
			if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 8 12] = "eeCe")) do={
				if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 1))) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 2))) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 1))) \
				) do={
					:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel - 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel - 2)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel + 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				}
			}
  
			#for eeeC
			if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 8 12] = "eeeC")) do={
				if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 1))) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 2))) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 3))) \
				) do={
					:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					set ($HighestSignal->($CurrentChannel - 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel - 2)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel - 3)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				}
			}
			#For Ce
			if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 8 12] = "Ce,-")) do={
				if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 1))) \
				) do={
					:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel + 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				} 
			}
			#For eC
			if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 8 12] = "Ce,-")) do={
				if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 1 ))) \
				) do={
					:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel + 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				} 
			}
			#For 20MHZ Wide Channels
			if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 5 8] = "20,")) do={
				if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
				) do={
					:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				}
			}
		}
		if (([:len $Channel] -1) = $CurrentChannel) do={
			[:set $z value=($z + 1)]
			:set $CurrentChannel value=0
		} else={
			:set $CurrentChannel value=[($CurrentChannel +1)]
		}
	}

	#Select Best Channel Based on Signal, Automatically chooses channelwidth based off of currently set channel width
	#Default Setting is for Canada country setting, Skipping DFS Channels but you can change the excluded channels lists to fit your needs
	#You must change the excluded channel in whichever channel-width you want to suit your needs in the script below, or all three.
	:local Bool
	if ([/interface wireless get wlan2 channel-width] = "20mhz") do={
		foreach k,v in=$HighestSignal do={
			set $Bool value="true"
			foreach x,i in=$Channel do={
	# add Exclusion Channels on second line from here ie."&& $k != 1" 0=5180 1=5200...etc
				if (($v <= ($HighestSignal->$x))&& ($Bool = "true") \
				&& ($k != 7 && $k != 8 && $k != 9 && $k != 10 && $k != 11 && $k != 12 && $k != 13 && $k != 14 && $k != 15 && $k != 16 && $k != 17 && $k != 18 && $k != 19 && $k != 20 && $k != 21 && $k != 22 && $k != 23 && $k != 24 && $k != 25 && $k != 26) \
				) do={
					:set $Bool value="true"
				} else={
					:set $Bool value="false"
				}
			}
			if ($Bool = "true") do={
				:log warning message=($Channel->$k)
				:set $BestChannel value=($Channel->$k)
			}
		}
		/interface wireless set wlan2 frequency=$BestChannel
	}
	
	if ([/interface wireless get wlan2 channel-width] = "20/40mhz-Ce") do={
		foreach k,v in=$HighestSignal do={
			:set $Bool value="true"
			foreach x,i in=$Channel do={
	# add Exclusion Channels on second line from here ie "&& $k != 1" 0=5180 1=5200...etc
				if (($v <= ($HighestSignal->$x)) && (($HighestSignal->($k + 1)) <= ($HighestSignal->$x)) && ($Bool = "true") \
				&& ($k != 7 && $k != 8 && $k != 9 && $k != 10 && $k != 11 && $k != 12 && $k != 13 && $k != 14 && $k != 15 && $k != 16 && $k != 17 && $k != 18 && $k != 19 && $k != 20 && $k != 21 && $k != 22 && $k != 23 && $k != 24 && $k != 25 && $k != 26 && $k != 31) \
				) do={
					:set $Bool value="true"
				} else={
					:set $Bool value="false"
				}
			}
			if ($Bool = "true") do={
				:log warning message=($Channel->$k)
				:set $BestChannel value=($Channel->$k)
			}
		}
		/interface wireless set wlan2 frequency=$BestChannel
	}

	if ([/interface wireless get wlan2 channel-width] = "20/40/80mhz-Ceee") do={
		foreach k,v in=$HighestSignal do={
			:set $Bool value="true"
			foreach x,i in=$Channel do={
	# add Exclusion Channels on second line from here ie "&& $k != 1" 0=5180 1=5200...etc
			if (($v <= ($HighestSignal->$x)) && (($HighestSignal->($k + 1)) <= ($HighestSignal->$x)) && (($HighestSignal->($k + 2)) <= ($HighestSignal->$x)) && (($HighestSignal->($k + 3)) <= ($HighestSignal->$x)) && ($Bool = "true") \
			&& ($k != 5 && $k != 6 && $k != 7 && $k != 8 && $k != 9 && $k != 10 && $k != 11 && $k != 12 && $k != 13 && $k != 14 && $k != 15 && $k != 16 && $k != 17 && $k != 18 && $k != 19 && $k != 20 && $k != 21 && $k != 22 && $k != 23 && $k != 24 && $k != 25 && $k != 26 && $k != 29 && $k != 30 && $k != 31) \
			) do={
				:set $Bool value="true"
			} else={
				:set $Bool value="false"
			}
		}
		if ($Bool = "true") do={
			:log warning message=($Channel->$k)
			:set $BestChannel value=($Channel->$k)
		}
	}
		/interface wireless set wlan2 frequency=$BestChannel
	}
}
Last edited by jonah1810 on Sun Jun 13, 2021 10:09 am, edited 10 times in total.
 
jonah1810
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 62
Joined: Tue Jul 30, 2019 10:19 pm

Re: A Better Wireless Auto Frequency Selection

Sat Jun 12, 2021 9:48 am

added complete 5ghz script and removed 2.4ghz script. will update with a complete version soon.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 2164
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: A Better Wireless Auto Frequency Selection

Sat Jun 12, 2021 10:43 am

Do you need all variable to be global? if not use local.

Here it the same version but with tab for all groups. (gives better reading)
:log warning message=[:time {
	:global Scan
	:global ScanLine
	:global LineEnd
	:global Scans [:toarray ""]
	:global ScansSignal
	:global CurrentChannel value=0
	:global Channel [:toarray {5180;5200;5220;5240;5260;5280;5300;5320;5340;5360;5380;5400;5420;5440;5480;5500;5520;5540;5560;5580;5600;5620;5640;5660;5680;5700;5720;5745;5765;5785;5805;5825}]
	:global HighestSignal [:toarray {-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100}]
	:global BestChannel
	:global z value=0
	:global i value=0
	#Set Array Of Scanned AP's CurrentChannel and signal
	:set $Scan value=[/file get Scan5.rsc contents]


	while ($z = 0)  do={
	:set $LineEnd value=[find $Scan "\n"]
	:set $ScanLine value=[pick $Scan 0 $LineEnd]

	if ($ScanLine~"802.11") do={
		if ($ScanLine~"/ac") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/ac"]]
		}
		if ($ScanLine~"/an/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/an"]]
		}
		if ($ScanLine~"/a/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/a/"]]
		}
		if ($ScanLine~"/a(") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/a("]]
		}
			:set $ScansSignal value=[:pick $ScanLine [find $ScanLine ",-"] [find $ScanLine ",802.11"]]
			:set ($Scans->$i) value=[($Scans->($i) . ($ScansSignal))]
	}

	if ($ScanLine~"nv2") do={
		if ($ScanLine~"/ac") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/ac"]]
		}
		if ($ScanLine~"/an/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/an"]]
		}
		if ($ScanLine~"/a/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/a/"]]
		}
		if ($ScanLine~"/a(") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/a("]]
		}
			:set $ScansSignal value=[:pick $ScanLine [find $ScanLine ",-"] [find $ScanLine ",nv2"]]
			:set ($Scans->$i) value=[($Scans->($i) . ($ScansSignal))]
	}

	if ($ScanLine~"nstreme") do={
		if ($ScanLine~"/ac/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/ac/"]]
		}
		if ($ScanLine~"/an/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/an/"]]
		}
		if ($ScanLine~"/a/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/a/"]]
		}
		:set $ScansSignal value=[:pick $ScanLine [find $ScanLine ",-"] [find $ScanLine ",nstreme"]]
		:set ($Scans->$i) value=[($Scans->($i) . ($ScansSignal))]
	}
	:set $Scan value=[:pick $Scan ($LineEnd +1) 999999999]
	if ([:len $Scan] > 20) do={
		:set $i value=($i + 1)
	} else={
	:set $z value=1}
	}

	# Find HighestSignal Signal On each channel based on channel width, loops twice to account for newly added channels

	:set $z value=(0)
	while ($z < 2) do={
		foreach k,v in=$Scans do={

			#For Ceee Channels

			if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 8 12] = "Ceee")) do={
				if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 1))) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 2))) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 3))) \
				) do={
					:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel + 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel + 2)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel + 3)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				}
			}

			#for eCee
			if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 8 12] = "eCee")) do={
				if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 1))) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 1))) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 2))) \
				) do={
					:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel - 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel + 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel + 2)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				}
			}
			#for eeCe
			if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 8 12] = "eeCe")) do={
				if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 1))) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 2))) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 1))) \
				) do={
					:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel - 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel - 2)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel + 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				}
			}

			#for eeeC
			if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 8 12] = "eeeC")) do={
				if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 1))) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 2))) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 3))) \
				) do={
					:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel - 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel - 2)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel - 3)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				}
			}
			#For 40Mhz Wide Channels
			if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 8 12] = "Ce,-")) do={
				if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 1))) \
				) do={
					:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
					:set ($HighestSignal->($CurrentChannel + 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				} 
			}
			#For 20MHZ Wide Channels
			if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 5 8] = "20,")) do={
				if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
				&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
				) do={
					:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				}
			}
		}
		if (([:len $Channel] -1) = $CurrentChannel) do={
			[:set $z value=($z + 1)]
			:set $CurrentChannel value=0
		} else={
			:set $CurrentChannel value=[($CurrentChannel +1)]
		}
	}

	#Select Best Channel Based on Signal, Automatically chooses channelwidth based off of currently set channel width
	#Default Setting is for Canada country setting, Skipping DFS Channels but you can change the excluded channels lists to fit your needs
	#You must change the excluded channel in whichever channel-width you want to suit your needs in the script below, or all three.
	:global Bool
	if ([/interface wireless get wlan2 channel-width] = "20mhz") do={
		foreach k,v in=$HighestSignal do={
			:set $Bool value="true"
			foreach x,i in=$Channel do={
				# add Exclusion Channels here on second line ie from here"&& $k != 1" 0=5180 1=5200...etc
				if (($v <= ($HighestSignal->$x))&& ($Bool = "true") \
				&& ($k != 7 && $k != 8 && $k != 9 && $k != 10 && $k != 11 && $k != 12 && $k != 13 && $k != 14 && $k != 15 && $k != 16 && $k != 17 && $k != 18 && $k != 19 && $k != 20 && $k != 21 && $k != 22 && $k != 23 && $k != 24 && $k != 25 && $k != 26) \
				) do={
					:set $Bool value="true"
				} else={
					:set $Bool value="false"
				}
			}
			if ($Bool = "true") do={
				:log warning message=($Channel->$k)
				:set $BestChannel value=($Channel->$k)
			}
		}
		/interface wireless set wlan2 frequency=$BestChannel
	}
	if ([/interface wireless get wlan2 channel-width] = "20/40mhz-Ce") do={
		foreach k,v in=$HighestSignal do={
			:set $Bool value="true"
			foreach x,i in=$Channel do={
				# add Exclusion Channels here on second line from here ie "&& $k != 1" 0=5180 1=5200...etc
				if (($v <= ($HighestSignal->$x)) && (($HighestSignal->($k + 1)) <= ($HighestSignal->$x)) && ($Bool = "true") \
				&& ($k != 7 && $k != 8 && $k != 9 && $k != 10 && $k != 11 && $k != 12 && $k != 13 && $k != 14 && $k != 15 && $k != 16 && $k != 17 && $k != 18 && $k != 19 && $k != 20 && $k != 21 && $k != 22 && $k != 23 && $k != 24 && $k != 25 && $k != 26 && $k != 31) \
				) do={
					:set $Bool value="true"
				} else={
					:set $Bool value="false"
				}
			}
			if ($Bool = "true") do={
				:log warning message=($Channel->$k)
				:set $BestChannel value=($Channel->$k)
			}
		}
		/interface wireless set wlan2 frequency=$BestChannel
	}

	if ([/interface wireless get wlan2 channel-width] = "20/40/80mhz-Ceee") do={
		foreach k,v in=$HighestSignal do={
			:set $Bool value="true"
			foreach x,i in=$Channel do={
				# add Exclusion Channels here on second line from here ie "&& $k != 1" 0=5180 1=5200...etc
				if (($v <= ($HighestSignal->$x)) && (($HighestSignal->($k + 1)) <= ($HighestSignal->$x)) && (($HighestSignal->($k + 2)) <= ($HighestSignal->$x)) && (($HighestSignal->($k + 3)) <= ($HighestSignal->$x)) && ($Bool = "true") \
				&& ($k != 5 && $k != 6 && $k != 7 && $k != 8 && $k != 9 && $k != 10 && $k != 11 && $k != 12 && $k != 13 && $k != 14 && $k != 15 && $k != 16 && $k != 17 && $k != 18 && $k != 19 && $k != 20 && $k != 21 && $k != 22 && $k != 23 && $k != 24 && $k != 25 && $k != 26 && $k != 29 && $k != 30 && $k != 31) \
				) do={
					:set $Bool value="true"
				} else={
					:set $Bool value="false"
				}
			}
			if ($Bool = "true") do={
				:log warning message=($Channel->$k)
				:set $BestChannel value=($Channel->$k)
			}
		}
		/interface wireless set wlan2 frequency=$BestChannel
	}
}
]
Last edited by Jotne on Sat Jun 12, 2021 10:58 am, edited 2 times in total.
 
Try Splunk> to monitor your MikroTik Router(s). Look at this page in how to set it up.

MikroTik->Splunk
 
 
jonah1810
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 62
Joined: Tue Jul 30, 2019 10:19 pm

Re: A Better Wireless Auto Frequency Selection

Sat Jun 12, 2021 10:56 am

Do you need all variable to be global? if not use local.
oops, i forgot to change that back, i only had it global to easily read values in winbox!
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 2164
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: A Better Wireless Auto Frequency Selection

Sat Jun 12, 2021 11:02 am

It seems that it may be shorten some, like
if ($ScanLine~"802.11") do={
if ($ScanLine~"nv2") do={
if ($ScanLine~"nstreme") do={

Looks equal, and may be shorten to run one group test 3 times with different input instead of 3 different group test.

Example
foreach i in=[:toarray "802.11,nv2,nstreme"]  do={:put $i}
 
Try Splunk> to monitor your MikroTik Router(s). Look at this page in how to set it up.

MikroTik->Splunk
 
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 2164
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: A Better Wireless Auto Frequency Selection

Sat Jun 12, 2021 4:41 pm

you must create a scan file called "Scan5.rsc" with preferably a full scan-list of 5180-5825.
How? I do not understand what to do to create the file.

Here is a re post with correct tabs. (you have a mix of spaces and tabs in front of lines, makes i hard to see each section.)
{
:local Scan
:local ScanLine
:local LineEnd
:local Scans [:toarray ""]
:local ScansSignal
:local CurrentChannel value=0
:local Channel [:toarray {5180;5200;5220;5240;5260;5280;5300;5320;5340;5360;5380;5400;5420;5440;5480;5500;5520;5540;5560;5580;5600;5620;5640;5660;5680;5700;5720;5745;5765;5785;5805;5825}]
:global HighestSignal [:toarray {-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100;-100}]
:global BestChannel
:global z value=0
:global i value=0
#Set Array Of Scanned AP's CurrentChannel and signal
:set $Scan value=[/file get Scan5.rsc contents]


while ($z = 0)  do={
	:set $LineEnd value=[find $Scan "\n"]
	:set $ScanLine value=[pick $Scan 0 $LineEnd]

	if ($ScanLine~"802.11") do={
		if ($ScanLine~"/ac") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/ac"]]
		}
		if ($ScanLine~"/an/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/an"]]
		}
		if ($ScanLine~"/a/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/a/"]]
		}
		if ($ScanLine~"/a(") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/a("]]
		}
			:set $ScansSignal value=[:pick $ScanLine [find $ScanLine ",-"] [find $ScanLine ",802.11"]]
			:set ($Scans->$i) value=[($Scans->($i) . ($ScansSignal))]
	}

	if ($ScanLine~"nv2") do={
		if ($ScanLine~"/ac") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/ac"]]
		}
		if ($ScanLine~"/an/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/an"]]
		}
		if ($ScanLine~"/a/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/a/"]]
		}
		if ($ScanLine~"/a(") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/a("]]
		}
			:set $ScansSignal value=[:pick $ScanLine [find $ScanLine ",-"] [find $ScanLine ",nv2"]]
			:set ($Scans->$i) value=[($Scans->($i) . ($ScansSignal))]
	}

	if ($ScanLine~"nstreme") do={
		if ($ScanLine~"/ac/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/ac/"]]
		}
		if ($ScanLine~"/an/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/an/"]]
		}
		if ($ScanLine~"/a/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/a/"]]
		}
			:set $ScansSignal value=[:pick $ScanLine [find $ScanLine ",-"] [find $ScanLine ",nstreme"]]
			:set ($Scans->$i) value=[($Scans->($i) . ($ScansSignal))]
	}
	:set $Scan value=[:pick $Scan ($LineEnd +1) 999999999]
	if ([:len $Scan] > 20) do={
		:set $i value=($i + 1)
	} else={
		:set $z value=1
	}
}

# Find HighestSignal Signal On each channel based on channel width, loops twice to account for newly added channels

:set $z value=(0)
while ($z < 2) do={
	foreach k,v in=$Scans do={

		#For Ceee Channels

		if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 8 12] = "Ceee")) do={
			if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 1))) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 2))) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 3))) \
			) do={
				:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				:set ($HighestSignal->($CurrentChannel + 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				:set ($HighestSignal->($CurrentChannel + 2)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				:set ($HighestSignal->($CurrentChannel + 3)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
			}
		}

		#for eCee
		if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 8 12] = "eCee")) do={
			if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 1))) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 1))) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 2))) \
			) do={
				:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				:set ($HighestSignal->($CurrentChannel - 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				:set ($HighestSignal->($CurrentChannel + 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				:set ($HighestSignal->($CurrentChannel + 2)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
			}
		}
		#for eeCe
		if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 8 12] = "eeCe")) do={
			if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 1))) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 2))) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 1))) \
			) do={
				:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				:set ($HighestSignal->($CurrentChannel - 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				:set ($HighestSignal->($CurrentChannel - 2)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				:set ($HighestSignal->($CurrentChannel + 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
			}
		}

		#for eeeC
		if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 8 12] = "eeeC")) do={
			if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 1))) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 2))) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel - 3))) \
			) do={
				:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				:set ($HighestSignal->($CurrentChannel - 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				:set ($HighestSignal->($CurrentChannel - 2)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				:set ($HighestSignal->($CurrentChannel - 3)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
			}
		}
		#For 40Mhz Wide Channels
		if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 8 12] = "Ce,-")) do={
			if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->($CurrentChannel + 1))) \
			) do={
				:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
				:set ($HighestSignal->($CurrentChannel + 1)) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
			} 
		}
		#For 20MHZ Wide Channels
		if ([:pick [($Scans->$k )] 0 4] = ($Channel->$CurrentChannel) && ([:pick ($Scans->$k) 5 8] = "20,")) do={
			if (([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > -100) \
			&& ([:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20] > ($HighestSignal->$CurrentChannel)) \
			) do={
				:set ($HighestSignal->$CurrentChannel) value=[:pick ($Scans->$k) ([find ($Scans->$k) ",-"] +1) 20]
			}
		}
	}
	if (([:len $Channel] -1) = $CurrentChannel) do={
		[:set $z value=($z + 1)]
		:set $CurrentChannel value=0
	} else={
		:set $CurrentChannel value=[($CurrentChannel +1)]
	}
}

#Select Best Channel Based on Signal, Automatically chooses channelwidth based off of currently set channel width
#Default Setting is for Canada country setting, Skipping DFS Channels but you can change the excluded channels lists to fit your needs
#You must change the excluded channel in whichever channel-width you want to suit your needs in the script below, or all three.
:global Bool
if ([/interface wireless get wlan2 channel-width] = "20mhz") do={
	foreach k,v in=$HighestSignal do={
		:set $Bool value="true"
		foreach x,i in=$Channel do={
			# add Exclusion Channels here on second line ie from here"&& $k != 1" 0=5180 1=5200...etc
			if (($v <= ($HighestSignal->$x))&& ($Bool = "true") \
			&& ($k != 7 && $k != 8 && $k != 9 && $k != 10 && $k != 11 && $k != 12 && $k != 13 && $k != 14 && $k != 15 && $k != 16 && $k != 17 && $k != 18 && $k != 19 && $k != 20 && $k != 21 && $k != 22 && $k != 23 && $k != 24 && $k != 25 && $k != 26) \
			) do={
				:set $Bool value="true"
			} else={
				:set $Bool value="false"
			}
		}
		if ($Bool = "true") do={
			:log warning message=($Channel->$k)
			:set $BestChannel value=($Channel->$k)
		}
	}
	/interface wireless set wlan2 frequency=$BestChannel
}
if ([/interface wireless get wlan2 channel-width] = "20/40mhz-Ce") do={
	foreach k,v in=$HighestSignal do={
		:set $Bool value="true"
		foreach x,i in=$Channel do={
			# add Exclusion Channels here on second line from here ie "&& $k != 1" 0=5180 1=5200...etc
			if (($v <= ($HighestSignal->$x)) && (($HighestSignal->($k + 1)) <= ($HighestSignal->$x)) && ($Bool = "true") \
			&& ($k != 7 && $k != 8 && $k != 9 && $k != 10 && $k != 11 && $k != 12 && $k != 13 && $k != 14 && $k != 15 && $k != 16 && $k != 17 && $k != 18 && $k != 19 && $k != 20 && $k != 21 && $k != 22 && $k != 23 && $k != 24 && $k != 25 && $k != 26 && $k != 31) \
			) do={
				:set $Bool value="true"
			} else={
				:set $Bool value="false"
			}
		}
		if ($Bool = "true") do={
			:log warning message=($Channel->$k)
			:set $BestChannel value=($Channel->$k)
		}
	}
	/interface wireless set wlan2 frequency=$BestChannel
}

if ([/interface wireless get wlan2 channel-width] = "20/40/80mhz-Ceee") do={
	foreach k,v in=$HighestSignal do={
		:set $Bool value="true"
		foreach x,i in=$Channel do={
			# add Exclusion Channels here on second line from here ie "&& $k != 1" 0=5180 1=5200...etc
			if (($v <= ($HighestSignal->$x)) && (($HighestSignal->($k + 1)) <= ($HighestSignal->$x)) && (($HighestSignal->($k + 2)) <= ($HighestSignal->$x)) && (($HighestSignal->($k + 3)) <= ($HighestSignal->$x)) && ($Bool = "true") \
			&& ($k != 5 && $k != 6 && $k != 7 && $k != 8 && $k != 9 && $k != 10 && $k != 11 && $k != 12 && $k != 13 && $k != 14 && $k != 15 && $k != 16 && $k != 17 && $k != 18 && $k != 19 && $k != 20 && $k != 21 && $k != 22 && $k != 23 && $k != 24 && $k != 25 && $k != 26 && $k != 29 && $k != 30 && $k != 31) \
			) do={
				:set $Bool value="true"
			} else={
				:set $Bool value="false"
			}
		}
		if ($Bool = "true") do={
			:log warning message=($Channel->$k)
			:set $BestChannel value=($Channel->$k)
		}
	}
	/interface wireless set wlan2 frequency=$BestChannel
}
}

Could not this part:
	if ($ScanLine~"802.11") do={
		if ($ScanLine~"/ac") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/ac"]]
		}
		if ($ScanLine~"/an/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/an"]]
		}
		if ($ScanLine~"/a/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/a/"]]
		}
		if ($ScanLine~"/a(") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/a("]]
		}
			:set $ScansSignal value=[:pick $ScanLine [find $ScanLine ",-"] [find $ScanLine ",802.11"]]
			:set ($Scans->$i) value=[($Scans->($i) . ($ScansSignal))]
	}

	if ($ScanLine~"nv2") do={
		if ($ScanLine~"/ac") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/ac"]]
		}
		if ($ScanLine~"/an/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/an"]]
		}
		if ($ScanLine~"/a/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/a/"]]
		}
		if ($ScanLine~"/a(") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/a("]]
		}
			:set $ScansSignal value=[:pick $ScanLine [find $ScanLine ",-"] [find $ScanLine ",nv2"]]
			:set ($Scans->$i) value=[($Scans->($i) . ($ScansSignal))]
	}

	if ($ScanLine~"nstreme") do={
		if ($ScanLine~"/ac/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/ac/"]]
		}
		if ($ScanLine~"/an/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/an/"]]
		}
		if ($ScanLine~"/a/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/a/"]]
		}
			:set $ScansSignal value=[:pick $ScanLine [find $ScanLine ",-"] [find $ScanLine ",nstreme"]]
			:set ($Scans->$i) value=[($Scans->($i) . ($ScansSignal))]
	}
be shorten to?
	if ($ScanLine~"(802.11|nv2|nstreme)") do={
		if ($ScanLine~"/ac/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/ac/"]]
		}
		if ($ScanLine~"/an/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/an/"]]
		}
		if ($ScanLine~"/a/") do={
			:set ($Scans->$i) value=[:pick $ScanLine ([find $ScanLine ",5"] + 1) [find $ScanLine "/a/"]]
		}
			:set $ScansSignal value=[:pick $ScanLine [find $ScanLine ",-"] [find $ScanLine ",nstreme"]]
			:set ($Scans->$i) value=[($Scans->($i) . ($ScansSignal))]
	}
 
Try Splunk> to monitor your MikroTik Router(s). Look at this page in how to set it up.

MikroTik->Splunk
 
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 3927
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: A Better Wireless Auto Frequency Selection

Sat Jun 12, 2021 7:29 pm

<tag>
Sorry, but I stop help on the forum.
Thanks to BartoszP who made me make this decision. Moderators should be more polite.
I do not remove anything I have already posted, hope that still help the others.
 
jonah1810
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 62
Joined: Tue Jul 30, 2019 10:19 pm

Re: A Better Wireless Auto Frequency Selection

Sat Jun 12, 2021 11:56 pm

Jotne use the command:
/interface wireless wlan2 scan duration=30s save-file=Scan5.rsc
While i cant do what you suggest in your newest post because of the line
":set $ScansSignal value=[:pick $ScanLine [find $ScanLine ",-"] [find $ScanLine ",nstreme"]]"
I was able to shorten it down into 2 foreach loops. I edited the OP to have the changes!

I like how you refactored the script to make it more readable but for some reason it stops working when I use the refactored version.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 2164
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: A Better Wireless Auto Frequency Selection

Sun Jun 13, 2021 8:56 am

You cant change to use an variable?
:set $ScansSignal value=[:pick $ScanLine [find $ScanLine ",-"] [find $ScanLine $i]]

You are still mixing space and tabs in front of lines. And you do miss tabs for several groups.
Every time some starts with {, rest should be tabbed in one
 
Try Splunk> to monitor your MikroTik Router(s). Look at this page in how to set it up.

MikroTik->Splunk
 
 
jonah1810
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 62
Joined: Tue Jul 30, 2019 10:19 pm

Re: A Better Wireless Auto Frequency Selection

Sun Jun 13, 2021 10:18 am

Well that is exactly what I am doing now. I just mean i cant use
if ($ScanLine~"(802.11|nv2|nstreme)")

because it will come back true and and then
:set $ScansSignal value=[:pick $ScanLine [find $ScanLine ",-"] ([find $ScanLine "$y"] -1)]
will have a chance of setting a blank variable instead of the signal. depending on which variable was last in loop.
but the nested foreach loops work just fine and are much neater than what I had before!

I refactored it aswell so it is easier to read.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 2164
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: A Better Wireless Auto Frequency Selection

Sun Jun 13, 2021 3:55 pm

I did not tell you to put all in one if, that will not work, but run it 3 times
foreach i in=[:toarray "802.11,nv2,nstreme"]  do={
	some code
		if ($ScanLine~$i) do={
			:set $ScansSignal value=[:pick $ScanLine [find $ScanLine ",-"] ([find $ScanLine "$i"] -1)]
		}
}
 
Try Splunk> to monitor your MikroTik Router(s). Look at this page in how to set it up.

MikroTik->Splunk
 

Who is online

Users browsing this forum: yawadebian and 25 guests