[英]How do I check if an ip address is in a specific range in perl?
我正在用perl編寫CGI腳本。 如何檢查IP地址(例如124.21.23.5)是否在100.0.0.0-200.79.255.255范圍內?
我獲取IP地址的方式是:
my $ip = $ENV{'REMOTE_ADDR'};
使用Net::IP
和overlaps
方法:
use strict;
use warnings;
use Net::IP;
my $range = Net::IP->new('100.0.0.0 - 200.79.255.255') or die Net::IP::Error();
while (<DATA>) {
chomp;
my $ip = Net::IP->new($_) or die Net::IP::Error();
my $match = $range->overlaps($ip) ? "(match)" : "";
print "$_ $match\n";
}
__DATA__
10.0.0.1
99.99.99.99
100.0.0.1
124.21.23.5
200.79.255.1
200.80.1.1
輸出:
10.0.0.1
99.99.99.99
100.0.0.1 (match)
124.21.23.5 (match)
200.79.255.1 (match)
200.80.1.1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.