Solved

Quick Perl script to grep keywords

Posted on 2004-09-24
7
335 Views
Last Modified: 2012-06-27
Hello,

Can someone show me a quick and dirty perl script that can take a file with a range of IPs and grep out IPs that are specified in another file?  For instance, I have 192.168.0.0-192.168.255.255 and I want to filter out about 100 IPs from that range and make a new file with those IPs still remaining.  Thanks for any help!

//j
0
Comment
Question by:dev8
  • 4
  • 2
7 Comments
 
LVL 5

Expert Comment

by:ZiaTioN
Comment Utility
I think I understand what you want. If you are going to be dealing with more than just the last octet of the IP addresses then more code would be required.

#!/usr/bin/perl -w

die "Required user input missing!\n" if ($#ARGV < 0);
my $start = $ARGV[0];
my $end   = $ARGV[1];

open(FILE, "<", "ip.txt") || die "Can't open file: $!\n";
open(OUT, ">>", "out.txt") || die "Can't open out.txt: $!\n";
while (<FILE>) {
   my $i = $1 if (/\d+\.\d+\.\d+\.(\d+)/);
   print OUT if (($i <= $end) && ($i >= $start));
}
close(OUT);
close(FILE);
print "Done!\n";


Hope this helps ya!
0
 
LVL 5

Expert Comment

by:ZiaTioN
Comment Utility
Oh I forgot to mention that I assumed the source IP address list would be delimited by newlines (\n) in the file, meaning one IP address on each line. If this is not the case this code will need to be altered.
0
 
LVL 2

Author Comment

by:dev8
Comment Utility
ZiaTioN,

With the above script, I specified with input already in it like so:

ip.pl ip.txt

The resulting file was an out.txt that returned the same information that was in the ip.txt file.  I ran it again and it just appended the same info in ip.txt to the existing out.txt. I was hoping that it would work like the following:

ip.pl ip.txt remove.txt

ip.pl is the script
ip.txt is where a list of IPs exist
remove.txt contains the list of IPs to remove from the ip.txt
a resulting file by the name of "out.txt" would be created and show the remaining IPs after specified IPs (in the remove.txt) were removed.  

Thanks for any more help!

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Accepted Solution

by:
ZiaTioN earned 250 total points
Comment Utility
Well first off you are suppose to supply the range of IP's to be filtered as command line arguments to the script above.

However I was not aware you wanted to remove these from the source list. Is there a specific reason why you want a list (remove.txt) of the filtered IP's? Or do you simply want to remove them and leave the newly edited list?

Oh and it appended to the out.txt the second time you ran it because I used the append singifyer ">>" in the three-arg-open call.

Either way here is what you asked for:
#!/usr/bin/perl -w

die "Required user input missing!\n" if ($#ARGV < 1);
die "User input not in numeric format!\n" if (($ARGV[0] =~ /\D+/) || ($ARGV[1] =~ /\D+/));
my $start = $ARGV[0];
my $end   = $ARGV[1];

open(FILE, "<", "ip.txt") || die "Can't open file: $!\n";
open(REM, ">", "remove.txt") || die "Can't open remove.txt: $!\n";
open(OUT, ">", "out.txt") || die "Can't open out.txt: $!\n";
while (<FILE>) {
   my $i = $1 if (/\d+\.\d+\.\d+\.(\d+)/);
   print REM if (($i <= $end) && ($i >= $start));
   print OUT if (($i > $end) || ($i < $start));
}
close(OUT);
close(REM);
close(FILE);
print "Done!\n";

Again the user input is the range of IP's to be filtered:

Ex:
ip.pl 5 100

This will filter out all IP addresses that have a 4th octet between 5 and 100 and leave all those under 5 and over 100 in out.txt.

And again number two: you will have to add more code to this if you are dealing with more than 255 address of the same subnet. This script only parses the 4th octet.
0
 
LVL 2

Author Comment

by:dev8
Comment Utility
Thanks Zia, that worked for me!
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
  my $i = $1 if (/\d+\.\d+\.\d+\.(\d+)/);
careful, if the regex does not match, $i will retain it's previous value
0
 
LVL 5

Expert Comment

by:ZiaTioN
Comment Utility
Actually it will not. With the "my" inside the loop each iteration re-initializes the variable (to null). All that will happen if the pattern does not find a match is that $i will remain undefined and the script will generate en error message complaining about an undefined variable in comparison (lt, gt, etc).
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
String Substitution 4 65
Perl strange behaviour 5 63
Awk Question 2 116
Replace  text in a file 2 83
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now