Advertisement
Guest User

GenieACS force parameter refresh script

a guest
Jan 11th, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.50 KB | Source Code | 0 0
  1. <?php
  2.  
  3. $nbi = 'https://YOUR.ACS.URL:7557';
  4.  
  5. $now = new DateTime();
  6. $date = $now->modify('1 days ago');
  7.  
  8. $filter = [
  9.     '_lastInform' => ['$gt' => $date->format(DateTimeInterface::ATOM)],
  10. ];
  11.  
  12. $devices = queryNbi($nbi, $filter);
  13. $count = count($devices);
  14.  
  15. printf("Refreshing %d devices\r\n", $count);
  16.  
  17. $index = 0;
  18.  
  19. $refreshItems = [
  20.     'InternetGatewayDevice.ManagementServer.PeriodicInformTime', // Parameter name doesn't matter if all you want is to force the CPE to inform
  21. ];
  22.  
  23. foreach ($devices as $id) {
  24.     $uri = sprintf('%s/devices/%s', $nbi, rawurlencode($id));
  25.  
  26.     if (!refresh($uri, $refreshItems, false)) {
  27.         print "Error refreshing parameter on device {$id}\r\n";
  28.     }
  29.  
  30.     printf("\t%d of %d - %%%0.2f\r", ++$index, $count, ($index / $count) * 100);
  31. }
  32.  
  33. printf("Refreshed %d devices\r\n\r\n", $count);
  34.  
  35. function refresh(string $uri, array $parameters, bool $connectionRequest = true): bool {
  36.     $url = $uri . '/tasks' . ($connectionRequest ? '?timeout=1&connection_request' : '');
  37.  
  38.     $data = [
  39.         'name'           => 'getParameterValues',
  40.         'parameterNames' => $parameters,
  41.     ];
  42.     /** @var string $dataString */
  43.     $dataString = json_encode($data, JSON_THROW_ON_ERROR);
  44.  
  45.     $ch = createCurl($url);
  46.  
  47.     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  48.     curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
  49.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  50.     curl_setopt($ch, CURLOPT_HTTPHEADER, [
  51.             'Content-Type: application/json',
  52.             'Content-Length: ' . mb_strlen($dataString),
  53.     ]);
  54.  
  55.     curl_exec($ch);
  56.  
  57.     $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  58.  
  59.     return $statusCode >= 200 && $statusCode <= 299;
  60. }
  61.  
  62. function queryNbi(string $nbi, array $filter): array {
  63.     $projections = ['_id'];
  64.  
  65.     $projection = $projections ? implode(',', $projections) : null;
  66.  
  67.     $uri = sprintf('%s/devices/?query=%s&projection=%s', $nbi, rawurlencode(json_encode($filter, JSON_THROW_ON_ERROR)), $projection);
  68.  
  69.     $curl = createCurl($uri);
  70.  
  71.     $data = json_decode(curl_exec($curl), true, 512, JSON_THROW_ON_ERROR);
  72.  
  73.     curl_close($curl);
  74.  
  75.     return array_map(static fn($row) => $row['_id'], $data);
  76. }
  77.  
  78. function createCurl(string $url): CurlHandle {
  79.     $curl = curl_init();
  80.  
  81.     curl_setopt($curl, CURLOPT_TIMEOUT_MS, 20000);
  82.     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
  83.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  84.  
  85.     curl_setopt($curl, CURLOPT_URL, $url);
  86.  
  87.     return $curl;
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement