0

I have some it's in a text file ips.txt. I want to get the full IP range of each IP from that list.

   The following is included in ips.txt
    37.123.206.198
    115.84.182.49
    154.16.116.35
    115.84.182.49
    142.250.192.14
    112.78.2.75

How can I get the full IP range of each ip? 1-255

Example (with first IP)
37.123.206.198

37.123.1.1
37.123.2.1
37.123.3.1
37.123.4.1
.
.
.
37.123.255.1
37.123.1.2
37.123.2.2
37.123.3.2
37.123.4.2
  • 1
    Those are individual IP addresses. They don't have a range. What are you trying to accomplish with this list? – gview Jul 03 '21 at 05:13
  • I've now explained it more clearly, please have a look. – XNIPER DAD Jul 03 '21 at 05:22
  • What have you tried so far? Where are you stuck? How does a single IP address relate to the range you are looking for? – Nico Haase Jul 03 '21 at 06:08
  • Does this answer your question? [Getting list IPs from CIDR notation in PHP](https://stackoverflow.com/questions/4931721/getting-list-ips-from-cidr-notation-in-php) – Top-Master Jul 03 '21 at 06:19
  • How many IPs in the file are you handling? Easiest way: Use `explode()` function to explode " . " and separate each number then manually traverse to each of them after that combine them all together. But consider using `ip2long()` if you're handling with a large data. – NcXNaV Jul 03 '21 at 06:23

2 Answers2

0

Read file and store ips in an array. Then:

<?php
$ips = [
    "37.123.206.198",
    "115.84.182.49",
    "154.16.116.35",
    "115.84.182.49",
    "142.250.192.14",
    "112.78.2.75"
];

$result = [];

foreach ($ips as $ip) {
    $ip_arr = explode('.', $ip);
    for ($i = 0; $i <= 255; $i++) {
        for ($j = 0; $j <= 255; $j++) {
            $new_ip = $ip_arr[0] . '.' . $ip_arr[1] . '.' . $i . '.' . $j;
            $result[$ip][] .= $new_ip;
        }
    }
}

print_r($result["37.123.206.198"][0]);


?>

Output: 37.123.0.0

Mohammad Mirsafaei
  • 954
  • 1
  • 5
  • 16
0

You can try to use ip2long to convert the IP addresses into integers.

/* Generate a list of all IP addresses between $start and $end (inclusive). */
function ip_range($start, $end) {
  return array_map('long2ip', range(ip2long($start), ip2long($end)) );
}
$ip_range = ip_range("1.1.1.1", "1.1.255.255");
foreach ($ip_range as $ip){
  echo $ip."<br>";
}

Try to run the code above and see if that's what you want.

Note: This will include x.x.x.0 as well (Ex: 1.1.2.0)

References: Check these out 1, 2.

NcXNaV
  • 1,657
  • 4
  • 14
  • 23