Skip to content

Instantly share code, notes, and snippets.

@Invis1ble
Last active January 21, 2017 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Invis1ble/7a22d6545ef91462a440dc12f1d1a3e4 to your computer and use it in GitHub Desktop.
Save Invis1ble/7a22d6545ef91462a440dc12f1d1a3e4 to your computer and use it in GitHub Desktop.
Small console script for checking IP address
#!/usr/bin/env php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
set_time_limit(0);
$triesNumber = 5;
$timeout = 5;
do {
$context = stream_context_create([
'http' => [
'timeout' => $timeout,
],
]);
$content = @file_get_contents('http://45.76.25.15/', null, $context); // ipv4.icanhazip.com
if (false === $content) {
continue;
}
$content = rtrim($content);
if (!filter_var($content, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
continue;
}
echo "\033[0;92m", $content, "\033[0m\n";
exit(0);
} while (-- $triesNumber);
echo "\033[0;91mn/a\033[0m\n";
exit(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment