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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl script to monitor URLs

Status
Not open for further replies.

lhg1

IS-IT--Management
Mar 29, 2005
134
DK
Hi

I'm trying to buield a perl script to do simple URL monitoring of URLs

I have 2 problems
1. Can't get imput parameters, only when its hardcoded into the script.
ex urlcheck.pl I can't get it to accept $url insteed og the hardcoded

2. The check tekst has to be very excact, I would like it to just check is the word is anywere in the output.

This works
Code:
#! /usr/bin/perl
use HTTP::Request;
require LWP::UserAgent;

$url = @ARGV[0];

{ print "$url\n"; }

$ua = LWP::UserAgent->new;
 $request = HTTP::Request->new(GET => '[URL unfurl="true"]http://www.msn.com');[/URL]
 $response = $ua->request($request);
$string = "${%$response}{_content}\n";

if ($string =~ /MSN\s/)
 {print "0\n";}
else
}
 { #print "ERROR\n"; 
   print "1\n";
 }
# { print "ERROR: $string\n"; }

but this doesn't - even this Msn in in the $string
Code:
#! /usr/bin/perl
use HTTP::Request;
require LWP::UserAgent;

$url = @ARGV[0];

{ print "$url\n"; }

$ua = LWP::UserAgent->new;
 $request = HTTP::Request->new(GET => '[URL unfurl="true"]http://www.msn.com');[/URL]
 $response = $ua->request($request);
$string = "${%$response}{_content}\n";

if ($string =~ /Msn\s/)
 {print "0\n";}
else
}
 { #print "ERROR\n"; 
   print "1\n";
 }
# { print "ERROR: $string\n"; }
 
This is almost word for word the default example for LWP. It's easier to start with code you know already works.

Do you really need to check for that word or can you use the HTTP status code (like 200,404, etc) as that is usually more dependable.

Code:
 # Create a user agent object
  use LWP::UserAgent;
  $ua = LWP::UserAgent->new;
  $ua->agent("MyApp/0.1 ");

  # Create a request
  my $req = HTTP::Request->new(POST => '[URL unfurl="true"]http://www.msn.com');[/URL]
  $req->content_type('application/x-[URL unfurl="true"]www-form-urlencoded');[/URL]
  $req->content('query=lib[URL unfurl="true"]www-perl&mode=dist');[/URL]

  # Pass request to the user agent and get a response back
  my $res = $ua->request($req);

  # Check the outcome of the response
  if ($res->is_success) {
      print $res->content;
	  if ($res->content =~ /MSN/) {
	   print "FOUND MSN!!!\n";
	  }
	  print $res->status_line, "\n";
  }
  else {
      print $res->status_line, "\n";
  }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top