0

I am stuck with this error while trying to read a file which holds IP addresses separated by a new line. What I want to do, is to read my file that holds bunch of IP's and check does they have proper record. This is my code:

$file = "test.sh";

if (is_file($file) && is_readable($file)) {
    $get_file_data = file_get_contents($file);

    $turn_to_array = explode("\n", $get_file_data);

    foreach ($turn_to_array as $key => $value) {

        if (filter_var($value, FILTER_VALIDATE_IP)) {
            echo gethostbyaddr(trim($value));
        } else {
            echo "IP invalid";
        }
    }
}

My test.sh file looks like following:

EDIT : example IP's

180.1.1.1
181.1.1.2

Do I need to add some specific tests to parse a file or there is some other issue?

The error caused by the very unique method used:

Warning: gethostbyaddr(): Address is not a valid IPv4 or IPv6 PHP

Solved.

My code was working, I wasn't getting rdns record properly, since it didn't exist for those IP's. I've checked it with host 185.1.1.1 and it returned actual IP not domain. Then checked IP's for whom I was sure that they have PTR records, and it woked. But I am not sure exactly how error was fixed actually.

John
  • 1
  • 13
  • 98
  • 177
fugitive
  • 357
  • 2
  • 8
  • 26
  • Be careful with using `PHP_EOL` in this context. The server you are running it on may be *nix and use `\n`, but the file may have been generated on Windows and thus have `\r\n` – Jeremy Harris Jul 27 '16 at 18:38
  • 2
    Instead of `file_get_contents` and `explode(PHP_EOL` just use `file(`. Also `var_dump` your variable `$value` to confirm it is as expected. – chris85 Jul 27 '16 at 18:39
  • @TessellatingHeckler x.x.x are just masks for IP. I am running test my Debian. – fugitive Jul 27 '16 at 18:41
  • @MilosM, but does your file literally contain `180.x.x.x`? Or does it contain a bunch of real IP addresses conforming to that pattern, e.g. `180.1.2.3`? – ChrisGPT was on strike Jul 27 '16 at 18:42
  • Try `trim`ming your ip addresses before you check them. – aynber Jul 27 '16 at 18:42
  • I've edited my question. I've tried with **file** no success, when I dump $value I get IP4 string as expected (no empty chars at beggining and end). I have no idea what is wrong :/ – fugitive Jul 27 '16 at 18:47
  • You get 16 for the length when you dump? – frz3993 Jul 27 '16 at 18:54

1 Answers1

1

I'm going to be honest, I think exploding the raw file data and hoping for the best may not be the best way to accomplish this. Instead, you should try to extract the exact data you need using regular expressions so you are certain you are extracting the correct values.

Try something like this:

$data = file_get_contents($file);

// This regex matches things that resemble an IP4 address
if (preg_match_all("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/", $data, $addresses)) {

    foreach ($addresses as $address) {

        // Need to verify it ACTUALLY is a valid IP address
        if (filter_var($address, FILTER_VALIDATE_IP)) { 

            echo gethostbyaddr($address);

        }

    }

}

NOTE: The regex used here was written by @alex here: Regex to match an IP address

If this doesn't help, then it may be a configuration issue. A few other things to check that might help with troubleshooting are:

  • Does gethostbyaddr('127.0.0.1'); throw the same error?
  • Do you have a firewall that prevents the DNS request from going through?
  • Can you use the dig command from the server to issue a lookup on the IP?
Community
  • 1
  • 1
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
  • It's also an incorrect regex because it will match `333.444.555.666` which isn't a valid IP. – TessellatingHeckler Jul 27 '16 at 18:57
  • @Jeremy Harris Tried your version as well, added extra check if regexp is true, but it fails validating. It fails validating with _FILTER_VALIDATE_IP_ as well. This is my php ver: PHP 5.6.24-0+deb8u1 – fugitive Jul 27 '16 at 18:57
  • I added the `filter_var` code that you would have to use to ensure it is a valid IP4 address, but it sounds like you tried that and it didn't work. – Jeremy Harris Jul 27 '16 at 19:01
  • @MilosM Did this help? You accepted the answer. If so, what ended up being the problem? I am genuinely curious. – Jeremy Harris Jul 27 '16 at 19:10
  • @JeremyHarris To be honest, I don't know. But both your and mine solution works so far. If I don't do additional checks via either regex or filter_var, warning is thrown, but script executes properly in both way. What troubles me now, is that bot `if else` statement is `true` , since I get rnds and error that IP failed validating.. – fugitive Jul 27 '16 at 19:18
  • @MilosM The `if` and `else` both execute? That doesn't sound right. What is the error you get? Safe IPs to list publicly (to avoid inaccurate IPs comments) are `10.*.*.*`, `192.168.*.*`, and/or `172.16-31.*.*`. – chris85 Jul 27 '16 at 19:48
  • @JeremyHarris No error mate, I just get output from both statements. Very weird. I am using mine version – fugitive Jul 27 '16 at 19:54