0,0 → 1,119 |
#!/usr/bin/php |
<?php |
|
# |
# generic Whois - Automatic Pattern Generator: ASN |
# |
# (c) 2012 Daniel Marschall, ViaThinkSoft [www.viathinksoft.de] |
# |
# Distribution, usage etc. pp. regulated by the current version of GPL. |
# |
# |
# Version 2012-11-17 |
# |
|
# TODO: also use extended stats of the RIRs, which may have avails / reserved? |
|
error_reporting(E_ALL | E_NOTICE | E_STRICT | E_DEPRECATED); |
|
require_once __DIR__ . '/config.inc.php'; |
require_once __DIR__ . '/../../shared/php_includes/common_functions.inc.php'; |
require_once __DIR__ . '/rirs.inc.php'; |
|
define('PATTERN_DIR', __DIR__ . '/../../main/pattern'); |
define('IANA_CACHE_DIR', __DIR__ . '/../.cache/pattern-generator/iana'); |
|
echo "ASN Preparation: Get IANA delegation file\n"; |
|
$iana_asn_url = "https://www.iana.org/assignments/as-numbers/as-numbers.txt"; |
$iana_asn_cache = IANA_CACHE_DIR . '/as-numbers.txt'; |
if ((USE_CACHE) && (file_exists($iana_asn_cache))) { |
@mkdir(dirname($iana_asn_cache), 0777, true); |
$iana_asn_data = file($iana_asn_cache, FILE_IGNORE_NEW_LINES); |
} else { |
$iana_asn_data = file($iana_asn_url, FILE_IGNORE_NEW_LINES); |
} |
|
echo "ASN: Parsing...\n"; |
|
$version = trim(str_replace('-', '', $iana_asn_data[3])); |
|
$iana_asn_data = grep($iana_asn_data, 'whois'); |
|
$iana_asn_data = implode("\n", $iana_asn_data); |
|
$iana_asn_data = str_replace('Assigned by RIPE NCC', '', $iana_asn_data); |
$iana_asn_data = str_replace('Assigned by AFRINIC', '', $iana_asn_data); // do not change to 'AfriNIC' ! |
$iana_asn_data = str_replace('Assigned by LACNIC', '', $iana_asn_data); |
$iana_asn_data = str_replace('Assigned by APNIC', '', $iana_asn_data); |
$iana_asn_data = str_replace('Assigned by ARIN', '', $iana_asn_data); |
|
while (strpos($iana_asn_data, ' ') !== false) { |
$iana_asn_data = str_replace(' ', ' ', $iana_asn_data); |
} |
|
$out = ''; |
|
$ary = explode("\n", $iana_asn_data); |
|
$res = array(); |
|
foreach ($ary as &$x) { |
$x = trim($x); |
if ($x == '') continue; |
$bry = explode(" ", $x); |
|
$asnums = $bry[0]; |
$whois = $bry[1]; |
|
if (strpos($asnums, '-') === false) { |
# Single ASN |
$res[$whois][$asnums] = $asnums; |
} else { |
# ASN range |
$tmp = explode('-', $asnums, 2); |
$res[$whois][$tmp[0]] = $tmp[1]; |
} |
} |
|
$out .= "#: version $version\n\n"; |
|
$out .= "# Autonomous System Numbers (ASN)\n"; |
$out .= "# Automatically generated by ".__DIR__."/gwhois-pattern-update\n"; |
$out .= "# Generation timestamp: ".date('Y-m-d H:i:s \G\M\TO')."\n"; |
$out .= "# Source: ($version) $iana_asn_url\n"; |
$out .= "\n"; |
|
foreach ($res as $whois => &$x) { |
$rir = array_search($whois, $rir_whois_server); |
|
if ($asn_additional_params[$rir] != '') { |
$whois .= '|prefix='.$asn_additional_params[$rir].'|'; |
} |
|
$out .= "# ".strtoupper($rir)."\n"; |
$out .= ":whois|$whois\n"; |
|
foreach ($x as $begin => &$end) { |
if ($begin == $end) { |
# Single ASN |
$out .= "*AS:$begin\n"; |
} else { |
# ASN Range |
$out .= "*AS:$begin-$end\n"; |
} |
} |
$out .= "\n"; |
} |
|
$out .= "# Last resort: ARIN\n"; |
$out .= ":whois|whois.arin.net|prefix=a |\n"; |
$out .= "^as(.*)$\n"; |
|
echo "ASN: Write to output file\n"; |
|
$h = fopen(PATTERN_DIR.'/asn', 'w') or die('Error opening file '.PATTERN_DIR.'/asn'); |
fwrite($h, $out) or die('Could not write to output file'); |
fclose($h); |
|
echo "ASN Finished\n"; |
Property changes: |
Added: svn:executable |
+* |
\ No newline at end of property |