API to select best AP for SU

would like to run scan in a Subscriber Unit (SU)
to determine the signal strengths of the various Access Point (AP) seen by the SU
using:
“routeros_api.class.php”

  • RouterOS PHP API class v1.0
  • Author: Denis Basta

MikroTik v5.14

function get_signals($ip) {
        $ip = trim($ip);
        if($ip != "" && isup($ip)) {
                $API = new routeros_api();
                echo("<br>ip $ip ok");
                if(connect_radio_client($API,$ip)) {
                        echo("<br>connected OK");
                        $interfacetable = $API->comm("/interface/wireless/print");
                        if(isset($interfacetable)) {
                                echo("<br>interface table is set<br>");
                                echo("<br>interfaces<br>");
                                foreach ($interfacetable as $entry) {
                                        foreach ($entry as $key => $value) {
                                                echo "<br>Key: $key; Value: $value\n";
                                        }
                                }
                                $result = $API->comm("\
/interface/wireless/scan
=.id=wlan1
=duration=6");
                                return $result;
                        }
                }
        }
}

wireless interface table prints OK
no errors in error_log
returns empty array

suggestions welcome!

Probably because when the SU scans for occupancy of the spectrum it disconnects from your server and therefore as you no longer have communication with the SU, it returns no data ?

do not think so
works AOK via telnet CLI
SU does go away for the scan but come right back after scan

Yes, Telnet WILL work, but try connecting to the SU with winbox or SSH while it’s scanning. I am sure it will disconnect you. I find Telnet is insecure, but very good for making a remote scan. All other and more secure connections always drop before the scan is complete I find.

you are correct that the SU must use the radio on other channels for the scan and thus is off line during the scan of all channels

not trying to be argumentative but complete
the telnet/IP session does not drop (timeout) during the scan even though the SU is off line = incommunicado

proof:
1.
ping during scan:
64 bytes from 10.106.41.46: icmp_seq=10 ttl=58 time=14.9 ms
64 bytes from 10.106.41.46: icmp_seq=11 ttl=58 time=16.2 ms
64 bytes from 10.106.41.46: icmp_seq=25 ttl=58 time=33.1 ms
64 bytes from 10.106.41.46: icmp_seq=26 ttl=58 time=16.4 ms
64 bytes from 10.106.41.46: icmp_seq=27 ttl=58 time=16.8 ms

here SU was off line for 25 - 11 = 14 seconds but IP session did not drop

the scan results were returned in telnet session with
no second log in required
3.
exactly same behavior with ssh

shame you are not using telnet then! Like I said, I do not believe the api command will ever complete as the scan will break the api connection. Which I believe is the reason why you cannot make it work with api.

You see the problem is I am lazy and I have been using telnet and ssh for automation of device management but I am tired or parsing the replies and some replies are truncated apparently because of the line length control of telnet. In particular the scan results via the perl telnet module are truncated.

I would like to use API because when it does work, the parsing is done for you!

A hymn to Saraswati, the Goddess of Knowledge:
Wonderful is your gift of knowledge
the more we share, the more it grows
the more we hoard it, the more it diminishes

if you have to do once - fr initial setup, you could attempt to create custom OpenWRT image and run API tool connecting to router from within the router and do scan, and get all the data, then connect to best AP and upload data to server. If you have several routers behind wireless link, you could run it on one router with most resources and thus - manage the wireless clients.

project is to manage thousands of SUs
in a dense network where each SU may see several APs
some see as many as eight APs!

idea is to slowly scan network make sure best AP is selected
things in the Fresnel zone move, trees grow etc.

If API won’t work, then maybe use telnet - would have to write once a parser as solve problem of truncation of replies - see example below.
Of maybe a script resident in the SU that could be queried by NOS?
Or maybe SU on its own selects best AP (somewhat complicated by SUs that have static public IPs)


Example of telnet truncation:
partial reply to $command = “/interface wireless scan 0 duration=6”;

debug output - telnet returned line quoted with <>

 96: line <       ADDRESS           SSID        BAND   CHA.. FREQ SIG   NF SNR RADIO-NAME >
 96: line <A R  B D4:CA:6D:11:5A:C9 Broadban... 5ghz-n 20mhz 5180 -79 -120  41            >
 98: line <A R  B D4:CA:6D:11:5A:C9 Broadban... 5ghz-n 20mhz 5180 -79 -120  41            >
 96: line <A R  B D4:CA:6D:11:5A:C6 Broadban... 5ghz-n 20mhz 5200 -73 -120  47 AP Crwn_E2 >
 98: line <A R  B D4:CA:6D:11:5A:C6 Broadban... 5ghz-n 20mhz 5200 -73 -120  47 AP Crwn_E2 >
 96: line <A R  B D4:CA:6D:11:5A:D2 Broadban... 5ghz-n 20mhz 5240 -68 -120  52 AP Crown...>

what does it means “best”?
best signal ? best ccq? best snr?
and wath if all 200 SU of one zone select only one AP as best? can you avoid this?

Isn’t the empty list an API bug?

I mean… if the API connection was being terminated after the scan command was issued, shouldn’t this cause an error message/exception/something on PHP’s end? A “!fatal” response at least?

you will receive !fatal if connection is running and there is router fault and connection cannot be held open. If connection is lost, then API reacts to that and closes it. As i wrote before, you can have something like metarouter running your custom code that would be able to connect from within the rotuer, issue scan or whatever operations that will cause router to go offline, gather results and then resume. Gathered data will reside on MetaROUTER guest, or you can automatically cause guest to adjust settings on router and set up wireless connect-list as required.

And you will need OpenWRT or similar to run custom code on it. Just note the requirements for cross-compilation so that your code actually runs on inside it.

But that’s just it - an empty result list means RouterOS closed the connection for some reason, without giving any kind of response (being !fatal, or !trap) to explain why. If the connection cannot be held opened due to a design requirement, I’d expect a !fatal or !trap with a message telling me something like “Connection is terminated in order for the command to finish”.

BTW, I also noticed one more thing…

@kp2a

                                $result = $API->comm("\
/interface/wireless/scan
=.id=wlan1
=duration=6");
                                return $result; 

needs to instead be

                                $result = $API->comm("/interface/wireless/scan",
                                    array(
                                        '.id' => 'wlan1',
                                        'duration' => '6'
                                    )
                                );
                                return $result; 

http://forum.mikrotik.com/posting.php?mode=reply&f=9&t=68102&sid=7e948f11232d209d654f1c2b907f2c36#

that did it!
that is why an extra pair of eyes are important!
thanks!
works fine!