如何用 perl 检查一个 IP 是否在某个 IP 段里?

如何用 perl 检查一个 IP 是否在某个 IP 段里?

我现在有一个 IP 段列表,现在需要实现检查某一个 IP 是否在这个 IP 段里面,不知道用 PERL 如何实现?


QUOTE:
原帖由 chuan 于 2007-11-2 23:09 发表
我现在有一个 IP 段列表,现在需要实现检查某一个 IP 是否在这个 IP 段里面,不知道用 PERL 如何实现?

try check this module on cpan:
http://search.cpan.org/~marcel/N ... lib/Net/IP/Match.pm
举个例子? 列表是文件?


QUOTE:
原帖由 chuan 于 2007-11-2 23:09 发表
我现在有一个 IP 段列表,现在需要实现检查某一个 IP 是否在这个 IP 段里面,不知道用 PERL 如何实现?

Also I once did the same thing as you, rather than using an existing module, you can convert the ip to int at first, then do the number comparsion to find what you want.

see the module source of mime:
http://search.cpan.org/~pangj/IP-ChinaISP-0.03/lib/IP/ChinaISP.pm
thanks to pangj..

I used perl and Net:NS module to setup a dynamic dns, that will reponse the backend server's ip which near the client.

which ip database that ChinaISP module used ?


QUOTE:
I used perl and Net::DNS module to setup a dynamic dns, that will reponse the backend server's ip which near the client.

Do you mean you wrote a DNS server based on Net::DNS module?
There's already a DNS Server Framework on internet:
http://www.stanford.edu/~riepel/lbnamed/Stanford-DNSserver/
It's much easy to use it to make a dns server.

QUOTE:
which ip database that ChinaISP module used ?

got the IPs from apnic.
it's simple code to setup a dns server based on Net::DNS::Nameserver.  :)

my $ns = Net::DNS::Nameserver -> new(
        LocalAddr    => [ '127.0.0.1' ],
        LocalPort    => 53,
        ReplyHandler => \&reply_handler,
        Verbose      => 0,
) || die "couldn't create nameserver object\n";

$ns->main_loop;


example code copy from Net::DNS::Nameserver module documentation.

I know Stanford-DNSserver, but never tested. do you thing it's better than Net::DNS::Nameserver? In the pass, I just use this dns for internal query by squid to point to the right backend.

now, I want to make the dns smart. :)
Yup, I once used also Squid and smart DNS based on Stanford::DNS framework.
I didn't use Net::DNS::Server module, wish you good luck with it.