| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package IP::IPwhere; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
12885
|
use 5.006; |
|
|
1
|
|
|
|
|
3
|
|
|
4
|
1
|
|
|
1
|
|
3
|
use strict; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
15
|
|
|
5
|
1
|
|
|
1
|
|
2
|
use warnings; |
|
|
1
|
|
|
|
|
4
|
|
|
|
1
|
|
|
|
|
23
|
|
|
6
|
1
|
|
|
1
|
|
518
|
use Data::Dumper; |
|
|
1
|
|
|
|
|
6299
|
|
|
|
1
|
|
|
|
|
49
|
|
|
7
|
1
|
|
|
1
|
|
388
|
use LWP::Simple; |
|
|
1
|
|
|
|
|
43275
|
|
|
|
1
|
|
|
|
|
6
|
|
|
8
|
1
|
|
|
1
|
|
894
|
use JSON; |
|
|
1
|
|
|
|
|
7905
|
|
|
|
1
|
|
|
|
|
3
|
|
|
9
|
1
|
|
|
1
|
|
117
|
use Encode; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
53
|
|
|
10
|
1
|
|
|
1
|
|
471
|
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.04 |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=cut |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
|
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=~/(22[4-9]|23[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=~/(24[0-9]|25[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 $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 ($code) { |
|
216
|
0
|
|
|
|
|
|
my $obj = $json->decode($code); |
|
217
|
0
|
|
|
|
|
|
my $ipArea = |
|
218
|
|
|
|
|
|
|
"taobao $obj->{data}->{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 |