#!/usr/bin/perl ########################################################################## # get a URL content (html or otherwise) from a host server # optionally snip a piece of the returned content # Receives the url requested # the case sensitive string to search for (optional) # the offset from the beginning of the string (optional) # the length of the snipet (optional) # Returns the data requested, or an error message ########################################################################## $requestURL = $ARGV[0]; $searchFor = $ARGV[1]; $offset = $ARGV[2]; $length = $ARGV[3]; if ($requestURL eq '') { print ("Usage: $0 requestedURL [searchString offset length] \n"); exit(-1); } if ($searchFor && ($offset == '' || $length == '')) { print ("Usage: $0 requestedURL [searchString offset length] \n"); exit(-2); } $data = &getURL($requestURL); if ($searchFor) { $data = &snip(\$data, $searchFor, $offset, $length); } print ($data, "\n"); ############################################################################ # Get an HTML page or other URL from the web # Receives the url requested # Returns the data received or an error message ############################################################################ sub getURL { my $url = $_[0]; # get requested URL from $_ $url = "http://" . $url; # prepend "http://" use LWP::UserAgent; # use LWP UserAgent method $browser = new LWP::UserAgent; # create a new user agent # $browser->agent("Name: " . $browser->agent); # Create a request my $req = new HTTP::Request(GET => $url); # $req->content_type('application/x-www-form-urlencoded'); # $req->content('match=www&errors=0'); # Send request and get a response back my $resp = $browser->request($req); ($resp->is_success) ? return($resp->content) : return('Bad request, or URL not found'); } ######################################################################## # Snip a piece of data from large data # Receives a 'reference' to a large data string # a string to search for # an offset from the beginning of the search string # the length of the returned snipped # Returns the data snippet ######################################################################## sub snip { my $dataref = $_[0]; # get the reference to large data my $find = $_[1]; # get the search string my $from = $_[2]; # get the offset + or - my $lgth = $_[3]; # get the length of the snipet $foundAt = index($$dataref, $find); # search for $find in $data if ($foundAt == -1) { return("String not found"); } my $snipet = substr($$dataref, $foundAt+$from, $lgth); # snip it return $snipet; }