| 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 | |||||||||||
| 11 | |||||||||||
| 12 | |||||||||||
| 13 | 1 | our @ISA = qw(Exporter); | |||||||||
| 14 | 1 | our @EXPORT = qw(squery query getTbeIParea getSinaIParea getBaiduIParea getPcoIParea); | |||||||||
| 15 | |||||||||||
| 16 | =head1 NAME | ||||||||||
| 17 | |||||||||||
| 18 | IP::IPwhere - IP address search whith baidu,taobao,sina,pconlie public IP API! | ||||||||||
| 19 | |||||||||||
| 20 | 批量ip归属地查询,调用阿里新浪、百度和pconline ip库api接口,也可以增加纯真库,单独 | ||||||||||
| 21 | 查询,没有整合到本模块中。 | ||||||||||
| 22 | |||||||||||
| 23 | 试用方法: ./ipwhere.pl 8.8.8.8 8.8.8.6 | ||||||||||
| 24 | |||||||||||
| 25 | 需要安装perl及扩展LWP::Simple;use JSON; | ||||||||||
| 26 | |||||||||||
| 27 | 建议通过cpanm LWP::Simple JSON 一键安装。 | ||||||||||
| 28 | |||||||||||
| 29 | =head1 VERSION | ||||||||||
| 30 | |||||||||||
| 31 | Version 0.01 | ||||||||||
| 32 | |||||||||||
| 33 | =cut | ||||||||||
| 34 | |||||||||||
| 35 | 1 | our $VERSION = '0.01'; | |||||||||
| 36 | |||||||||||
| 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 EXPORT | ||||||||||
| 47 | |||||||||||
| 48 | |||||||||||
| 49 | =cut | ||||||||||
| 50 | |||||||||||
| 51 | |||||||||||
| 52 | 1 | my %ipcache; | |||||||||
| 53 | 1 | my $DEBUG=0; | |||||||||
| 54 | |||||||||||
| 55 | sub squery { | ||||||||||
| 56 | |||||||||||
| 57 | 0 | ▶ | my $ip=shift; | ||||||||
|
Subroutine
squery: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 58 | 0 | my $result; | |||||||||
| 59 | 0 | $result.=getTbeIParea($ip); | |||||||||
| 60 | 0 | $result.=getSinaIParea($ip); | |||||||||
| 61 | 0 | $result.=getBaiduIParea($ip); | |||||||||
| 62 | 0 | $result.=getPcoIParea($ip); | |||||||||
| 63 | 0 | return $result; | |||||||||
| 64 | |||||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | sub query { | ||||||||||
| 68 | |||||||||||
| 69 | 0 | ▶ | my $ip=shift; | ||||||||
|
Subroutine
query: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 70 | 0 | my $result; | |||||||||
| 71 | |||||||||||
| 72 | 0 | for(validIP(@{$ip})){ | |||||||||
| 73 | |||||||||||
| 74 | 0 | $result.=getTbeIParea($_); | |||||||||
| 75 | 0 | $result.=getSinaIParea($_); | |||||||||
| 76 | 0 | $result.=getBaiduIParea($_); | |||||||||
| 77 | 0 | $result.=getPcoIParea($_); | |||||||||
| 78 | } | ||||||||||
| 79 | 0 | return $result; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | sub validIP(){ | ||||||||||
| 83 | 0 | ▶ | my @ip=@_; | ||||||||
|
Subroutine
validIP: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 84 | 0 | my $re=qr([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]); | |||||||||
| 85 | 0 | my @oip=grep{/^($re\.){3}$re$/} @_; | |||||||||
| 86 | 0 | return @oip; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | sub gbk2utf { | ||||||||||
| 90 | |||||||||||
| 91 | 0 | ▶ | my $str=shift; | ||||||||
|
Subroutine
gbk2utf: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 92 | 0 | return encode("utf-8", decode("gbk", $str)); | |||||||||
| 93 | 0 | return; | |||||||||
| 94 | |||||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | |||||||||||
| 98 | sub cached { | ||||||||||
| 99 | 0 | ▶ | my $ip = shift; | ||||||||
|
Subroutine
cached: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 100 | 0 | ▶ | print "DEBUG\::cached\::IN $ip\n" if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 0 | ▶ | return $ipcache{$ip} ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | sub clear { | ||||||||||
| 105 | |||||||||||
| 106 | 0 | ▶ | my $ip = shift; | ||||||||
|
Subroutine
clear: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 107 | 0 | ▶ | print "DEBUG\::clear\::IN $ip\n" if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 0 | ▶ | if ($ip) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 0 | undef $ipcache{$ip}; | |||||||||
| 110 | } | ||||||||||
| 111 | else { | ||||||||||
| 112 | 0 | undef %ipcache; | |||||||||
| 113 | } | ||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | sub getBaiduIParea() | ||||||||||
| 117 | { | ||||||||||
| 118 | |||||||||||
| 119 | 0 | ▶ | my $ip=shift; | ||||||||
|
Subroutine
getBaiduIParea: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||
| 120 | 0 | my $key="BD_".$ip; | |||||||||
| 121 | 0 | ▶ | return decode("gbk",$ipcache{$key}) if exists($ipcache{$key}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | |||||||||||
| 123 | 0 | my $url=qq(http://opendata.baidu.com/api.php?query=$ip&co=&resource_id=6006&t=1433920989928&ie=utf8&oe=gbk&format=json); | |||||||||
| 124 | 0 | my $code = get($url); | |||||||||
| 125 | #my $jso=$1 if $code =~/var remote_ip_info =(.*);$/; | ||||||||||
| 126 | 0 | ▶ | print $code,"\n" if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | 0 | my $json = new JSON; | |||||||||
| 128 | 0 | ▶ | my $obj = $json->decode($code) if $code; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | 0 | ▶ | print Dumper($obj),"\n" if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | 0 | ▶ | print "baidu $_:$obj->{msg}\n" if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | 0 | my $ipArea="baidu $ip:$obj->{data}->[0]->{location}\n"; | |||||||||
| 132 | 0 | $ipcache{$key}=$ipArea; | |||||||||
| 133 | 0 | return decode("gbk",$ipArea); | |||||||||
| 134 | } | ||||||||||
| 135 | sub getPcoIParea() | ||||||||||
| 136 | { | ||||||||||
| 137 | |||||||||||
| 138 | 0 | ▶ | my $ip=shift; | ||||||||
|
Subroutine
getPcoIParea: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 139 | 0 | my $key="pco_".$ip; | |||||||||
| 140 | 0 | ▶ | return $ipcache{$key} if exists($ipcache{$key}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | #print $ip,"\n"; | ||||||||||
| 142 | 0 | my $url=qq(http://whois.pconline.com.cn/ipJson.jsp?callback=YSD&ip=$ip); | |||||||||
| 143 | 0 | my $code = get($url); | |||||||||
| 144 | #print $code,"\n"; | ||||||||||
| 145 | 0 | ▶ | my $jso=$1 if $code =~/\{YSD\((.*)\)\;\}$/ms; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | #print $jso,"\n"; | ||||||||||
| 147 | 0 | my $json = new JSON; | |||||||||
| 148 | 0 | ▶ | my $obj = $json->decode($jso) if $jso; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 149 | #print Dumper($obj),"\n"; | ||||||||||
| 150 | #print "pconline $_:$obj->{msg}\n if $DEBUG"; | ||||||||||
| 151 | 0 | my $ipArea="pconline $ip:$obj->{pro},$obj->{city},$obj->{region},$obj->{addr}\n"; | |||||||||
| 152 | 0 | $ipcache{$key}=$ipArea; | |||||||||
| 153 | 0 | return $ipArea; | |||||||||
| 154 | } | ||||||||||
| 155 | sub getSinaIParea() | ||||||||||
| 156 | { | ||||||||||
| 157 | 0 | ▶ | my $ip=shift; | ||||||||
|
Subroutine
getSinaIParea: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 158 | 0 | my $key="SL_".$ip; | |||||||||
| 159 | 0 | ▶ | return $ipcache{$key} if exists($ipcache{$key}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 160 | 0 | my $url=qq(http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=$ip); | |||||||||
| 161 | 0 | my $code = get($url); | |||||||||
| 162 | 0 | ▶ | my $jso=$1 if $code =~/var remote_ip_info =(.*);$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 163 | #print $jso,"\n"; | ||||||||||
| 164 | 0 | my $json = new JSON; | |||||||||
| 165 | 0 | my $obj = $json->decode($jso); | |||||||||
| 166 | #print Dumper($obj),"\n"; | ||||||||||
| 167 | 0 | my $ipArea="sina $ip:$obj->{country},$obj->{province},$obj->{city},$obj->{isp}\n"; | |||||||||
| 168 | 0 | $ipcache{$key}=$ipArea; | |||||||||
| 169 | 0 | return $ipArea; | |||||||||
| 170 | } | ||||||||||
| 171 | |||||||||||
| 172 | sub getTbeIParea(){ | ||||||||||
| 173 | 0 | ▶ | my $ip=shift; | ||||||||
|
Subroutine
getTbeIParea: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 174 | 0 | my $key="TB_".$ip; | |||||||||
| 175 | 0 | ▶ | unless (exists($ipcache{$key})){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | 0 | my $url=qq(http://ip.taobao.com/service/getIpInfo.php?ip=$ip); | |||||||||
| 177 | 0 | my $code = get($url); | |||||||||
| 178 | #print Dumper($code),"\n"; | ||||||||||
| 179 | 0 | my $json = new JSON; | |||||||||
| 180 | 0 | ▶ | if ($code) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 181 | 0 | my $obj = $json->decode($code); | |||||||||
| 182 | 0 | my $ipArea="taobao $obj->{data}->{ip}:$obj->{data}->{country},$obj->{data}->{region},$obj->{data}->{city},$obj->{data}->{isp}\n"; | |||||||||
| 183 | 0 | $ipcache{$key}=$ipArea; | |||||||||
| 184 | |||||||||||
| 185 | 0 | return $ipArea; | |||||||||
| 186 | 0 | }else {return} | |||||||||
| 187 | }else { | ||||||||||
| 188 | |||||||||||
| 189 | 0 | return $ipcache{$key}; | |||||||||
| 190 | |||||||||||
| 191 | } | ||||||||||
| 192 | |||||||||||
| 193 | } | ||||||||||
| 194 | =head1 AUTHOR | ||||||||||
| 195 | |||||||||||
| 196 | Orange, C<< <bollwarm at ijz.me> >> | ||||||||||
| 197 | |||||||||||
| 198 | =head1 BUGS | ||||||||||
| 199 | |||||||||||
| 200 | Please report any bugs or feature requests to C<bug-ip-ipwhere at rt.cpan.org>, or through | ||||||||||
| 201 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IP-IPwhere>. I will be notified, and then you'll | ||||||||||
| 202 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 203 | |||||||||||
| 204 | |||||||||||
| 205 | |||||||||||
| 206 | |||||||||||
| 207 | =head1 SUPPORT | ||||||||||
| 208 | |||||||||||
| 209 | You can find documentation for this module with the perldoc command. | ||||||||||
| 210 | |||||||||||
| 211 | perldoc IP::IPwhere | ||||||||||
| 212 | |||||||||||
| 213 | |||||||||||
| 214 | You can also look for information at: | ||||||||||
| 215 | |||||||||||
| 216 | =over 4 | ||||||||||
| 217 | |||||||||||
| 218 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||
| 219 | |||||||||||
| 220 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=IP-IPwhere> | ||||||||||
| 221 | |||||||||||
| 222 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 223 | |||||||||||
| 224 | L<http://annocpan.org/dist/IP-IPwhere> | ||||||||||
| 225 | |||||||||||
| 226 | =item * CPAN Ratings | ||||||||||
| 227 | |||||||||||
| 228 | L<http://cpanratings.perl.org/d/IP-IPwhere> | ||||||||||
| 229 | |||||||||||
| 230 | =item * Search CPAN | ||||||||||
| 231 | |||||||||||
| 232 | L<http://search.cpan.org/dist/IP-IPwhere/> | ||||||||||
| 233 | |||||||||||
| 234 | =back | ||||||||||
| 235 | |||||||||||
| 236 | |||||||||||
| 237 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 238 | |||||||||||
| 239 | |||||||||||
| 240 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 241 | |||||||||||
| 242 | Copyright 2016 Orange. | ||||||||||
| 243 | |||||||||||
| 244 | This library is free software; you can redistribute it and/or modify | ||||||||||
| 245 | it under the same terms as Perl itself. | ||||||||||
| 246 | =cut | ||||||||||
| 247 | 1 | 1 | |||||||||