Search code examples
perllwp

How to find string in HTML response


I have written a script to login to a web page and print the response. Now I want to find a string in the HTML response, but I don't know how.

My current code:

use strict;
use warnings;
use LWP::UserAgent;


my $clientIP = "129.168.1.50:80";
my $clientURL = "http://" . $clientIP . "/conf";

## User Agent (UA)
my $ua=LWP::UserAgent->new;
$ua->timeout(10);

$ua->credentials($clientIP, 'Secure Area', 'user', 'pa$$word');

my $page = $ua->get($clientURL);
my $body = $page->content();
print $body;

Current print output from $body:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>configuration</title>
</head>

<body>
        <h1>Client</h1>
        <p>
                Version
                2.16.4.9</p>
        <a href="settings">Settings</a>
        <br>
        <a href="updateskin">Update skin</a>
        <br>
        <a href="updatesettings">Update settings</a>
        <br>
        <p>Software Solutions</p>
</body>
</html>

How can I find the version string and number in the response and write it to a variable?

The goal of the script is to get the version number and write it to a file.


Solution

  • Thanks for your help. The regular expression is the simplest solution.

    # Remove all wordwraps
    while($body=~s/[\n\r\l]//){} 
    my $ver='unknown'; # Default version
    if($body=~/version\s*([0-9\.]+)/i){$ver=$1;}
    print "\n" . $ver . "\n";