Community discussions

MikroTik App
 
gius64
Member Candidate
Member Candidate
Topic Author
Posts: 213
Joined: Tue Jan 14, 2014 3:43 pm

[HOW TO] Remote Scan using API

Tue Apr 01, 2014 8:01 pm

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 :D
Last edited by gius64 on Tue Apr 01, 2014 11:32 pm, edited 3 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: [HOW TO] Remote Scan using API

Tue Apr 01, 2014 10:55 pm

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. :)
Last edited by rextended on Tue Apr 01, 2014 11:52 pm, edited 3 times in total.
 
gius64
Member Candidate
Member Candidate
Topic Author
Posts: 213
Joined: Tue Jan 14, 2014 3:43 pm

Re: [HOW TO] Remote Scan using API

Tue Apr 01, 2014 11:01 pm

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?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: [HOW TO] Remote Scan using API

Tue Apr 01, 2014 11:02 pm

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]')
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: [HOW TO] Remote Scan using API

Tue Apr 01, 2014 11:06 pm

How do you write to file?
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).
Last edited by rextended on Tue Apr 01, 2014 11:53 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: [HOW TO] Remote Scan using API

Tue Apr 01, 2014 11:09 pm

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

Good work.
 
gius64
Member Candidate
Member Candidate
Topic Author
Posts: 213
Joined: Tue Jan 14, 2014 3:43 pm

Re: [HOW TO] Remote Scan using API

Tue Apr 01, 2014 11:12 pm

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 :D

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.
I really thank you for sharing all this information,
is the spirit of the forum!

Good work.
Thank you :) I would be lost without this forum!
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: [HOW TO] Remote Scan using API

Tue Apr 01, 2014 11:24 pm

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?
 
gius64
Member Candidate
Member Candidate
Topic Author
Posts: 213
Joined: Tue Jan 14, 2014 3:43 pm

Re: [HOW TO] Remote Scan using API

Tue Apr 01, 2014 11:31 pm

Sure! It's for this reason i called it remote scan :D

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);
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: [HOW TO] Remote Scan using API

Tue Apr 01, 2014 11:54 pm

One unmissable reasonable limit are 60 second... :evil:
Or you lost the radio.... :lol:
 
gius64
Member Candidate
Member Candidate
Topic Author
Posts: 213
Joined: Tue Jan 14, 2014 3:43 pm

Re: [HOW TO] Remote Scan using API

Tue Apr 01, 2014 11:57 pm

Why? :shock:
I never did a scan for more than 30-40s but I'm curious about this!
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: [HOW TO] Remote Scan using API

Wed Apr 02, 2014 12:13 am

I talk about timeout.... :)
If you not remember to set, or set to "never" you must reboot REMOTE radio for stop scanning.... :lol:
 
gius64
Member Candidate
Member Candidate
Topic Author
Posts: 213
Joined: Tue Jan 14, 2014 3:43 pm

Re: [HOW TO] Remote Scan using API

Wed Apr 02, 2014 12:17 am

Oh, sure, you're right :D
 
jamesj
just joined
Posts: 9
Joined: Mon Mar 31, 2014 9:36 pm

Re: [HOW TO] Remote Scan using API

Thu Feb 12, 2015 12:10 am

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.
}

Who is online

Users browsing this forum: No registered users and 17 guests