API and PHP

NetonixUser
Member
 
Posts: 7
Joined: Wed Oct 19, 2016 8:51 am
Has thanked: 0 time
Been thanked: 1 time

API and PHP

Wed Oct 19, 2016 9:57 am

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 "{}:][

User avatar
Eric Stern
Employee
Employee
 
Posts: 487
Joined: Wed Apr 09, 2014 9:41 pm
Location: Toronto, Ontario
Has thanked: 0 time
Been thanked: 119 times

Re: API and PHP

Wed Oct 19, 2016 1:31 pm

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

NetonixUser
Member
 
Posts: 7
Joined: Wed Oct 19, 2016 8:51 am
Has thanked: 0 time
Been thanked: 1 time

Re: API and PHP

Mon Oct 24, 2016 11:48 am

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?

NetonixUser
Member
 
Posts: 7
Joined: Wed Oct 19, 2016 8:51 am
Has thanked: 0 time
Been thanked: 1 time

Re: API and PHP

Mon Oct 24, 2016 11:52 am

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"

User avatar
Eric Stern
Employee
Employee
 
Posts: 487
Joined: Wed Apr 09, 2014 9:41 pm
Location: Toronto, Ontario
Has thanked: 0 time
Been thanked: 119 times

Re: API and PHP

Mon Oct 24, 2016 2:57 pm

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;


NetonixUser
Member
 
Posts: 7
Joined: Wed Oct 19, 2016 8:51 am
Has thanked: 0 time
Been thanked: 1 time

Re: API and PHP

Tue Oct 25, 2016 7:01 am

It worked first time :)

Thanks so much for your help

User avatar
MichaelBrandl
Member
 
Posts: 29
Joined: Mon Sep 21, 2015 11:15 am
Location: Hampshire, UK
Has thanked: 4 times
Been thanked: 2 times

Re: API and PHP

Tue Jun 13, 2017 5:29 am

Hi Eric,

Is this the same for authenticating on the Netonix Manager API?

Thanks
Mike

User avatar
Eric Stern
Employee
Employee
 
Posts: 487
Joined: Wed Apr 09, 2014 9:41 pm
Location: Toronto, Ontario
Has thanked: 0 time
Been thanked: 119 times

Re: API and PHP

Tue Jun 13, 2017 1:44 pm

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.

User avatar
MichaelBrandl
Member
 
Posts: 29
Joined: Mon Sep 21, 2015 11:15 am
Location: Hampshire, UK
Has thanked: 4 times
Been thanked: 2 times

Re: API and PHP

Mon Jun 19, 2017 7:04 am

Hi Eric,

That doesnt seem to work for me, do you have an example PHP Script?

Thanks


Mike

User avatar
Eric Stern
Employee
Employee
 
Posts: 487
Joined: Wed Apr 09, 2014 9:41 pm
Location: Toronto, Ontario
Has thanked: 0 time
Been thanked: 119 times

Re: API and PHP

Mon Jun 19, 2017 1:40 pm

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;

Next
Return to General Discussion

Who is online

Users browsing this forum: Google [Bot] and 9 guests