Perl breaking my balls....
Hello all,
Because I want to be a good sysadmin I figured I should wrap my head around perl sooner or later. Now, I don't know if this is because I learned python first or what, but I am having a hell of a time figuring anything out at all. I have been wanting to write a script that parses my exim logs and uses it to create a (sort-of) real time ip ban list from the results, so I decided to try in perl. It has taken me the better part of today, and all I have to show is about 20 lines of working code. What I am stuck on presently is dealing with arrays. I have two arrays, lets say @already_banned and @to_be_banned, both which are made up of 1 or more ip addresses. I need to loop over the values of @to_be_banned, and remove them if they also exist in @already_banned. I went and bought both Learning Perl, and Programming Perl, and neither book has helped with what I want (or anything else for that matter...). Everything I have tried has either bombed with cryptic error messages, or worse, run successfully, but not done what I want.... To tell the truth I haven't even been able to figure out how to check an array for membership? Why is this seemingly simple task so difficult? I found a section in the perl cookbook titled "Finding Elements in One Array but Not Another" which appears to be what I want...but when I tried to integrate it...the program ran but did nothing (ie: the target ips were not removed...) I have also tried "if ( $_ in @array) {" type of construct but perl tells me that "'in' is uninitialized"?!?!? I just don't get it. Does anyone feel like taking the time to help me out? I don't want you to write my code, but I have been stuck at this spot for several hours now, and I'm about to say to hell with perl and do it in python (which if I had I would have been done hours ago) Again, I want to learn perl...there must be something basic I am missing.... |
This is what foreach is good for, e.g.:
Code:
$found = 0; |
I'm so lost that I am not sure how your example relates to what I want to do here. I'm going to post my code so you can see what I have so far:
Code:
#!/usr/bin/perl So for this last code stanza, I guess the psuedo code is: Code:
foreach $ip_address (@ip_to_ban) { |
You probably solved this, but here it goes.
I started off from where you said you have a problem. You should recognise most of the code as yours, it only has small changes: Code:
$i = 0; #increment to keep track of index in @rbls Try it, it should do what you want. |
Hey thanks for the help. I understand your code quite well, unfortunetly It is still not removing the banned ips from @rbls.
I have two banned ips on the system I'm testing this on...when I add: Code:
foreach $b (@banned) { Code:
24.66.229.173 # output of the print @banned array This is my biggest frustration with perl so far...that so many times the code will run without error but still not work properly. This gives you no clue as to why it isn't working. |
Hmm I have it working flawlessly with your list. Save and run the code below (perl some_name.pl):
Code:
@rbls=( Quote:
|
I think I know what your problem is. It looks like each IP you push onto the array carries a newline character at the end. Strip that with the chomp function rpior to pushing onto @rbls. Then there will not be a break like you have in the output:
Quote:
Quote:
|
Yes, thank you so much....
I changed: Code:
@lines = <LOG>; # to... Thanks again for all the help |
Re: Perl breaking my balls....
Quote:
First of all, don't despair - with a little help from a good book, such as "Learning Perl", you should be up and running fairly quickly. My suggestion is to simply start reading it, from the beginning. Don't jump to chapter 2 before reading chapter 1... If you follow the book as it is laid out, your confusion should quickly disappear. As for your question, to iterate over an array, one way is to use the foreach function. So for example: Code:
my @new_ip_array; At the end of this code, the @new_ip_array will contain those IPs that were in @to_be_banned but not in @already_banned. If you have any questions about the code, feel free to ask. Quote:
|
Quote:
I couldn't agree more :-) As a Perl beginner, always use the "-w" switch. I would also suggest to use "use strict;" i.e.: Code:
#! /usr/bin/perl -w |
Quote:
Quote:
Code:
for ip in to_be_banned: Quote:
Code:
# NOT Perl 6 code... As for -w and use strict that is very good advice and I will use them from now on.... |
All times are GMT -5. The time now is 06:19 AM. |