![]() |
|
[ Source navigation ] [ Diff markup ] [ Identifier search ] [ general search ] |
||
|
0001 #!/usr/bin/perl -w 0002 0003 # (c) Andrei Nigmatulin, 2005 0004 # 0005 # this script provided "as is", without any warranties. use it at your own risk. 0006 # 0007 # special thanx to Andrew Sitnikov for perl port 0008 # 0009 # this script converts CSV geoip database (free download at http://www.maxmind.com/app/geoip_country) 0010 # to format, suitable for use with nginx_http_geo module (http://sysoev.ru/nginx) 0011 # 0012 # for example, line with ip range 0013 # 0014 # "62.16.68.0","62.16.127.255","1041253376","1041268735","RU","Russian Federation" 0015 # 0016 # will be converted to four subnetworks: 0017 # 0018 # 62.16.68.0/22 RU; 0019 # 62.16.72.0/21 RU; 0020 # 62.16.80.0/20 RU; 0021 # 62.16.96.0/19 RU; 0022 0023 0024 use warnings; 0025 use strict; 0026 0027 while( <STDIN> ){ 0028 if (/"[^"]+","[^"]+","([^"]+)","([^"]+)","([^"]+)"/){ 0029 print_subnets($1, $2, $3); 0030 } 0031 } 0032 0033 sub print_subnets { 0034 my ($a1, $a2, $c) = @_; 0035 my $l; 0036 while ($a1 <= $a2) { 0037 for ($l = 0; ($a1 & (1 << $l)) == 0 && ($a1 + ((1 << ($l + 1)) - 1)) <= $a2; $l++){}; 0038 print long2ip($a1) . "/" . (32 - $l) . " " . $c . ";\n"; 0039 $a1 += (1 << $l); 0040 } 0041 } 0042 0043 sub long2ip { 0044 my $ip = shift; 0045 0046 my $str = 0; 0047 0048 $str = ($ip & 255); 0049 0050 $ip >>= 8; 0051 $str = ($ip & 255).".$str"; 0052 0053 $ip >>= 8; 0054 $str = ($ip & 255).".$str"; 0055 0056 $ip >>= 8; 0057 $str = ($ip & 255).".$str"; 0058 }
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.0.0 LXR engine. The LXR team |
![]() ![]() |