| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package IP::IPwhere; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 15620 | use 5.006; | 
|  | 1 |  |  |  |  | 2 |  | 
| 4 | 1 |  |  | 1 |  | 5 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 26 |  | 
| 5 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 6 |  | 
|  | 1 |  |  |  |  | 46 |  | 
| 6 | 1 |  |  | 1 |  | 655 | use Data::Dumper; | 
|  | 1 |  |  |  |  | 7836 |  | 
|  | 1 |  |  |  |  | 63 |  | 
| 7 | 1 |  |  | 1 |  | 465 | use LWP::Simple; | 
|  | 1 |  |  |  |  | 44982 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 8 | 1 |  |  | 1 |  | 943 | use JSON; | 
|  | 1 |  |  |  |  | 8305 |  | 
|  | 1 |  |  |  |  | 4 |  | 
| 9 | 1 |  |  | 1 |  | 118 | use Encode; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 64 |  | 
| 10 | 1 |  |  | 1 |  | 530 | use utf8; | 
|  | 1 |  |  |  |  | 9 |  | 
|  | 1 |  |  |  |  | 4 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | our @ISA = qw(Exporter); | 
| 13 |  |  |  |  |  |  | our @EXPORT = | 
| 14 |  |  |  |  |  |  | qw(squery query getTbeIParea getSinaIParea getBaiduIParea getPcoIParea); | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | =encoding utf8 | 
| 17 |  |  |  |  |  |  | =head1 NAME | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | IP::IPwhere - IP address search whith baidu,taobao,sina,pconlie public IP API! | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | 批量ip归属地查询,调用阿里新浪、百度和pconline ip库api接口,也可以增加纯真库,单独 | 
| 22 |  |  |  |  |  |  | 查询,没有整合到本模块中。 | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | 试用方法: ./ipwhere.pl 8.8.8.8 8.8.8.6 | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | 需要安装perl及扩展LWP::Simple;use JSON; | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | 建议通过cpanm LWP::Simple JSON 一键安装。 | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | =head1 VERSION | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | Version 0.05 | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | =cut | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | our $VERSION = '0.05'; | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | Quick summary of what the module does. | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | use IP::IPwhere; | 
| 43 |  |  |  |  |  |  | print query(\@ARGV); | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | =head1 METHODS | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =head2 squery( $IP ) | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | Returns the result of query. | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | =head2 query(\@ipArr) | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | Returns the result of query for mutis IP whith the style of array res. | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | =head2  getXXXIParea | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | Returns the result of query of the special web API,include tabao,sina,baidu and pconline. | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | =cut | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | my %ipcache; | 
| 63 |  |  |  |  |  |  | my $DEBUG = 0; | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | sub squery { | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 0 |  |  | 0 | 1 |  | my $ip =vpIP(shift); | 
| 68 | 0 | 0 |  |  |  |  | return $ip if $ip=~/^IANA/; | 
| 69 | 0 |  |  |  |  |  | my $result; | 
| 70 | 0 |  |  |  |  |  | $result .= getTbeIParea($ip); | 
| 71 | 0 |  |  |  |  |  | $result .= getSinaIParea($ip); | 
| 72 | 0 |  |  |  |  |  | $result .= getBaiduIParea($ip); | 
| 73 | 0 |  |  |  |  |  | $result .= getPcoIParea($ip); | 
| 74 | 0 |  |  |  |  |  | return $result; | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub vpIP { | 
| 79 | 0 |  |  | 0 | 0 |  | my $ip=shift; | 
| 80 | 0 |  |  |  |  |  | my $re  = qr([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]); | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 0 | 0 |  |  |  |  | return "IANA非法ip" unless  $ip=~$re; | 
| 83 | 0 | 0 |  |  |  |  | return "IANA本机地址\n" if $ip=~/^127\./; | 
| 84 | 0 | 0 |  |  |  |  | return "IANA缺省网关地址\n" if $ip=~/^0\./; | 
| 85 | 0 | 0 |  |  |  |  | return "IANA广播地址\n" if $ip=~/^255\.255\.255\.255/; | 
| 86 | 0 | 0 |  |  |  |  | return "IANA组播地址\n" if $ip=~/^2(2[4-9]|3[1-9]\.)/; | 
| 87 | 0 | 0 |  |  |  |  | return "IANA本地内网地址\n" if $ip=~/^10\./; | 
| 88 | 0 | 0 |  |  |  |  | return "IANA本地内网地址\n" if $ip=~/^192\.168/; | 
| 89 | 0 | 0 |  |  |  |  | return "IANA本地内网地址\n" if $ip=~/^172\.16/; | 
| 90 | 0 | 0 |  |  |  |  | return "IANA保留地址\n" if $ip=~/^169\.254/; | 
| 91 | 0 | 0 |  |  |  |  | return "IANA保留地址\n" if $ip=~/^2(4[0-9]|5[1-5])/; | 
| 92 | 0 |  |  |  |  |  | return $ip; | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  | sub query { | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 0 |  |  | 0 | 1 |  | my $ip = shift; | 
| 98 | 0 |  |  |  |  |  | my $result; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 0 |  |  |  |  |  | for ( validIP( @{$ip} ) ) { | 
|  | 0 |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 0 |  |  |  |  |  | $result .= getTbeIParea($_); | 
| 103 | 0 |  |  |  |  |  | $result .= getSinaIParea($_); | 
| 104 | 0 |  |  |  |  |  | $result .= getBaiduIParea($_); | 
| 105 | 0 |  |  |  |  |  | $result .= getPcoIParea($_); | 
| 106 |  |  |  |  |  |  | } | 
| 107 | 0 |  |  |  |  |  | return $result; | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | sub validIP() { | 
| 111 | 0 |  |  | 0 | 0 |  | my @ip  = @_; | 
| 112 | 0 |  |  |  |  |  | my $re  = qr([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]); | 
| 113 | 0 |  |  |  |  |  | my @oip = grep { /^($re\.){3}$re$/ } @_; | 
|  | 0 |  |  |  |  |  |  | 
| 114 | 0 |  |  |  |  |  | return @oip; | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | sub gbk2utf { | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 0 |  |  | 0 | 0 |  | my $str = shift; | 
| 120 | 0 |  |  |  |  |  | return encode( "utf-8", decode( "gbk", $str ) ); | 
| 121 | 0 |  |  |  |  |  | return; | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | sub cached { | 
| 126 | 0 |  |  | 0 | 0 |  | my $ip = shift; | 
| 127 | 0 | 0 |  |  |  |  | print "DEBUG\::cached\::IN $ip\n" if $DEBUG; | 
| 128 | 0 | 0 |  |  |  |  | return $ipcache{$ip} ? 1 : 0; | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | sub clear { | 
| 132 |  |  |  |  |  |  |  | 
| 133 | 0 |  |  | 0 | 0 |  | my $ip = shift; | 
| 134 | 0 | 0 |  |  |  |  | print "DEBUG\::clear\::IN $ip\n" if $DEBUG; | 
| 135 | 0 | 0 |  |  |  |  | if ($ip) { | 
| 136 | 0 |  |  |  |  |  | undef $ipcache{$ip}; | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  | else { | 
| 139 | 0 |  |  |  |  |  | undef %ipcache; | 
| 140 |  |  |  |  |  |  | } | 
| 141 |  |  |  |  |  |  | } | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | sub getBaiduIParea() { | 
| 144 |  |  |  |  |  |  |  | 
| 145 | 0 |  |  | 0 | 0 |  | my $ip  = shift; | 
| 146 | 0 |  |  |  |  |  | my $key = "BD_" . $ip; | 
| 147 | 0 | 0 |  |  |  |  | return decode( "gbk", $ipcache{$key} ) if exists( $ipcache{$key} ); | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 0 |  |  |  |  |  | my $url = | 
| 150 |  |  |  |  |  |  | qq(http://opendata.baidu.com/api.php?query=$ip&co=&resource_id=6006&t=1433920989928&ie=utf8&oe=gbk&format=json); | 
| 151 | 0 |  |  |  |  |  | my $code = get($url); | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | #my $jso=$1 if $code =~/var remote_ip_info =(.*);$/; | 
| 154 | 0 | 0 |  |  |  |  | print $code, "\n" if $DEBUG; | 
| 155 | 0 |  |  |  |  |  | my $json = new JSON; | 
| 156 | 0 | 0 |  |  |  |  | my $obj = $json->decode($code) if defined $code; | 
| 157 | 0 | 0 |  |  |  |  | print Dumper($obj), "\n" if $DEBUG; | 
| 158 | 0 | 0 |  |  |  |  | print "baidu $_:$obj->{msg}\n" if $DEBUG; | 
| 159 | 0 |  |  |  |  |  | my $ipArea = "baidu $ip:$obj->{data}->[0]->{location}\n"; | 
| 160 | 0 |  |  |  |  |  | $ipcache{$key} = $ipArea; | 
| 161 | 0 |  |  |  |  |  | return decode( "gbk", $ipArea ); | 
| 162 |  |  |  |  |  |  | } | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | sub getPcoIParea() { | 
| 165 |  |  |  |  |  |  |  | 
| 166 | 0 |  |  | 0 | 0 |  | my $ip  = shift; | 
| 167 | 0 |  |  |  |  |  | my $key = "pco_" . $ip; | 
| 168 | 0 | 0 |  |  |  |  | return $ipcache{$key} if exists( $ipcache{$key} ); | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | #print $ip,"\n"; | 
| 171 | 0 |  |  |  |  |  | my $url  = qq(http://whois.pconline.com.cn/ipJson.jsp?callback=YSD&ip=$ip); | 
| 172 | 0 |  |  |  |  |  | my $code = get($url); | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | #print $code,"\n"; | 
| 175 | 0 | 0 |  |  |  |  | my $jso = $1 if $code =~ /\{YSD\((.*)\)\;\}$/ms; | 
| 176 |  |  |  |  |  |  |  | 
| 177 | 0 |  |  |  |  |  | my $json = new JSON; | 
| 178 | 0 | 0 |  |  |  |  | my $obj = $json->decode($jso) if $jso; | 
| 179 |  |  |  |  |  |  |  | 
| 180 | 0 |  |  |  |  |  | my $ipArea = | 
| 181 |  |  |  |  |  |  | "pconline $ip:$obj->{pro},$obj->{city},$obj->{region},$obj->{addr}\n"; | 
| 182 | 0 |  |  |  |  |  | $ipcache{$key} = $ipArea; | 
| 183 | 0 |  |  |  |  |  | return $ipArea; | 
| 184 |  |  |  |  |  |  | } | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | sub getSinaIParea() { | 
| 187 | 0 |  |  | 0 | 0 |  | my $ip  = shift; | 
| 188 | 0 |  |  |  |  |  | my $key = "SL_" . $ip; | 
| 189 | 0 | 0 |  |  |  |  | return $ipcache{$key} if exists( $ipcache{$key} ); | 
| 190 | 0 |  |  |  |  |  | my $url = | 
| 191 |  |  |  |  |  |  | qq(http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=$ip); | 
| 192 | 0 |  |  |  |  |  | my $code = get($url); | 
| 193 | 0 | 0 |  |  |  |  | my $jso = $1 if $code =~ /var remote_ip_info =(.*);$/; | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | #print $jso,"\n"; | 
| 196 | 0 |  |  |  |  |  | my $json = new JSON; | 
| 197 | 0 |  |  |  |  |  | my $obj  = $json->decode($jso); | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | #print Dumper($obj),"\n"; | 
| 200 | 0 |  |  |  |  |  | my $ipArea = | 
| 201 |  |  |  |  |  |  | "sina $ip:$obj->{country},$obj->{province},$obj->{city},$obj->{isp}\n"; | 
| 202 | 0 |  |  |  |  |  | $ipcache{$key} = $ipArea; | 
| 203 | 0 |  |  |  |  |  | return $ipArea; | 
| 204 |  |  |  |  |  |  | } | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | sub getTbeIParea() { | 
| 207 | 0 |  |  | 0 | 0 |  | my $ip  = shift; | 
| 208 | 0 |  |  |  |  |  | my $key = "TB_" . $ip; | 
| 209 | 0 | 0 |  |  |  |  | unless ( exists( $ipcache{$key} ) ) { | 
| 210 | 0 |  |  |  |  |  | my $url  = qq(http://ip.taobao.com/service/getIpInfo.php?ip=$ip); | 
| 211 | 0 |  |  |  |  |  | my $code = get($url); | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | #print Dumper($code),"\n"; | 
| 214 | 0 |  |  |  |  |  | my $json = new JSON; | 
| 215 | 0 | 0 |  |  |  |  | if (defined $code) { | 
| 216 | 0 |  |  |  |  |  | my $obj = $json->decode($code); | 
| 217 | 0 |  |  |  |  |  | my $ipArea = | 
| 218 |  |  |  |  |  |  | "taobao $ip:$obj->{data}->{country},$obj->{data}->{region},$obj->{data}->{city},$obj->{data}->{isp}\n"; | 
| 219 | 0 |  |  |  |  |  | $ipcache{$key} = $ipArea; | 
| 220 |  |  |  |  |  |  |  | 
| 221 | 0 |  |  |  |  |  | return $ipArea; | 
| 222 |  |  |  |  |  |  | } | 
| 223 | 0 |  |  |  |  |  | else { return } | 
| 224 |  |  |  |  |  |  | } | 
| 225 |  |  |  |  |  |  | else { | 
| 226 |  |  |  |  |  |  |  | 
| 227 | 0 |  |  |  |  |  | return $ipcache{$key}; | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | } | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  | } | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | =head1 AUTHOR | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | Orange, C<<  >> | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | =head1 BUGS | 
| 238 |  |  |  |  |  |  |  | 
| 239 |  |  |  |  |  |  | Please report any bugs or feature requests to C, or through | 
| 240 |  |  |  |  |  |  | the web interface at L.  I will be notified, and then you'll | 
| 241 |  |  |  |  |  |  | automatically be notified of progress on your bug as I make changes. | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  |  | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  |  | 
| 246 |  |  |  |  |  |  | =head1 SUPPORT | 
| 247 |  |  |  |  |  |  |  | 
| 248 |  |  |  |  |  |  | You can find documentation for this module with the perldoc command. | 
| 249 |  |  |  |  |  |  |  | 
| 250 |  |  |  |  |  |  | perldoc IP::IPwhere | 
| 251 |  |  |  |  |  |  |  | 
| 252 |  |  |  |  |  |  |  | 
| 253 |  |  |  |  |  |  | You can also look for information at: | 
| 254 |  |  |  |  |  |  |  | 
| 255 |  |  |  |  |  |  | =over 4 | 
| 256 |  |  |  |  |  |  |  | 
| 257 |  |  |  |  |  |  | =item * RT: CPAN's request tracker (report bugs here) | 
| 258 |  |  |  |  |  |  |  | 
| 259 |  |  |  |  |  |  | L | 
| 260 |  |  |  |  |  |  |  | 
| 261 |  |  |  |  |  |  | =item * AnnoCPAN: Annotated CPAN documentation | 
| 262 |  |  |  |  |  |  |  | 
| 263 |  |  |  |  |  |  | L | 
| 264 |  |  |  |  |  |  |  | 
| 265 |  |  |  |  |  |  | =item * CPAN Ratings | 
| 266 |  |  |  |  |  |  |  | 
| 267 |  |  |  |  |  |  | L | 
| 268 |  |  |  |  |  |  |  | 
| 269 |  |  |  |  |  |  | =item * Search CPAN | 
| 270 |  |  |  |  |  |  |  | 
| 271 |  |  |  |  |  |  | L | 
| 272 |  |  |  |  |  |  |  | 
| 273 |  |  |  |  |  |  | =back | 
| 274 |  |  |  |  |  |  |  | 
| 275 |  |  |  |  |  |  |  | 
| 276 |  |  |  |  |  |  | =head1 ACKNOWLEDGEMENTS | 
| 277 |  |  |  |  |  |  |  | 
| 278 |  |  |  |  |  |  |  | 
| 279 |  |  |  |  |  |  | =head1 LICENSE AND COPYRIGHT | 
| 280 |  |  |  |  |  |  |  | 
| 281 |  |  |  |  |  |  | Copyright 2016 Orange. | 
| 282 |  |  |  |  |  |  |  | 
| 283 |  |  |  |  |  |  | This library is free software; you can redistribute it and/or modify | 
| 284 |  |  |  |  |  |  | it under the same terms as Perl itself. | 
| 285 |  |  |  |  |  |  | =cut | 
| 286 |  |  |  |  |  |  |  | 
| 287 |  |  |  |  |  |  | 1 |