-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathss_unifi_get_sta.php
125 lines (97 loc) · 3.94 KB
/
ss_unifi_get_sta.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
//Cacti Script to gather stats from Unifi Devices....
/* do NOT run this script through a web browser */
if (!isset($_SERVER["argv"][0]) || isset($_SERVER['REQUEST_METHOD']) || isset($_SERVER['REMOTE_ADDR'])) {
die("<br><strong>This script is only meant to run at the command line.</strong>");
}
$no_http_headers = true;
/* display ALL errors */
error_reporting(0);
if (!isset($called_by_script_server)) {
include_once(dirname(__FILE__) . "/../include/global.php");
array_shift($_SERVER['argv']);
print call_user_func_array("ss_unifi_get_sta", $_SERVER['argv']);
}
function ss_unifi_get_sta($host, $user, $pass, $info, $site)
{
//force to lowercase and change : to -
$info = strtolower(str_replace(":", "-", $info));
$baseurl = 'https://'.$host.':8443';
//get the data
$ch = curl_init();
curl_setopt($ch, CURLOPT_REFERER, $baseurl."/manage/account/login");
curl_setopt($ch, CURLOPT_URL, $baseurl.'/api/login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,array (
"Content-Type: application/json",
"X-Requested-With:XMLHttpRequest",
"Connection:keep-alive"
));
curl_setopt($ch, CURLOPT_ENCODING, "gzip, deflate");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
//curl_setopt($ch, CURLOPT_GET, false);
//Error Checking:
//curl_setopt($ch, CURLOPT_VERBOSE, true);
$postData = json_encode(array("username" => $user, "password" => $pass));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec($ch);
//echo $store;
curl_setopt($ch, CURLOPT_URL, $baseurl.'/api/s/'.$site.'/stat/sta');
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_REFERER, $baseurl."/manage");
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
// var_dump($output);
$json_a = json_decode($output,true);
foreach($json_a['data'] as $data) {
//for debug, will list all the output
/*
echo 'ap_mac: '.$data['ap_mac'].'
essid: '.$data['essid'].'
mac: '.$data['mac'].'
signal: '.$data['signal'].'
rssi: '.$data['rssi'].'
rx_bytes: '.$data['rx_bytes'].'
tx_bytes: '.$data['tx_bytes'].'
';
*/
$mySSIDArray[] = $data['essid'];
$myAPArray[] = str_replace(":","-",$data['ap_mac']);
//Get rssi for each ssid in an array
$myessid = $data['essid'];
${$myessid}[] = $data['rssi'];
//Get rssi per AP in an array
$myap = str_replace(":","-",$data['ap_mac']);
${$myap}[] = $data['rssi'];
}
$SSIDcount = array_count_values($mySSIDArray); //count up the number of times it shows up
$APcount = array_count_values($myAPArray); //count up the number of times it shows up
$results = "connections:0 rssi:0";
foreach ($SSIDcount as $key => $value) {
if ($info == strtolower($key)) {
$myrssi = round(array_sum(${$key})/count(${$key}), 2);
$results = "connections:$value rssi:$myrssi";
}
}
foreach ($APcount as $key => $value) {
if ($info == $key) {
$myrssi = round(array_sum(${$key})/count(${$key}),2);
$results = "connections:$value rssi:$myrssi";
}
}
return $results;
}
?>