Home > Archive > PERL Beginners > March 2004 > Matching ranges of IP addresses
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Matching ranges of IP addresses
|
|
| Jason Price 2004-03-31, 4:31 pm |
| I am working on a script that searches through a log file, looking for =
IP matches based on several ranges of IPs. I'm trying to find a way to =
do something like this:
@results =3D grep /192.168.0.[192-254]/, @list
Which, obviously, doesn't work. Is there any way to specify a range of =
multi-digit numbers in a regular expression?
Thanks.
Jason
| |
| James Edward Gray II 2004-03-31, 5:33 pm |
| On Mar 31, 2004, at 2:50 PM, Price, Jason (TLR Corp) wrote:
> I am working on a script that searches through a log file, looking for
> IP matches based on several ranges of IPs. I'm trying to find a way
> to do something like this:
>
> @results = grep /192.168.0.[192-254]/, @list
@results = grep m/192.168.0.\d{3}/ and $1 >= 192 and $1 <= 254, @list;
How's that?
> Which, obviously, doesn't work. Is there any way to specify a range
> of multi-digit numbers in a regular expression?
@results = grep m/192.168.0.(?:19[2-9]|2[0-4]\d|25[0-4])/, @list;
I think that works too, but I don't think it's as simple as the above.
Season to taste. ;)
James
| |
| Paul Johnson 2004-03-31, 5:33 pm |
| On Wed, Mar 31, 2004 at 02:50:30PM -0600, Price, Jason (TLR Corp) wrote:
> I am working on a script that searches through a log file, looking for
> IP matches based on several ranges of IPs. I'm trying to find a way
> to do something like this:
>
> @results = grep /192.168.0.[192-254]/, @list
>
> Which, obviously, doesn't work. Is there any way to specify a range
> of multi-digit numbers in a regular expression?
Does it really need to be a regular expression?
@results = grep /192.168.0.(\d+)/ && $1 >= 192 && $1 <= 254, @list;
--
Paul Johnson - paul@pjcj.net
http://www.pjcj.net
| |
| James Edward Gray II 2004-03-31, 5:33 pm |
| On Mar 31, 2004, at 3:02 PM, James Edward Gray II wrote:
> On Mar 31, 2004, at 2:50 PM, Price, Jason (TLR Corp) wrote:
>
>
> @results = grep m/192.168.0.\d{3}/ and $1 >= 192 and $1 <= 254, @list;
Sorry to reply to my own message, but I believe I made a mistake up
there. I think you have to switch those ands to &&s. Sorry about
that.
James
| |
| James Edward Gray II 2004-03-31, 5:33 pm |
|
On Mar 31, 2004, at 3:09 PM, James Edward Gray II wrote:
> On Mar 31, 2004, at 3:02 PM, James Edward Gray II wrote:
>
>
> Sorry to reply to my own message, but I believe I made a mistake up
> there. I think you have to switch those ands to &&s. Sorry about
> that.
Egad, I forget the parens around the \d{3} too. Use Paul's solution.
It's what I meant, minus about five mistakes. I must be dumb today.
James
| |
| Jason Price 2004-03-31, 5:33 pm |
| Thanks to both James and Paul - that solution worked great.
Thanks!
Jason
-----Original Message-----
From: James Edward Gray II [mailto:james@grayproductions.net]
Sent: Wednesday, March 31, 2004 3:17 PM
To: Perl Beginners
Cc: Price, Jason (TLR Corp)
Subject: Re: Matching ranges of IP addresses
On Mar 31, 2004, at 3:09 PM, James Edward Gray II wrote:
> On Mar 31, 2004, at 3:02 PM, James Edward Gray II wrote:
>
[color=darkred]
@list;[color=darkred]
>
> Sorry to reply to my own message, but I believe I made a mistake up=20
> there. I think you have to switch those ands to &&s. Sorry about=20
> that.
Egad, I forget the parens around the \d{3} too. Use Paul's solution. =20
It's what I meant, minus about five mistakes. I must be dumb today.
James
| |
| Smoot Carl-Mitchell 2004-03-31, 5:34 pm |
| On Wed, 31 Mar 2004 23:09:28 +0200
Paul Johnson <paul@pjcj.net> wrote:
> On Wed, Mar 31, 2004 at 02:50:30PM -0600, Price, Jason (TLR Corp)
> wrote:
>
>
> Does it really need to be a regular expression?
You might consider writing something using the Socket package. It
has a routine, inet_aton to convert an Internet address in dotted
decimal to its numeric representation. You can then convert your initial
address range to two numbers and then convert the dotted decimal
representation of each IP address to a number and do a direct
numeric comparison.
You might also take a look at the CPAN module Net::IP::Match. It looks
like it does what you want to do unless you really want to code up your
own solution from scratch.
--
Smoot Carl-Mitchell
Systems/Network Architect
email: smoot@tic.com
cell: +1 602 421 9005
home: +1 480 922 7313
| |
| John W. Krahn 2004-03-31, 6:36 pm |
| James Edward Gray II wrote:
>
> On Mar 31, 2004, at 3:09 PM, James Edward Gray II wrote:
>
>
> Egad, I forget the parens around the \d{3} too. Use Paul's solution.
> It's what I meant, minus about five mistakes. I must be dumb today.
And you (and Paul) forgot to backslash the dots.
@results = grep { /192\.168\.0\.(\d{3})/ and $1 >= 192 and $1 <= 254 } @list;
John
--
use Perl;
program
fulfillment
|
|
|
|
|