Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $nbi = 'https://YOUR.ACS.URL:7557';
- $now = new DateTime();
- $date = $now->modify('1 days ago');
- $filter = [
- '_lastInform' => ['$gt' => $date->format(DateTimeInterface::ATOM)],
- ];
- $devices = queryNbi($nbi, $filter);
- $count = count($devices);
- printf("Refreshing %d devices\r\n", $count);
- $index = 0;
- $refreshItems = [
- 'InternetGatewayDevice.ManagementServer.PeriodicInformTime', // Parameter name doesn't matter if all you want is to force the CPE to inform
- ];
- foreach ($devices as $id) {
- $uri = sprintf('%s/devices/%s', $nbi, rawurlencode($id));
- if (!refresh($uri, $refreshItems, false)) {
- print "Error refreshing parameter on device {$id}\r\n";
- }
- printf("\t%d of %d - %%%0.2f\r", ++$index, $count, ($index / $count) * 100);
- }
- printf("Refreshed %d devices\r\n\r\n", $count);
- function refresh(string $uri, array $parameters, bool $connectionRequest = true): bool {
- $url = $uri . '/tasks' . ($connectionRequest ? '?timeout=1&connection_request' : '');
- $data = [
- 'name' => 'getParameterValues',
- 'parameterNames' => $parameters,
- ];
- /** @var string $dataString */
- $dataString = json_encode($data, JSON_THROW_ON_ERROR);
- $ch = createCurl($url);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
- curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, [
- 'Content-Type: application/json',
- 'Content-Length: ' . mb_strlen($dataString),
- ]);
- curl_exec($ch);
- $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- return $statusCode >= 200 && $statusCode <= 299;
- }
- function queryNbi(string $nbi, array $filter): array {
- $projections = ['_id'];
- $projection = $projections ? implode(',', $projections) : null;
- $uri = sprintf('%s/devices/?query=%s&projection=%s', $nbi, rawurlencode(json_encode($filter, JSON_THROW_ON_ERROR)), $projection);
- $curl = createCurl($uri);
- $data = json_decode(curl_exec($curl), true, 512, JSON_THROW_ON_ERROR);
- curl_close($curl);
- return array_map(static fn($row) => $row['_id'], $data);
- }
- function createCurl(string $url): CurlHandle {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_TIMEOUT_MS, 20000);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_URL, $url);
- return $curl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement