# How to know wheter a certain IP address belongs to given network segments?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
I have a network segment list in a text file called IPsegment.txt,it looks like
this:

IPsegment.txt
219.111.192.0/18
68.132.0.0/17
67.146.0.0/16
192.162.0.0/16
152.172.0.0/16
34.132.0.0/14
97.208.0.0/13

And I have some IP addresses in another text file called IPlist.txt.It looks
like this:

IPlist.txt
www.yahoo.com,66.94.230.51
www.baidu.com,202.108.250.249
www.sina.com.cn,61.135.152.77
www.sohu.com,61.135.150.75
....

Now,I want to know whether the IP addresses in the IPlist.txt belongs the
network segment writen in the IPsegment.txt.
The mathing IP list is output in a new text file called matchinglist.txt.
It looks like this:

matchinglist.txt
www.baidu.com,202.108.250.249
www.sina.com.cn,61.135.152.77
....

How can I do that? Thank you in advance.

## Re: How to know wheter a certain IP address belongs to given network segments?

> I have a network segment list in a text file called IPsegment.txt,it looks
like
> this:
>
[snip]
>
> And I have some IP addresses in another text file called IPlist.txt.It
looks
> like this:
>
[snip]
>
> Now,I want to know whether the IP addresses in the IPlist.txt belongs the
> network segment writen in the IPsegment.txt.
> The mathing IP list is output in a new text file called matchinglist.txt.
> It looks like this:
>
[snip]
>
> How can I do that? Thank you in advance.

By splitting the fqdn and IPs into key/value pairs in a hash and then
checking the existence of the address against the hash. In the future,
please make an effort to solve your own problems before postng, and then if
you run into a specific problem you can always ask here.

Matt

## Re: How to know wheter a certain IP address belongs to given network segments?

> I have a network segment list in a text file called IPsegment.txt,it looks
like
> this:
>
> IPsegment.txt
> 219.111.192.0/18
> 68.132.0.0/17
> 67.146.0.0/16
> 192.162.0.0/16
> 152.172.0.0/16
> 34.132.0.0/14
> 97.208.0.0/13
>
>
> And I have some IP addresses in another text file called IPlist.txt.It
looks
> like this:
>
> IPlist.txt
> www.yahoo.com,66.94.230.51
> www.baidu.com,202.108.250.249
> www.sina.com.cn,61.135.152.77
> www.sohu.com,61.135.150.75
> ...
>
> Now,I want to know whether the IP addresses in the IPlist.txt belongs the
> network segment writen in the IPsegment.txt.
> The mathing IP list is output in a new text file called matchinglist.txt.
> It looks like this:
>
> matchinglist.txt
> www.baidu.com,202.108.250.249
> www.sina.com.cn,61.135.152.77
> ...
>
>
> How can I do that? Thank you in advance.

This is not a Perl question.

That said, you can find several helpful tutorials with a Google search for
"ip addressing subnetting". Adding "Perl" to the search string shows at
least
one attempt at an IP address and subnet conversion calculator, done with
Perl.

One of the links that may be helpful to you is:

http://mipagina.cantv.net/lem/perl/iptut.htm

Search on the above page for "Matching against your address space" to see
how the module "NetAddr::IP" is used to solve a problem similar to (a part
of) your problem.

When you have a Perl problem, first read and comply with the Posting
Guidelines, then ask your Perl question.

Cheers.
--
Bill Segraves