Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I check a file for a string? 1

Status
Not open for further replies.

countrydj

ISP
Sep 27, 2002
21
GB
Hi..
I am very new to perl.
I am getting a lot of unwanted visitors posting obsenities on my notice board and other such boards.

At the moment I have a list of ip numbers within the script:
$remote_ip = $ENV{REMOTE_ADDR};
if(
($remote_ip eq "194.83.57.154")
|| ($remote_ip eq "121.127.81.60")
|| ($remote_ip eq "121.143.108.207")
|| ($remote_ip eq "124.51.183.147")
|| ($remote_ip eq "125.182.104.63")
|| ($remote_ip eq "140.109.40.252")
etc...
{
exit;
}

I have a list of ip numbers and would like to put them into a text file so that all scripts can use the same list of unwanted ip numbers.
e.g
$badIps = "bad-ip-file.txt";
if( search-for-ip($remote_ip, $badIps) )

{
exit;
}

Can anybody help me by telling me how to code it???

Thanks

John C


 

Code:
open (FILE, "./file/to/read.txt") or die "Can't open file: $!";
my @lines = <FILE>;
close (FILE);

chomp @lines; # remove newline chars from all the lines

foreach my $line (@lines) {
   if ($ENV{REMOTE_ADDR} eq $line) {
      croak;
   }
}

and put one IP on each line

-------------
Cuvou.com | The NEW Kirsle.net
 
Keep in mind that if any of these IPs are either a dial-up connection or a company (the first is a City Council in the UK) that you're not only blocking the person that made the remark, but anyone else that attempts to user your website from that location.

- George
 
Hi Kirsle
Thanks very much for your reply.
This helps me such a lot.

Can you tell me, is it possible to read a file on another server?
i.e.
open (FILE, "remote server/read.txt")

I have been testing and I can't make it work.

Thanks,


John C
 
Hi Rieekan..
I am aware of the problem of blocking all comers from the same IP number.
However, I'm not just talking about emails and postings that I don't want, I'm referring to porn and viagra postings, and the like.
The trap that I use offers the sender to send a message by normal email if they feel that they have been unfairly penalised.
But thanks for the reminder.

John C
 
countrydj: You can read a file from a remote server in a couple of ways.

If you're talking about local servers, i.e. within the same LAN, you can share folders and access them like normal...

i.e. Windows servers
Code:
open (READ, "\\\\servername\\sharename"); # \\servername\sharename, but escape the \'s

If we're talking about servers that are far enough away that the only way to get to them is probably via HTTP, you can use LWP.

For a simple request, LWP::Simple is sufficient:

Code:
use LWP::Simple;
my $data = get "[URL unfurl="true"]http://server.ext/file.txt";[/URL]
my @lines = split(/\n/, $data);

Keep in mind the differences:
Code:
my @lines = <FILEHANDLE>;
This, when used on a local opened filehandle, returns an array containing each line in the text file (you can also loop on the filehandle, but that's another topic).

Code:
my $data = get "[URL unfurl="true"]http://url/file.html";[/URL]
This puts ALL of the data from "file.html" into $data, including all the newline characters. It's equivalent to going to this URL in your browser and clicking "View Source". So to turn this into an array of lines, like it was when you read in a local file, you split it at the \n (newline) characters.

-------------
Cuvou.com | The NEW Kirsle.net
 
Hi travs69...
I have captch on one of my boards and it stopped a lot of junk.
I am thinking seriously of introducing it on all boards and forms. One problem is that I'm not fully conversant with it yet (being a novice).
But thanks for the advice.

John C
 
Hi Kirsle...
Many thanks for all your advice.
I do have two situations that apply here. One is a server on the same subnet and the other is a remote server.
I will try them out very soon.
Once again, many thanks for your trouble.

John C
 
Hi Kirsle...

I have now completed my tests.
I found that I can use:
my $data = get "on each server, including the local server.
Is this likely to cause any problem, other that a small time delay ???

Thanks very much for all your help and advice.

Regards,

John C
 
If the file is very large, or it's being accessed too often, it might slow down your servers because of the bandwidth. Or if one of the servers goes down or stops their HTTP daemon, the file won't be accessible. But for the most part you shouldn't run into too many problems with it.

-------------
Cuvou.com | The NEW Kirsle.net
 
Hi Kirsle...

Thanks very much for all your help and advise.
It is very much appreciated.

Regards,

John CV
 
Hi Kirsle...
I have another problem that I hope you can help me with:

I have a form for adding a notice to a notice board which is passed to a perl script.
I am constantly being inundated with spam url links on the notice board which I want to stop.

Can anybody tell me how I can check the form $notice for the first occurance of http:// and https:// and www. In order to block these notices.

In PHP I can use:
$pos = strpos($notice, " if ($pos !== false) {
URLs not allowed ;
}

I need to do the same thing in Perl.

Can you help, PLEASE?

Thanks,

John C
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top