Page 1 of 2
API and PHP
Posted: Wed Oct 19, 2016 9:57 am
by NetonixUser
I am trying to pull the MAC table from a Netonix WS-6-MINI Swithc using PHP
The PHP code is...
$html = file_get_contents('http://192.168.1.20/api.php/v1/mactable');
echo $html;
This code works on every other site I have tried no problems
Using this code on the Netonix I get this error...
"
Fatal error: Using $this when not in object context in
/www/api.php on line
691"
Any ideas?
Or is there documentation on the API?
The only thing I can think of is this page
http://192.168.1.20/api.php/v1/mactable contains lots of special characters like "{}:][
Re: API and PHP
Posted: Wed Oct 19, 2016 1:31 pm
by Eric Stern
Its because you have not authenticated the request. You will need to POST to
http://192.168.1.20/index.php with a username and password to create a session, grab the cookie from the response and send the cookie along with the request for
http://192.168.1.20/api.php/v1/mactable.
The
http://192.168.1.20/api.php/v1/mactable contains special characters because it is returning the mactable information in JSON format.
Alternately use SSH and grab /tmp/mactable.json, which contains the same information.
Or use SNMP (BRIDGE-MIB).
Re: API and PHP
Posted: Mon Oct 24, 2016 11:48 am
by NetonixUser
Great thanks, I just tested that theory and your right I didn't notice.
You can browse to
http://192.168.1.20/api.php/v1/mactable using web browser and you can view the MAC address's but only if your logged in.
I have spent the last week trying to write the PHP code to authenticate with the Netonix and pull the address's off. So far I am not having any luck. Is there any documentation for this or can you please help write the code?
Re: API and PHP
Posted: Mon Oct 24, 2016 11:52 am
by NetonixUser
This is the code I am using at the momment
$payload = 'admin:admin';
$process = curl_init('http://192.168.1.20/index.php');
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $additionalHeaders));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, 'admin:admin');
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, $payload);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
curl_close($process);
echo $return;
The reply from the Netonix is "Invalid username or password"
Re: API and PHP
Posted: Mon Oct 24, 2016 2:57 pm
by Eric Stern
- Code: Select all
$data = array('username' => 'admin', 'password' => 'admin');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents('https://192.168.2.9/index.php', false, $context);
$session_id = '';
foreach($http_response_header as $header) {
if (preg_match('/^Set-Cookie: (PHPSESSID=.*;)/mi', $header, $match) == 1) {
$session_id = $match[1];
}
}
$options = array(
'http' => array(
'method' => "GET",
'header' => "Cookie: " . $session_id
)
);
$context = stream_context_create($options);
$result = file_get_contents('https://192.168.2.9/api/v1/mactable', false, $context);
echo $result;
Re: API and PHP
Posted: Tue Oct 25, 2016 7:01 am
by NetonixUser
It worked first time :)
Thanks so much for your help
Re: API and PHP
Posted: Tue Jun 13, 2017 5:29 am
by MichaelBrandl
Hi Eric,
Is this the same for authenticating on the Netonix Manager API?
Thanks
Mike
Re: API and PHP
Posted: Tue Jun 13, 2017 1:44 pm
by Eric Stern
Similar, but not the same. Manager uses session cookies. You can authenticate the session by doing a POST to / including "username" and "password" in the body. Then you can do any API calls by just including the cookie.
Re: API and PHP
Posted: Mon Jun 19, 2017 7:04 am
by MichaelBrandl
Hi Eric,
That doesnt seem to work for me, do you have an example PHP Script?
Thanks
Mike
Re: API and PHP
Posted: Mon Jun 19, 2017 1:40 pm
by Eric Stern
It actually requires 2 cookies.
- Code: Select all
$data = array('username' => 'admin', 'password' => 'admin');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
),
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$context = stream_context_create($options);
$result = file_get_contents('https://192.168.2.8:3443/', false, $context);
$session_id = '';
$session_sig='';
foreach($http_response_header as $header) {
if (preg_match('/^Set-Cookie: (session=.*?);/mi', $header, $match) == 1) {
$session_id = $match[1];
}
if (preg_match('/^Set-Cookie: (session.sig=.*?);/mi', $header, $match) == 1) {
$session_sig = $match[1];
}
}
$options = array(
'http' => array(
'method' => "GET",
'header' => "Cookie: " . $session_id . "; " . $session_sig
),
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$context = stream_context_create($options);
$result = file_get_contents('https://192.168.2.8:3443/api/devices', false, $context);
echo $result;