-
Notifications
You must be signed in to change notification settings - Fork 0
/
UNIFI_get_sta.php
112 lines (89 loc) · 3.29 KB
/
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
<?php
//Cacti Script to gather stats from Unifi Devices....
if($argc!=6) exit("Usage php UNIFI_get_sta.php {host_ip} {username} {password} {M-A-C or SSID you want info for} {site}
");
$host=$argv[1];
$user=$argv[2];
$pass=$argv[3];
//force to lowercase and change : to -
$info=strtolower(str_replace(":","-",$argv[4]));
$site=$argv[5];
$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='username='.$user.'&password='.$pass.'&strict=true';
//$postData='{"username":"'.$user.'","password"="'.$pass.'","strict":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);
//echo $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
foreach ($SSIDcount as $key => $value) {
if($info==strtolower($key)) {
$myrssi=round(array_sum(${$key})/count(${$key}),2);
print "connections:$value rssi:$myrssi";
}
}
foreach ($APcount as $key => $value) {
if($info==$key) {
$myrssi=round(array_sum(${$key})/count(${$key}),2);
print "connections:$value rssi:$myrssi";
}
}
//print "\n";
?>