skip to Main Content

I will preface this by saying I’m a Lighting Designer not a programmer so be nice!

I have this API call that I’ve generated from Postman. Short of copy and pasting it over and over, what’s the most effective way to send this call to multiple IPs addresses in a range, factoring in that some IPs might not exist?

i.e. I want to send the below call to every IP from 2.4.7.1 through 2.4.7.99 however 2.4.7.56 may not exist as a device

TIA

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'http://2.4.7.1/api/profile/10/recall',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
  "keep_ip_settings": false
}',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Accept: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

2

Answers


  1. You may put the curl statements in a function (e.g callcurl) so that you can just use a loop to call the function.

    The loop can simply be (ignoring 2.4.7.56 by a if condition):

    $index=1;
    
    while ($index <=99){
       if ($index !=56) {
            callcurl("2.4.7.".$index);
          } 
    $index++;
    }
    

    So the whole code will be:

    <?php
    
    function callcurl($ip) {
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => 'http://'. $ip . '/api/profile/10/recall',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS =>'{
      "keep_ip_settings": false
    }',
      CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Accept: application/json'
      ),
    ));
    
    $response = curl_exec($curl);
    
    curl_close($curl);
    echo $response;
    }
    
    
    $index=1;
    
    while ($index <=99){
       if ($index !=56) {
            callcurl("2.4.7.".$index);
          } 
    $index++;
    }
    
    ?>
    
    Login or Signup to reply.
  2. <?php
    
    $baseIP = '2.4.7.';
    $apiEndpoint = '/api/profile/10/recall';
    
    for ($i = 1; $i <= 99; $i++) {
        $currentIP = $baseIP . $i;
        
        // Check if the IP exists (you can adjust the timeout based on your needs)
        if (filter_var($currentIP, FILTER_VALIDATE_IP) && pingIP($currentIP, 1)) {
            $curl = curl_init();
    
            curl_setopt_array($curl, array(
                CURLOPT_URL => 'http://' . $currentIP . $apiEndpoint,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => '{
                    "keep_ip_settings": false
                }',
                CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json',
                    'Accept: application/json'
                ),
            ));
    
            $response = curl_exec($curl);
    
            curl_close($curl);
            echo "API call sent to: $currentIPn";
            echo $response . "n";
        } else {
            echo "Skipping non-existing IP: $currentIPn";
        }
    }
    
    function pingIP($ip, $timeout) {
        $cmd = "ping -c 1 -W $timeout $ip";
        exec($cmd, $output, $result);
        return $result === 0;
    }
    ?>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search