NAME

Simple::IPInfo - Get IP/IPList Info (location, as number, etc)

给定单个IP或IP列表,查对应的区域信息,或AS号

SYNOPSIS

use Simple::IPInfo;
use Data::Dumper;
use utf8;

my $r_loc = get_ip_loc('202.38.64.10');
my $r_asn = get_ip_as('202.38.64.10');
print Dumper($r_loc,$r_asn);

my $rr_loc = get_ip_loc([ '202.38.64.10', '202.96.196.33' ]);
my $rr_asn = get_ip_as([ '202.38.64.10', '202.96.196.33' ]);
print Dumper($rr_loc, $rr_asn);

DESCRIPTION

default ip as data: ftp://routeviews.org/dnszones/originas.bz2

default ip location data: http://ip.taobao.com

METHOD

get_ip_loc

get ip location, with state, prov, isp

返回IP区域信息,包括国家、省份、运营商

my $r = get_ip_loc('202.38.64.10');

my $rr = get_ip_loc([ '202.38.64.10', '202.96.196.33' ]);

# {  
# $ip_a => { state => ... , prov => ..., isp => ... }, 
# $ip_b => { state => ... , prov => ..., isp => ... }, 
# }

get_ip_as

get ip as number

返回IP的AS号

my $r = get_ip_as('202.38.64.10');

my $rr = get_ip_as([ '202.38.64.10', '202.96.196.33' ]);

# {  
# $ip_a => { as => ... }, 
# $ip_b => { as => ... }, 
# }

get_ip_info

get_ip_info from some json file, see also IPInfo_LOC.json

给定IP列表,以及json文件,返回json文件提供的IP信息,格式参考IPInfo_LOC.json

my $rr = get_ip_info([ '202.38.64.10', '202.96.196.33' ], 
    ip_info_file => 'some_ipinfo.json', 
    use_ip_c => 0, 
);

get_ipc_info

get_ipc_info from ip info hash

指定ip,从已知的ip_c段hash中取出对应的信息

my $r =  get_ipc_info('202.38.64.10', $rr);

read_table_ipinfo

add ipinfo to arrayref

use Simple::IPInfo;
use Data::Dumper;

my $arr = [ [qw/202.38.64.10 xxx/], [qw/8.8.8.8 yyy/], ];

my $r = read_table_ipinfo(
    $arr, 
    0,
    write_file => '02.read_table_ipinfo_loc.csv', 
    sep => ',', 
    charset         => 'utf8',
    return_arrayref => 1,
    ipinfo_names    => [qw/state prov isp/],
    write_head => [qw/ip some state prov isp/ ], 
);
print Dumper($r);

my $asn_r = read_table_ipinfo(
    $arr, 
    0,
    write_file => '02.read_table_ipinfo_as.csv', 
    sep => ',', 
    charset         => 'utf8',
    return_arrayref => 1,
    ipinfo_file => $Simple::IPInfo::IPINFO_AS_F,
    ipinfo_names    => [qw/as/],
);
print Dumper($asn_r);

get_ipinfo_by_curl

ip info is from http://ipinfo.io

get ip location, with loc, country, isp, as number

返回IP区域信息,包括地理位置、国家、运营商、AS号

my $r = get_ipinfo_by_curl('202.38.64.10');

print Dumper($r);

ATTR

DEBUG

set $Simple::IPInfo::DEBUG = 1 for more details

BIN

system need install curl、bunzip2、tar

get ip as data from routeviews.org

1) get as data and parse it

perl parse_ip_as.pl 

2)  write as json file

perl write_ip_as.pl

get ip loc from taobao

1) init data dir, and null csv file

perl init_ip_loc.pl

2) add ask_ip_loc.pl to crontab, for example

*/5 * * * * cd ip_loc_taobao/bin && perl ask_ip_loc.pl >/dev/null 2>&1

3) merge data/*.csv, get main tidy csv

perl merge_ip_loc.pl

perl tidy_ip_loc.pl ip_loc_taobao.csv ip_loc_new.csv

4) compare with ip_loc_old.csv

perl compare_ip_loc.pl  ip_loc_old.csv ip_loc_new.csv  ip_loc_merge.csv

5) fix cn ip loc

perl fix_cn_ip_loc.pl ip_loc_merge.csv  fix_cn_ip_loc.csv

6) merge with fix_cn data

perl compare_ip_loc.pl ip_loc_merge.csv fix_cn_ip_loc.csv ip_loc.csv

7) parse ip_loc.csv, map ipc to inet range

perl parse_ip_loc.pl ip_loc.csv ip_loc_inet.csv

8) write ip_loc_inet.csv into json 

perl write_ip_loc.pl ip_loc_inet.csv IPInfo_LOC.json

AUTHOR

Abby Pan <abbypan@gmail.com>

thanks to Gabor Szabo <szabgab@gmail.com>