line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package IP::IPwhere; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
55115
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
15
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
9
|
|
|
1
|
|
|
|
|
29
|
|
6
|
1
|
|
|
1
|
|
510
|
use Data::Dumper; |
|
1
|
|
|
|
|
5619
|
|
|
1
|
|
|
|
|
48
|
|
7
|
1
|
|
|
1
|
|
387
|
use LWP::Simple; |
|
1
|
|
|
|
|
56101
|
|
|
1
|
|
|
|
|
4
|
|
8
|
1
|
|
|
1
|
|
854
|
use JSON; |
|
1
|
|
|
|
|
8298
|
|
|
1
|
|
|
|
|
5
|
|
9
|
1
|
|
|
1
|
|
92
|
use Encode; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
53
|
|
10
|
1
|
|
|
1
|
|
473
|
use utf8; |
|
1
|
|
|
|
|
16
|
|
|
1
|
|
|
|
|
5
|
|
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.06'; |
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
|
|
|
|
|
|
|
#$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
|
|
|
|
|
|
|
# $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
|
0
|
|
|
|
|
|
my $json = new JSON; |
195
|
0
|
|
|
|
|
|
my $obj = $json->decode($jso); |
196
|
0
|
|
|
|
|
|
my $ipArea = |
197
|
|
|
|
|
|
|
"sina $ip:$obj->{country},$obj->{province},$obj->{city},$obj->{isp}\n"; |
198
|
0
|
|
|
|
|
|
$ipcache{$key} = $ipArea; |
199
|
0
|
|
|
|
|
|
return $ipArea; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
sub getTbeIParea() { |
203
|
0
|
|
|
0
|
0
|
|
my $ip = shift; |
204
|
0
|
|
|
|
|
|
my $key = "TB_" . $ip; |
205
|
0
|
0
|
|
|
|
|
unless ( exists( $ipcache{$key} ) ) { |
206
|
0
|
|
|
|
|
|
my $url = qq(http://ip.taobao.com/service/getIpInfo.php?ip=$ip); |
207
|
0
|
|
|
|
|
|
my $code = get($url); |
208
|
0
|
|
|
|
|
|
my $json = new JSON; |
209
|
0
|
0
|
|
|
|
|
if (defined $code) { |
210
|
0
|
|
|
|
|
|
my $obj = $json->decode($code); |
211
|
0
|
|
|
|
|
|
my $ipArea = |
212
|
|
|
|
|
|
|
"taobao $ip:$obj->{data}->{country},$obj->{data}->{region},$obj->{data}->{city},$obj->{data}->{isp}\n"; |
213
|
0
|
|
|
|
|
|
$ipcache{$key} = $ipArea; |
214
|
|
|
|
|
|
|
|
215
|
0
|
|
|
|
|
|
return $ipArea; |
216
|
|
|
|
|
|
|
} |
217
|
0
|
|
|
|
|
|
else { return } |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
else { |
220
|
|
|
|
|
|
|
|
221
|
0
|
|
|
|
|
|
return $ipcache{$key}; |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head1 AUTHOR |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
Orange, C<< >> |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head1 BUGS |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
234
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
235
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=head1 SUPPORT |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
perldoc IP::IPwhere |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
You can also look for information at: |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=over 4 |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
L |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
L |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=item * CPAN Ratings |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
L |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=item * Search CPAN |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
L |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=back |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
Copyright 2016 Orange. |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
278
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
279
|
|
|
|
|
|
|
=cut |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
1 |