Skip to content

Commit 1d363de

Browse files
committed
added code examples demonstrating how to work with PPSKs
1 parent f343d3c commit 1d363de

File tree

3 files changed

+380
-0
lines changed

3 files changed

+380
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
/**
3+
* PHP API usage example
4+
*
5+
* contributed by: Art of WiFi
6+
* description: example basic PHP script to create a new PPSK for a WLAN on the UniFi controller
7+
*/
8+
use UniFi_API\Exceptions\CurlExtensionNotLoadedException;
9+
use UniFi_API\Exceptions\CurlGeneralErrorException;
10+
use UniFi_API\Exceptions\CurlTimeoutException;
11+
use UniFi_API\Exceptions\InvalidBaseUrlException;
12+
use UniFi_API\Exceptions\InvalidSiteNameException;
13+
use UniFi_API\Exceptions\JsonDecodeException;
14+
use UniFi_API\Exceptions\LoginFailedException;
15+
use UniFi_API\Exceptions\LoginRequiredException;
16+
17+
require 'vendor/autoload.php';
18+
19+
/**
20+
* Record start time.
21+
*/
22+
$start_time = microtime(true);
23+
24+
/**
25+
* Include the config file (place your credentials etc. there if not already present),
26+
* see the config.template.php file for an example.
27+
*
28+
* @var array $controller_user
29+
* @var array $controller_password
30+
* @var array $controller_url
31+
* @var array $controller_version
32+
* @var array $debug
33+
*/
34+
require_once 'config.php';
35+
36+
/**
37+
* The id of the site to use.
38+
*/
39+
$site_id = 'default';
40+
41+
/**
42+
* The new PPSK details.
43+
*/
44+
$new_ppsk_password = 'mysecretppsk'; // the password for the new PPSK, this password must be unique for the SSID
45+
$new_ppsk_network_id = 'zzzzzzzzzzzzzzzzzzzzz'; // id for the required VLAN, taken from the output of list_networkconf()
46+
$new_ppsk_wlan_id = 'xxxxxxxxxxxxxxxxxxxxx'; // id for the required WLAN, taken from the output of list_wlanconf()
47+
48+
try {
49+
/**
50+
* initialize the UniFi API connection class and log in to the controller and do our thing
51+
*/
52+
$unifi_connection = new UniFi_API\Client(
53+
$controller_user,
54+
$controller_password,
55+
$controller_url,
56+
$site_id,
57+
$controller_version
58+
);
59+
60+
$request_start_time = microtime(true);
61+
62+
$set_debug_mode = $unifi_connection->set_debug($debug);
63+
$login_results = $unifi_connection->login();
64+
$wlan_conf = $unifi_connection->list_wlanconf();
65+
66+
/**
67+
* Get the details for the WLAN the PPSK will be created for.
68+
*/
69+
$wlan_details = [];
70+
71+
foreach ($wlan_conf as $wlan) {
72+
if ($wlan->_id === $new_ppsk_wlan_id) {
73+
$wlan_details = $wlan;
74+
75+
break;
76+
}
77+
}
78+
79+
if (empty($wlan_details)) {
80+
echo 'WLAN not found, exiting... Please check the $new_ppsk_wlan_id value 🤨' . PHP_EOL;
81+
82+
exit;
83+
}
84+
85+
/**
86+
* Create the new PPSK, then append it to the existing PPSKs array.
87+
*/
88+
$new_ppsk = [
89+
'password' => $new_ppsk_password,
90+
'networkconf_id' => $new_ppsk_network_id,
91+
];
92+
93+
$wlan_details->private_preshared_keys[] = $new_ppsk;
94+
95+
$unifi_connection->set_wlansettings_base($new_ppsk_wlan_id, $wlan_details);
96+
97+
$request_end_time = microtime(true);
98+
99+
/**
100+
* Record end time.
101+
*/
102+
$end_time = microtime(true);
103+
104+
/**
105+
* Calculate and display the execution time.
106+
*/
107+
$execution_time = $end_time - $start_time;
108+
109+
echo 'The PPSK has been created successfully!👍' . PHP_EOL;
110+
111+
echo 'Full execution time: ' . $execution_time . ' seconds' . PHP_EOL;
112+
echo 'Time to fetch, process and push data back: ' . ($request_end_time - $request_start_time) . ' seconds' . PHP_EOL;
113+
} catch (CurlExtensionNotLoadedException $e) {
114+
echo 'CurlExtensionNotLoadedException: ' . $e->getMessage(). PHP_EOL;
115+
} catch (InvalidBaseUrlException $e) {
116+
echo 'InvalidBaseUrlException: ' . $e->getMessage(). PHP_EOL;
117+
} catch (InvalidSiteNameException $e) {
118+
echo 'InvalidSiteNameException: ' . $e->getMessage(). PHP_EOL;
119+
} catch (JsonDecodeException $e) {
120+
echo 'JsonDecodeException: ' . $e->getMessage(). PHP_EOL;
121+
} catch (LoginRequiredException $e) {
122+
echo 'LoginRequiredException: ' . $e->getMessage(). PHP_EOL;
123+
} catch (CurlGeneralErrorException $e) {
124+
echo 'CurlGeneralErrorException: ' . $e->getMessage(). PHP_EOL;
125+
} catch (CurlTimeoutException $e) {
126+
echo 'CurlTimeoutException: ' . $e->getMessage(). PHP_EOL;
127+
} catch (LoginFailedException $e) {
128+
echo 'LoginFailedException: ' . $e->getMessage(). PHP_EOL;
129+
} catch (Exception $e) {
130+
echo 'General Exception: ' . $e->getMessage(). PHP_EOL;
131+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/**
3+
* PHP API usage example
4+
*
5+
* contributed by: Art of WiFi
6+
* description: example basic PHP script to list all PPSKs for all WLANs in a specific UniFi site
7+
*/
8+
9+
use UniFi_API\Exceptions\CurlExtensionNotLoadedException;
10+
use UniFi_API\Exceptions\CurlGeneralErrorException;
11+
use UniFi_API\Exceptions\CurlTimeoutException;
12+
use UniFi_API\Exceptions\InvalidBaseUrlException;
13+
use UniFi_API\Exceptions\InvalidSiteNameException;
14+
use UniFi_API\Exceptions\JsonDecodeException;
15+
use UniFi_API\Exceptions\LoginFailedException;
16+
use UniFi_API\Exceptions\LoginRequiredException;
17+
18+
require 'vendor/autoload.php';
19+
20+
/**
21+
* Record start time.
22+
*/
23+
$start_time = microtime(true);
24+
25+
/**
26+
* Include the config file (place your credentials etc. there if not already present),
27+
* see the config.template.php file for an example.
28+
*
29+
* @var array $controller_user
30+
* @var array $controller_password
31+
* @var array $controller_url
32+
* @var array $controller_version
33+
* @var array $debug
34+
*/
35+
require_once 'config.php';
36+
37+
/**
38+
* The id of the site to use.
39+
*/
40+
$site_id = 'default';
41+
42+
try {
43+
/**
44+
* initialize the UniFi API connection class and log in to the controller and do our thing
45+
*/
46+
$unifi_connection = new UniFi_API\Client(
47+
$controller_user,
48+
$controller_password,
49+
$controller_url,
50+
$site_id,
51+
$controller_version
52+
);
53+
54+
$request_start_time = microtime(true);
55+
56+
$set_debug_mode = $unifi_connection->set_debug($debug);
57+
$login_results = $unifi_connection->login();
58+
$wlan_conf = $unifi_connection->list_wlanconf();
59+
60+
/**
61+
* Get the details for the WLAN the PPSK will be created for.
62+
*/
63+
$wlan_details = [];
64+
65+
foreach ($wlan_conf as $wlan) {
66+
/**
67+
* Skip this SSID if private_pre_shared_keys is not set or empty.
68+
*/
69+
if (empty($wlan->private_preshared_keys)) {
70+
continue;
71+
}
72+
73+
echo json_encode($wlan->private_preshared_keys, JSON_PRETTY_PRINT) . PHP_EOL;
74+
}
75+
76+
$request_end_time = microtime(true);
77+
78+
/**
79+
* Record end time.
80+
*/
81+
$end_time = microtime(true);
82+
83+
/**
84+
* Calculate and display the execution time.
85+
*/
86+
$execution_time = $end_time - $start_time;
87+
88+
echo 'Full execution time: ' . $execution_time . ' seconds' . PHP_EOL;
89+
echo 'Time to fetch, process and push data back: ' . ($request_end_time - $request_start_time) . ' seconds' . PHP_EOL;
90+
} catch (CurlExtensionNotLoadedException $e) {
91+
echo 'CurlExtensionNotLoadedException: ' . $e->getMessage(). PHP_EOL;
92+
} catch (InvalidBaseUrlException $e) {
93+
echo 'InvalidBaseUrlException: ' . $e->getMessage(). PHP_EOL;
94+
} catch (InvalidSiteNameException $e) {
95+
echo 'InvalidSiteNameException: ' . $e->getMessage(). PHP_EOL;
96+
} catch (JsonDecodeException $e) {
97+
echo 'JsonDecodeException: ' . $e->getMessage(). PHP_EOL;
98+
} catch (LoginRequiredException $e) {
99+
echo 'LoginRequiredException: ' . $e->getMessage(). PHP_EOL;
100+
} catch (CurlGeneralErrorException $e) {
101+
echo 'CurlGeneralErrorException: ' . $e->getMessage(). PHP_EOL;
102+
} catch (CurlTimeoutException $e) {
103+
echo 'CurlTimeoutException: ' . $e->getMessage(). PHP_EOL;
104+
} catch (LoginFailedException $e) {
105+
echo 'LoginFailedException: ' . $e->getMessage(). PHP_EOL;
106+
} catch (Exception $e) {
107+
echo 'General Exception: ' . $e->getMessage(). PHP_EOL;
108+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
/**
3+
* PHP API usage example
4+
*
5+
* contributed by: Art of WiFi
6+
* description: example basic PHP script to remove a PPSK from a specific UniFi site
7+
*/
8+
use UniFi_API\Exceptions\CurlExtensionNotLoadedException;
9+
use UniFi_API\Exceptions\CurlGeneralErrorException;
10+
use UniFi_API\Exceptions\CurlTimeoutException;
11+
use UniFi_API\Exceptions\InvalidBaseUrlException;
12+
use UniFi_API\Exceptions\InvalidSiteNameException;
13+
use UniFi_API\Exceptions\JsonDecodeException;
14+
use UniFi_API\Exceptions\LoginFailedException;
15+
use UniFi_API\Exceptions\LoginRequiredException;
16+
17+
require 'vendor/autoload.php';
18+
19+
/**
20+
* Record start time.
21+
*/
22+
$start_time = microtime(true);
23+
24+
$total_removals = 0;
25+
26+
/**
27+
* Include the config file (place your credentials etc. there if not already present),
28+
* see the config.template.php file for an example.
29+
*
30+
* @var array $controller_user
31+
* @var array $controller_password
32+
* @var array $controller_url
33+
* @var array $controller_version
34+
* @var array $debug
35+
*/
36+
require_once 'config.php';
37+
38+
/**
39+
* The id of the site to use.
40+
*/
41+
$site_id = 'default';
42+
43+
/**
44+
* The password value of the PPSK to remove.
45+
*/
46+
$ppsk_to_remove = 'mysecretppsk';
47+
48+
try {
49+
/**
50+
* Initialize the UniFi API connection class and log in to the controller and do our thing.
51+
*/
52+
$unifi_connection = new UniFi_API\Client(
53+
$controller_user,
54+
$controller_password,
55+
$controller_url,
56+
$site_id,
57+
$controller_version
58+
);
59+
60+
$request_start_time = microtime(true);
61+
62+
$set_debug_mode = $unifi_connection->set_debug($debug);
63+
$login_results = $unifi_connection->login();
64+
$wlan_conf = $unifi_connection->list_wlanconf();
65+
66+
foreach ($wlan_conf as $wlan) {
67+
/**
68+
* Skip this SSID if the private_pre_shared_keys array is not set or empty.
69+
*/
70+
if (empty($wlan->private_preshared_keys)) {
71+
continue;
72+
}
73+
74+
$removals = 0;
75+
76+
foreach ($wlan->private_preshared_keys as $ppsk) {
77+
if ($ppsk->password === $ppsk_to_remove) {
78+
echo 'Removing PPSK with password: "' . $ppsk_to_remove . '"' . PHP_EOL;
79+
80+
/**
81+
* Remove the PPSK from the private_preshared_keys array.
82+
*/
83+
$wlan->private_preshared_keys = array_values(array_filter($wlan->private_preshared_keys, function ($value) use ($ppsk_to_remove) {
84+
return $value->password !== $ppsk_to_remove;
85+
}));
86+
87+
$removals++;
88+
}
89+
}
90+
91+
/**
92+
* Push the updated WLAN configuration back to the controller if we removed one or more PPSKs.
93+
*/
94+
if ($removals > 0) {
95+
echo 'Pushing updated WLAN configuration back to the controller...' . PHP_EOL;
96+
$unifi_connection->set_wlansettings_base($wlan->_id, $wlan);
97+
$total_removals += $removals;
98+
}
99+
}
100+
101+
$request_end_time = microtime(true);
102+
103+
/**
104+
* Record end time.
105+
*/
106+
$end_time = microtime(true);
107+
108+
/**
109+
* Calculate the execution time.
110+
*/
111+
$execution_time = $end_time - $start_time;
112+
113+
if ($total_removals === 0) {
114+
echo 'No PPSKs were removed, exiting...' . PHP_EOL;
115+
116+
exit;
117+
}
118+
119+
echo 'Total PPSKs removed: ' . $total_removals . PHP_EOL;
120+
121+
echo 'Full execution time: ' . $execution_time . ' seconds' . PHP_EOL;
122+
echo 'Time to fetch, process and push data back: ' . ($request_end_time - $request_start_time) . ' seconds' . PHP_EOL;
123+
} catch (CurlExtensionNotLoadedException $e) {
124+
echo 'CurlExtensionNotLoadedException: ' . $e->getMessage(). PHP_EOL;
125+
} catch (InvalidBaseUrlException $e) {
126+
echo 'InvalidBaseUrlException: ' . $e->getMessage(). PHP_EOL;
127+
} catch (InvalidSiteNameException $e) {
128+
echo 'InvalidSiteNameException: ' . $e->getMessage(). PHP_EOL;
129+
} catch (JsonDecodeException $e) {
130+
echo 'JsonDecodeException: ' . $e->getMessage(). PHP_EOL;
131+
} catch (LoginRequiredException $e) {
132+
echo 'LoginRequiredException: ' . $e->getMessage(). PHP_EOL;
133+
} catch (CurlGeneralErrorException $e) {
134+
echo 'CurlGeneralErrorException: ' . $e->getMessage(). PHP_EOL;
135+
} catch (CurlTimeoutException $e) {
136+
echo 'CurlTimeoutException: ' . $e->getMessage(). PHP_EOL;
137+
} catch (LoginFailedException $e) {
138+
echo 'LoginFailedException: ' . $e->getMessage(). PHP_EOL;
139+
} catch (Exception $e) {
140+
echo 'General Exception: ' . $e->getMessage(). PHP_EOL;
141+
}

0 commit comments

Comments
 (0)