added code examples demonstrating how to work with PPSKs
This commit is contained in:
131
examples/ppsks_examples/create_ppsk.php
Executable file
131
examples/ppsks_examples/create_ppsk.php
Executable file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
/**
|
||||
* PHP API usage example
|
||||
*
|
||||
* contributed by: Art of WiFi
|
||||
* description: example basic PHP script to create a new PPSK for a WLAN on the UniFi controller
|
||||
*/
|
||||
use UniFi_API\Exceptions\CurlExtensionNotLoadedException;
|
||||
use UniFi_API\Exceptions\CurlGeneralErrorException;
|
||||
use UniFi_API\Exceptions\CurlTimeoutException;
|
||||
use UniFi_API\Exceptions\InvalidBaseUrlException;
|
||||
use UniFi_API\Exceptions\InvalidSiteNameException;
|
||||
use UniFi_API\Exceptions\JsonDecodeException;
|
||||
use UniFi_API\Exceptions\LoginFailedException;
|
||||
use UniFi_API\Exceptions\LoginRequiredException;
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
/**
|
||||
* Record start time.
|
||||
*/
|
||||
$start_time = microtime(true);
|
||||
|
||||
/**
|
||||
* Include the config file (place your credentials etc. there if not already present),
|
||||
* see the config.template.php file for an example.
|
||||
*
|
||||
* @var array $controller_user
|
||||
* @var array $controller_password
|
||||
* @var array $controller_url
|
||||
* @var array $controller_version
|
||||
* @var array $debug
|
||||
*/
|
||||
require_once 'config.php';
|
||||
|
||||
/**
|
||||
* The id of the site to use.
|
||||
*/
|
||||
$site_id = 'default';
|
||||
|
||||
/**
|
||||
* The new PPSK details.
|
||||
*/
|
||||
$new_ppsk_password = 'mysecretppsk'; // the password for the new PPSK, this password must be unique for the SSID
|
||||
$new_ppsk_network_id = 'zzzzzzzzzzzzzzzzzzzzz'; // id for the required VLAN, taken from the output of list_networkconf()
|
||||
$new_ppsk_wlan_id = 'xxxxxxxxxxxxxxxxxxxxx'; // id for the required WLAN, taken from the output of list_wlanconf()
|
||||
|
||||
try {
|
||||
/**
|
||||
* initialize the UniFi API connection class and log in to the controller and do our thing
|
||||
*/
|
||||
$unifi_connection = new UniFi_API\Client(
|
||||
$controller_user,
|
||||
$controller_password,
|
||||
$controller_url,
|
||||
$site_id,
|
||||
$controller_version
|
||||
);
|
||||
|
||||
$request_start_time = microtime(true);
|
||||
|
||||
$set_debug_mode = $unifi_connection->set_debug($debug);
|
||||
$login_results = $unifi_connection->login();
|
||||
$wlan_conf = $unifi_connection->list_wlanconf();
|
||||
|
||||
/**
|
||||
* Get the details for the WLAN the PPSK will be created for.
|
||||
*/
|
||||
$wlan_details = [];
|
||||
|
||||
foreach ($wlan_conf as $wlan) {
|
||||
if ($wlan->_id === $new_ppsk_wlan_id) {
|
||||
$wlan_details = $wlan;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($wlan_details)) {
|
||||
echo 'WLAN not found, exiting... Please check the $new_ppsk_wlan_id value 🤨' . PHP_EOL;
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the new PPSK, then append it to the existing PPSKs array.
|
||||
*/
|
||||
$new_ppsk = [
|
||||
'password' => $new_ppsk_password,
|
||||
'networkconf_id' => $new_ppsk_network_id,
|
||||
];
|
||||
|
||||
$wlan_details->private_preshared_keys[] = $new_ppsk;
|
||||
|
||||
$unifi_connection->set_wlansettings_base($new_ppsk_wlan_id, $wlan_details);
|
||||
|
||||
$request_end_time = microtime(true);
|
||||
|
||||
/**
|
||||
* Record end time.
|
||||
*/
|
||||
$end_time = microtime(true);
|
||||
|
||||
/**
|
||||
* Calculate and display the execution time.
|
||||
*/
|
||||
$execution_time = $end_time - $start_time;
|
||||
|
||||
echo 'The PPSK has been created successfully!👍' . PHP_EOL;
|
||||
|
||||
echo 'Full execution time: ' . $execution_time . ' seconds' . PHP_EOL;
|
||||
echo 'Time to fetch, process and push data back: ' . ($request_end_time - $request_start_time) . ' seconds' . PHP_EOL;
|
||||
} catch (CurlExtensionNotLoadedException $e) {
|
||||
echo 'CurlExtensionNotLoadedException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (InvalidBaseUrlException $e) {
|
||||
echo 'InvalidBaseUrlException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (InvalidSiteNameException $e) {
|
||||
echo 'InvalidSiteNameException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (JsonDecodeException $e) {
|
||||
echo 'JsonDecodeException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (LoginRequiredException $e) {
|
||||
echo 'LoginRequiredException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (CurlGeneralErrorException $e) {
|
||||
echo 'CurlGeneralErrorException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (CurlTimeoutException $e) {
|
||||
echo 'CurlTimeoutException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (LoginFailedException $e) {
|
||||
echo 'LoginFailedException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (Exception $e) {
|
||||
echo 'General Exception: ' . $e->getMessage(). PHP_EOL;
|
||||
}
|
||||
108
examples/ppsks_examples/list_ppsks.php
Executable file
108
examples/ppsks_examples/list_ppsks.php
Executable file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
/**
|
||||
* PHP API usage example
|
||||
*
|
||||
* contributed by: Art of WiFi
|
||||
* description: example basic PHP script to list all PPSKs for all WLANs in a specific UniFi site
|
||||
*/
|
||||
|
||||
use UniFi_API\Exceptions\CurlExtensionNotLoadedException;
|
||||
use UniFi_API\Exceptions\CurlGeneralErrorException;
|
||||
use UniFi_API\Exceptions\CurlTimeoutException;
|
||||
use UniFi_API\Exceptions\InvalidBaseUrlException;
|
||||
use UniFi_API\Exceptions\InvalidSiteNameException;
|
||||
use UniFi_API\Exceptions\JsonDecodeException;
|
||||
use UniFi_API\Exceptions\LoginFailedException;
|
||||
use UniFi_API\Exceptions\LoginRequiredException;
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
/**
|
||||
* Record start time.
|
||||
*/
|
||||
$start_time = microtime(true);
|
||||
|
||||
/**
|
||||
* Include the config file (place your credentials etc. there if not already present),
|
||||
* see the config.template.php file for an example.
|
||||
*
|
||||
* @var array $controller_user
|
||||
* @var array $controller_password
|
||||
* @var array $controller_url
|
||||
* @var array $controller_version
|
||||
* @var array $debug
|
||||
*/
|
||||
require_once 'config.php';
|
||||
|
||||
/**
|
||||
* The id of the site to use.
|
||||
*/
|
||||
$site_id = 'default';
|
||||
|
||||
try {
|
||||
/**
|
||||
* initialize the UniFi API connection class and log in to the controller and do our thing
|
||||
*/
|
||||
$unifi_connection = new UniFi_API\Client(
|
||||
$controller_user,
|
||||
$controller_password,
|
||||
$controller_url,
|
||||
$site_id,
|
||||
$controller_version
|
||||
);
|
||||
|
||||
$request_start_time = microtime(true);
|
||||
|
||||
$set_debug_mode = $unifi_connection->set_debug($debug);
|
||||
$login_results = $unifi_connection->login();
|
||||
$wlan_conf = $unifi_connection->list_wlanconf();
|
||||
|
||||
/**
|
||||
* Get the details for the WLAN the PPSK will be created for.
|
||||
*/
|
||||
$wlan_details = [];
|
||||
|
||||
foreach ($wlan_conf as $wlan) {
|
||||
/**
|
||||
* Skip this SSID if private_pre_shared_keys is not set or empty.
|
||||
*/
|
||||
if (empty($wlan->private_preshared_keys)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
echo json_encode($wlan->private_preshared_keys, JSON_PRETTY_PRINT) . PHP_EOL;
|
||||
}
|
||||
|
||||
$request_end_time = microtime(true);
|
||||
|
||||
/**
|
||||
* Record end time.
|
||||
*/
|
||||
$end_time = microtime(true);
|
||||
|
||||
/**
|
||||
* Calculate and display the execution time.
|
||||
*/
|
||||
$execution_time = $end_time - $start_time;
|
||||
|
||||
echo 'Full execution time: ' . $execution_time . ' seconds' . PHP_EOL;
|
||||
echo 'Time to fetch, process and push data back: ' . ($request_end_time - $request_start_time) . ' seconds' . PHP_EOL;
|
||||
} catch (CurlExtensionNotLoadedException $e) {
|
||||
echo 'CurlExtensionNotLoadedException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (InvalidBaseUrlException $e) {
|
||||
echo 'InvalidBaseUrlException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (InvalidSiteNameException $e) {
|
||||
echo 'InvalidSiteNameException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (JsonDecodeException $e) {
|
||||
echo 'JsonDecodeException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (LoginRequiredException $e) {
|
||||
echo 'LoginRequiredException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (CurlGeneralErrorException $e) {
|
||||
echo 'CurlGeneralErrorException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (CurlTimeoutException $e) {
|
||||
echo 'CurlTimeoutException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (LoginFailedException $e) {
|
||||
echo 'LoginFailedException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (Exception $e) {
|
||||
echo 'General Exception: ' . $e->getMessage(). PHP_EOL;
|
||||
}
|
||||
141
examples/ppsks_examples/remove_ppsk.php
Executable file
141
examples/ppsks_examples/remove_ppsk.php
Executable file
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
/**
|
||||
* PHP API usage example
|
||||
*
|
||||
* contributed by: Art of WiFi
|
||||
* description: example basic PHP script to remove a PPSK from a specific UniFi site
|
||||
*/
|
||||
use UniFi_API\Exceptions\CurlExtensionNotLoadedException;
|
||||
use UniFi_API\Exceptions\CurlGeneralErrorException;
|
||||
use UniFi_API\Exceptions\CurlTimeoutException;
|
||||
use UniFi_API\Exceptions\InvalidBaseUrlException;
|
||||
use UniFi_API\Exceptions\InvalidSiteNameException;
|
||||
use UniFi_API\Exceptions\JsonDecodeException;
|
||||
use UniFi_API\Exceptions\LoginFailedException;
|
||||
use UniFi_API\Exceptions\LoginRequiredException;
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
/**
|
||||
* Record start time.
|
||||
*/
|
||||
$start_time = microtime(true);
|
||||
|
||||
$total_removals = 0;
|
||||
|
||||
/**
|
||||
* Include the config file (place your credentials etc. there if not already present),
|
||||
* see the config.template.php file for an example.
|
||||
*
|
||||
* @var array $controller_user
|
||||
* @var array $controller_password
|
||||
* @var array $controller_url
|
||||
* @var array $controller_version
|
||||
* @var array $debug
|
||||
*/
|
||||
require_once 'config.php';
|
||||
|
||||
/**
|
||||
* The id of the site to use.
|
||||
*/
|
||||
$site_id = 'default';
|
||||
|
||||
/**
|
||||
* The password value of the PPSK to remove.
|
||||
*/
|
||||
$ppsk_to_remove = 'mysecretppsk';
|
||||
|
||||
try {
|
||||
/**
|
||||
* Initialize the UniFi API connection class and log in to the controller and do our thing.
|
||||
*/
|
||||
$unifi_connection = new UniFi_API\Client(
|
||||
$controller_user,
|
||||
$controller_password,
|
||||
$controller_url,
|
||||
$site_id,
|
||||
$controller_version
|
||||
);
|
||||
|
||||
$request_start_time = microtime(true);
|
||||
|
||||
$set_debug_mode = $unifi_connection->set_debug($debug);
|
||||
$login_results = $unifi_connection->login();
|
||||
$wlan_conf = $unifi_connection->list_wlanconf();
|
||||
|
||||
foreach ($wlan_conf as $wlan) {
|
||||
/**
|
||||
* Skip this SSID if the private_pre_shared_keys array is not set or empty.
|
||||
*/
|
||||
if (empty($wlan->private_preshared_keys)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$removals = 0;
|
||||
|
||||
foreach ($wlan->private_preshared_keys as $ppsk) {
|
||||
if ($ppsk->password === $ppsk_to_remove) {
|
||||
echo 'Removing PPSK with password: "' . $ppsk_to_remove . '"' . PHP_EOL;
|
||||
|
||||
/**
|
||||
* Remove the PPSK from the private_preshared_keys array.
|
||||
*/
|
||||
$wlan->private_preshared_keys = array_values(array_filter($wlan->private_preshared_keys, function ($value) use ($ppsk_to_remove) {
|
||||
return $value->password !== $ppsk_to_remove;
|
||||
}));
|
||||
|
||||
$removals++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Push the updated WLAN configuration back to the controller if we removed one or more PPSKs.
|
||||
*/
|
||||
if ($removals > 0) {
|
||||
echo 'Pushing updated WLAN configuration back to the controller...' . PHP_EOL;
|
||||
$unifi_connection->set_wlansettings_base($wlan->_id, $wlan);
|
||||
$total_removals += $removals;
|
||||
}
|
||||
}
|
||||
|
||||
$request_end_time = microtime(true);
|
||||
|
||||
/**
|
||||
* Record end time.
|
||||
*/
|
||||
$end_time = microtime(true);
|
||||
|
||||
/**
|
||||
* Calculate the execution time.
|
||||
*/
|
||||
$execution_time = $end_time - $start_time;
|
||||
|
||||
if ($total_removals === 0) {
|
||||
echo 'No PPSKs were removed, exiting...' . PHP_EOL;
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
echo 'Total PPSKs removed: ' . $total_removals . PHP_EOL;
|
||||
|
||||
echo 'Full execution time: ' . $execution_time . ' seconds' . PHP_EOL;
|
||||
echo 'Time to fetch, process and push data back: ' . ($request_end_time - $request_start_time) . ' seconds' . PHP_EOL;
|
||||
} catch (CurlExtensionNotLoadedException $e) {
|
||||
echo 'CurlExtensionNotLoadedException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (InvalidBaseUrlException $e) {
|
||||
echo 'InvalidBaseUrlException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (InvalidSiteNameException $e) {
|
||||
echo 'InvalidSiteNameException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (JsonDecodeException $e) {
|
||||
echo 'JsonDecodeException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (LoginRequiredException $e) {
|
||||
echo 'LoginRequiredException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (CurlGeneralErrorException $e) {
|
||||
echo 'CurlGeneralErrorException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (CurlTimeoutException $e) {
|
||||
echo 'CurlTimeoutException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (LoginFailedException $e) {
|
||||
echo 'LoginFailedException: ' . $e->getMessage(). PHP_EOL;
|
||||
} catch (Exception $e) {
|
||||
echo 'General Exception: ' . $e->getMessage(). PHP_EOL;
|
||||
}
|
||||
Reference in New Issue
Block a user