[HOW TO] Remote Scan using API

Hello guys,
i realized a PHP script to do a remote scan using PHP Pear API for RouterOS and I want to share it with you.

<?php
set_time_limit(0);
$userIp = '0.0.0.0'; //ip here
$username = 'username'; //password here
$password = 'password'; //username here
$wlan = 0; //wlan id here
$scan_duration = 6; //Duration of scan in seconds here
$connection_timeout = 30; //API connection timeout here (set it bigger than scan_duration)

$client = new RouterOS\Client($userIp, $username, $password, null, false, $connection_timeout, Transmitter\NetworkStream::CRYPTO_TLS);
$setRequest = new RouterOS\Request('/interface/wireless/scan');
$responses = $client->sendSync($setRequest
                ->setArgument('duration', 6)
                ->setArgument('number', $wlan)
                );
$networks = array();
foreach ($responses as $response) {
	if ($response->getType() === RouterOS\Response::TYPE_DATA) {
	$mac = $response->getArgument('address');
	$ssid = $response->getArgument('ssid');
	$freq = $response->getArgument('freq');
	$sig = $response->getArgument('sig');
	$snr = $response->getArgument('snr');
	$radio_name = $response->getArgument('radio-name');
	
	$networks[$mac] = array(
	'ssid' => $ssid,
	'freq' => $freq,
	'sig' => $sig,
	'snr' => $snr,
	'radio_name' => $radio_name
	);
	}
}

print_r($networks);
?>

Hope you’ll find it usefull :smiley:

Some info:
This script not do remote scan. (the beginning of the scan drop the connection)

For remote scan use SSH with option save output to file,
when the connection return, you can take the file. :slight_smile:

You can set your wlan here:

->setArgument('number', WLAN_NUMBER_HERE)

It drops the connection but when the connection come back it returns you the data, i tested it, and it works correctly and it’s quick

How do you write to file?

numbers can be 0 after using command print, but in some case is not 0 but like “*a”…

I do not know php, but you find how make possible do:

->setArgument(‘number’, > ‘[ find where default-name=wlan1]’> )

Use this idea (also is the right syntax for scan):

/system ssh src-address=1.1.1.1 address=2.2.2.2 port=22 output-to-file=namefile.ext command> ="/interface wireless scan [ find where default-name=wlan1] duration=30> " user=username

On only 6 seconds you can not scan all, 30 seconds is better, find more signals, also weak, and is done also if you drop the WiFi connections (SSH has 5min timeout).

I really thank you for sharing all this information,
is the spirit of the forum!

Good work.

Are you sure? I’ve a RB435 with 4 wireless card and their id are 0, 1, 2, 3
They also have an ID like *a or *19a but using the print command i see 0, 1, 2, 3

If I write (using SSH):

:put [find where name="wlan4"]

It returns *id

Sure the script can be optimized and more user friendly, but it’s a working start point :smiley:

You can also set another duration on the script if 6 isn’t enough.
Just checked using 30s duration and 60s timeout it works great!

Your SSH command connects from a router to another so it works like this script.

Using PHP you can quickly save your scan results to DB to make advanced queries to deal with interference.

Thank you :slight_smile: I would be lost without this forum!

You can also set another duration on the script if 6 isn't enough.

(Remember: i not know php.)

So… if i put 30 seconds scan on REMOTE machine connected by Wireless, not by Wire, the scan continue (not stop as winbox), using API, as when I use SSH?

Sure! It’s for this reason i called it remote scan :smiley:

API connection is like SSH connection, i think there isn’t a real limit on timeout.
If you set 60s duration and 80s timeout, it works!

Just be sure you don’t have any limitation in your php configuration about script time limit.

You can remove the limit in the script writing this on the top, before the other code:

set_time_limit(0);

One unmissable reasonable limit are 60 second… :imp:
Or you lost the radio… :laughing:

Why? :open_mouth:
I never did a scan for more than 30-40s but I’m curious about this!

I talk about timeout… :slight_smile:
If you not remember to set, or set to “never” you must reboot REMOTE radio for stop scanning… :laughing:

Oh, sure, you’re right :smiley:

When I issue the below line of code I get an error where I can’t connect to the router

$client = new RouterOS\Client($userIp, $username, $password, null, false, $connection_timeout, Transmitter\NetworkStream::CRYPTO_TLS);

it is dying on Transmitter\NetworkStream::CRYPTO_TLS

exception ‘Exception’ with message ‘Class Transmitter\NetworkStream could not be
loaded from Transmitter\NetworkStream.php, file does not exist (registered path
s=“C:\inetpub\wwwroot”) [PEAR2_Autoload-0.2.4]’ in C:\inetpub\wwwroot\PEAR2\Auto
load.php:181
Stack trace:

My basic connect code is - I must be over looking something…

<?php use PEAR2\Net\RouterOS; require_once 'PEAR2/Autoload.php'; set_time_limit(30); $userIp = '192.168.88.1'; //ip here $username = 'admin'; //password here $password = ''; //username here $wlan = 0; //wlan id here $scan_duration = 6; //Duration of scan in seconds here $connection_timeout = 30; //API connection timeout here (set it bigger than scan_duration) \ \ try { $client = new RouterOS\Client($userIp, $username, $password, null, false, $connection_timeout, Transmitter\NetworkStream::CRYPTO_TLS); } catch (Exception $e) { die('Unable to connect to the router.'); //Inspect $e if you want to know details about the failure. }