| 1 | package IP::IPwhere; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use LWP::Simple; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Encode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | our @ISA = qw(Exporter); | |||||||||
| 13 | 1 | 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 | 1 | 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 | 1 | my %ipcache; | |||||||||
| 63 | 1 | my $DEBUG = 0; | |||||||||
| 64 | |||||||||||
| 65 | sub squery { | ||||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | my $ip =vpIP(shift); | ||||||||
|
Subroutine
squery: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 68 | 0 | ▶ | return $ip if $ip=~/^IANA/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | my $ip=shift; | ||||||||
|
Subroutine
vpIP: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||
| 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 | ▶ | return "IANA非法ip" unless $ip=~$re; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 0 | ▶ | return "IANA本机地址\n" if $ip=~/^127\./; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | ▶ | return "IANA缺省网关地址\n" if $ip=~/^0\./; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | ▶ | return "IANA广播地址\n" if $ip=~/^255\.255\.255\.255/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 0 | ▶ | return "IANA组播地址\n" if $ip=~/^2(2[4-9]|3[1-9]\.)/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | ▶ | return "IANA本地内网地址\n" if $ip=~/^10\./; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | ▶ | return "IANA本地内网地址\n" if $ip=~/^192\.168/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | ▶ | return "IANA本地内网地址\n" if $ip=~/^172\.16/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 0 | ▶ | return "IANA保留地址\n" if $ip=~/^169\.254/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 0 | ▶ | return "IANA保留地址\n" if $ip=~/^2(4[0-9]|5[1-5])/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 0 | return $ip; | |||||||||
| 93 | |||||||||||
| 94 | } | ||||||||||
| 95 | sub query { | ||||||||||
| 96 | |||||||||||
| 97 | 0 | ▶ | my $ip = shift; | ||||||||
|
Subroutine
query: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 98 | 0 | my $result; | |||||||||
| 99 | |||||||||||
| 100 | 0 | for ( validIP( @{$ip} ) ) { | |||||||||
| 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 | ▶ | my @ip = @_; | ||||||||
|
Subroutine
validIP: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 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$/ } @_; | |||||||||
| 114 | 0 | return @oip; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | sub gbk2utf { | ||||||||||
| 118 | |||||||||||
| 119 | 0 | ▶ | my $str = shift; | ||||||||
|
Subroutine
gbk2utf: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 120 | 0 | return encode( "utf-8", decode( "gbk", $str ) ); | |||||||||
| 121 | 0 | return; | |||||||||
| 122 | |||||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | sub cached { | ||||||||||
| 126 | 0 | ▶ | my $ip = shift; | ||||||||
|
Subroutine
cached: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 127 | 0 | ▶ | print "DEBUG\::cached\::IN $ip\n" if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 0 | ▶ | return $ipcache{$ip} ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | sub clear { | ||||||||||
| 132 | |||||||||||
| 133 | 0 | ▶ | my $ip = shift; | ||||||||
|
Subroutine
clear: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 134 | 0 | ▶ | print "DEBUG\::clear\::IN $ip\n" if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 0 | ▶ | if ($ip) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 0 | undef $ipcache{$ip}; | |||||||||
| 137 | } | ||||||||||
| 138 | else { | ||||||||||
| 139 | 0 | undef %ipcache; | |||||||||
| 140 | } | ||||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | sub getBaiduIParea() { | ||||||||||
| 144 | |||||||||||
| 145 | 0 | ▶ | my $ip = shift; | ||||||||
|
Subroutine
getBaiduIParea: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||
| 146 | 0 | my $key = "BD_" . $ip; | |||||||||
| 147 | 0 | ▶ | return decode( "gbk", $ipcache{$key} ) if exists( $ipcache{$key} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | print $code, "\n" if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 155 | 0 | my $json = new JSON; | |||||||||
| 156 | 0 | ▶ | my $obj = $json->decode($code) if defined $code; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 0 | ▶ | print Dumper($obj), "\n" if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 158 | 0 | ▶ | print "baidu $_:$obj->{msg}\n" if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | my $ip = shift; | ||||||||
|
Subroutine
getPcoIParea: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 167 | 0 | my $key = "pco_" . $ip; | |||||||||
| 168 | 0 | ▶ | return $ipcache{$key} if exists( $ipcache{$key} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | my $jso = $1 if $code =~ /\{YSD\((.*)\)\;\}$/ms; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | |||||||||||
| 177 | 0 | my $json = new JSON; | |||||||||
| 178 | 0 | ▶ | my $obj = $json->decode($jso) if $jso; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | my $ip = shift; | ||||||||
|
Subroutine
getSinaIParea: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 188 | 0 | my $key = "SL_" . $ip; | |||||||||
| 189 | 0 | ▶ | return $ipcache{$key} if exists( $ipcache{$key} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | my $jso = $1 if $code =~ /var remote_ip_info =(.*);$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | my $ip = shift; | ||||||||
|
Subroutine
getTbeIParea: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 208 | 0 | my $key = "TB_" . $ip; | |||||||||
| 209 | 0 | ▶ | unless ( exists( $ipcache{$key} ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | if (defined $code) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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<< <bollwarm at ijz.me> >> | ||||||||||
| 236 | |||||||||||
| 237 | =head1 BUGS | ||||||||||
| 238 | |||||||||||
| 239 | Please report any bugs or feature requests to C<bug-ip-ipwhere at rt.cpan.org>, or through | ||||||||||
| 240 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IP-IPwhere>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=IP-IPwhere> | ||||||||||
| 260 | |||||||||||
| 261 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 262 | |||||||||||
| 263 | L<http://annocpan.org/dist/IP-IPwhere> | ||||||||||
| 264 | |||||||||||
| 265 | =item * CPAN Ratings | ||||||||||
| 266 | |||||||||||
| 267 | L<http://cpanratings.perl.org/d/IP-IPwhere> | ||||||||||
| 268 | |||||||||||
| 269 | =item * Search CPAN | ||||||||||
| 270 | |||||||||||
| 271 | L<http://search.cpan.org/dist/IP-IPwhere/> | ||||||||||
| 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 | 1 | |||||||||