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 "{}:][
API and PHP
- NetonixUser
- Member
- Posts: 7
- Joined: Wed Oct 19, 2016 8:51 am
- Has thanked: 0 time
- Been thanked: 1 time
-
Eric Stern - Employee
- Posts: 532
- Joined: Wed Apr 09, 2014 9:41 pm
- Location: Toronto, Ontario
- Has thanked: 0 time
- Been thanked: 130 times
Re: API and PHP
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).
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
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?
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
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"
$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"
-
Eric Stern - Employee
- Posts: 532
- Joined: Wed Apr 09, 2014 9:41 pm
- Location: Toronto, Ontario
- Has thanked: 0 time
- Been thanked: 130 times
Re: API and PHP
- 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
-
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
Hi Eric,
Is this the same for authenticating on the Netonix Manager API?
Thanks
Mike
Is this the same for authenticating on the Netonix Manager API?
Thanks
Mike
-
Eric Stern - Employee
- Posts: 532
- Joined: Wed Apr 09, 2014 9:41 pm
- Location: Toronto, Ontario
- Has thanked: 0 time
- Been thanked: 130 times
Re: API and PHP
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.
-
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
Hi Eric,
That doesnt seem to work for me, do you have an example PHP Script?
Thanks
Mike
That doesnt seem to work for me, do you have an example PHP Script?
Thanks
Mike
-
Eric Stern - Employee
- Posts: 532
- Joined: Wed Apr 09, 2014 9:41 pm
- Location: Toronto, Ontario
- Has thanked: 0 time
- Been thanked: 130 times
Re: API and PHP
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;
Who is online
Users browsing this forum: No registered users and 46 guests