1829103 Members
2959 Online
109986 Solutions
New Discussion

Perl Help

 
SOLVED
Go to solution
Allanm
Super Advisor

Perl Help

Hi Folks,

I created a Perl script to do a wget on an URL.

Script's output is like the following -

{
"_exp" = "81652254";
"apps" = {
"AcctInfoapps" = "http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/OrderManagementapps-MEA.joa/accountInfo";
"StatContentapps" = "http://egq3-wsf-5001.corp.zaphida.com:8531/service/staticcontent";
"ContappsB2B" = "http://spaceqa3-svc.corp.zaphida.com:9203/jboss/apps.joa/content";
"CommQandAStatisticapps" = "http://spaceqa3-svc.corp.zaphida.com:8546/service/qastats";
"SerialNberapps" = "http://spaceqa3-svc.corp.zaphida.com:8341/jboss/apps.joa/serial";
};
}

What I need is the following -

1)is to store the output to an array and parse out http://...:port (e.g. -
http://spaceqa3-svc.corp.zaphida.com:8341/)
and the appname (e.g. SerialNberapps )
and store in scalar.

2)Then append the uri cgi-bin/jboss/apps.joa/health?method=checkurl to each of the urls (from 1)

3) echo appname and again run wget against each url from 2.

Here is the script that I created -

#!/usr/bin/perl -w

use LWP::UserAgent;
$ua = LWP::UserAgent->new;
$ua->agent("Mozilla/8.0");

$req = HTTP::Request->new(GET => 'http://apps.zaphida.com:8501/jboss/Service.woa/mea?method=getServices');
$req->header('Accept' => 'text');

$res = $ua->request($req);

if ($res->is_success) {
print $res->decoded_content;
}
else {
print "Error: ". $res->status_line."\n";
}
10 REPLIES 10
James R. Ferguson
Acclaimed Contributor

Re: Perl Help

Hi Allan:

See if this snippet guides you:

# cat ./fetch
#!/usr/bin/perl
use strict;
use warnings;
{
$/ = undef;
my $str = ;
$str =~ m{SerialNberapps".+"(.+)"} and print $1, "\n";
}
__DATA__
{
"_exp" = "81652254";
"apps" = {
"AcctInfoapps" = "http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/OrderManagementapps-MEA.joa/accountInfo";
"StatContentapps" = "http://egq3-wsf-5001.corp.zaphida.com:8531/service/staticcontent";
"ContappsB2B" = "http://spaceqa3-svc.corp.zaphida.com:9203/jboss/apps.joa/content";
"CommQandAStatisticapps" = "http://spaceqa3-svc.corp.zaphida.com:8546/service/qastats";
"SerialNberapps" = "http://spaceqa3-svc.corp.zaphida.com:8341/jboss/apps.joa/serial";
};
}

# ./fetch
http://spaceqa3-svc.corp.zaphida.com:8341/jboss/apps.joa/serial

Regards!

...JRF...
Allanm
Super Advisor

Re: Perl Help

Hi JRF,

The string matching is a problem cause I can have 50+ appnames in the the output and I need a non hard code way of parsing out the http://...:port and appname.

Also once I have the http://...:port URL I want to append a certain uri to the URLs (http://....port ) and echo appname and run a wget against each one of the appended URL so as to check their health.

The output would be something like this

Appname1
Status - OK
Appname2
Status - WARN

Thanks,
Allanm
James R. Ferguson
Acclaimed Contributor

Re: Perl Help

Hi (again) Allan:

> The string matching is a problem cause I can have 50+ appnames in the the output and I need a non hard code way of parsing out the http://...:port and appname.

Then consider this prototype:

# cat ./fetch
#!/usr/bin/perl
use strict;
use warnings;
{
$/ = undef;
my $str = ;
my @names = ( $str =~ m{SerialNberapps".+"(.+)"}g );
print "$_\n" for @names;
}
__DATA__
{
"_exp" = "81652254";
"apps" = {
"AcctInfoapps" = "http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/OrderManagementapps-MEA.joa/accountInfo";
"StatContentapps" = "http://egq3-wsf-5001.corp.zaphida.com:8531/service/staticcontent";
"ContappsB2B" = "http://spaceqa3-svc.corp.zaphida.com:9203/jboss/apps.joa/content";
"CommQandAStatisticapps" = "http://spaceqa3-svc.corp.zaphida.com:8546/service/qastats";
"SerialNberapps" = "http://spaceqa3-svc.corp.zaphida.com:8341/jboss/apps.joa/serial";
};
}
{
"_exp" = "81652254";
"apps" = {
"AcctInfoapps" = "http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/OrderManagementapps-MEA.joa/accountInfo";
"StatContentapps" = "http://egq3-wsf-5001.corp.zaphida.com:8531/service/staticcontent";
"ContappsB2B" = "http://spaceqa3-svc.corp.zaphida.com:9203/jboss/apps.joa/content";
"CommQandAStatisticapps" = "http://spaceqa3-svc.corp.zaphida.com:8546/service/qastats";
"SerialNberapps" = "http://spaceqa4-svc.corp.zaphida.com:8341/jboss/apps.joa/serial";
};
}
{
"_exp" = "81652254";
"apps" = {
"AcctInfoapps" = "http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/OrderManagementapps-MEA.joa/accountInfo";
"StatContentapps" = "http://egq3-wsf-5001.corp.zaphida.com:8531/service/staticcontent";
"ContappsB2B" = "http://spaceqa3-svc.corp.zaphida.com:9203/jboss/apps.joa/content";
"CommQandAStatisticapps" = "http://spaceqa3-svc.corp.zaphida.com:8546/service/qastats";
"SerialNberapps" = "http://spaceqa5-svc.corp.zaphida.com:8341/jboss/apps.joa/serial";
};
}

...thus:

# ./fetch
http://spaceqa3-svc.corp.zaphida.com:8341/jboss/apps.joa/serial
http://spaceqa4-svc.corp.zaphida.com:8341/jboss/apps.joa/serial
http://spaceqa5-svc.corp.zaphida.com:8341/jboss/apps.joa/serial

...which collects all matches into an array whose elements are then printed for demonstration.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Perl Help

Hi (again) Allan:

Maybe I'm being too obtuse. Your wrote:

> ... store the output to an array and parse out http://...:port (e.g. -
http://spaceqa3-svc.corp.zaphida.com:8341/)
and the appname (e.g. SerialNberapps )
and store in scalar.

Given that $res->decoded_content has the output you could do:

# push @names, $1 if $res->decoded_context =~ m{SerialNberapps".+"(.+)"};

...to collect the "appname" URLs into an array.

Regards!

...JRF...
Allanm
Super Advisor

Re: Perl Help

The string matching just gets me SerialNberapps but not the other apps like
AcctInfoapps, StatContentapps ....

Is there a way to get those? More like a generic way to loop through them in an array/hash?

Sorry if I may not have been clear earlier...

Thanks,
Allan
James R. Ferguson
Acclaimed Contributor
Solution

Re: Perl Help

Hi (again) Allan:

> The string matching just gets me SerialNberapps but not the other apps like
AcctInfoapps, StatContentapps ....

Yes:

# cat ./fetch
#!/usr/bin/perl
use strict;
use warnings;
my $res;
my @names;
while ( $res = ) {
push @names, ( $res =~ m{^".+apps.*"\s*=\s*"(.+)"} );
}
print "$_\n" for @names;
__DATA__
{
"_exp" = "81652254";
"apps" = {
"AcctInfoapps" = "http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/OrderManagementapps-MEA.joa/accountInfo";
"StatContentapps" = "http://egq3-wsf-5001.corp.zaphida.com:8531/service/staticcontent";
"ContappsB2B" = "http://spaceqa3-svc.corp.zaphida.com:9203/jboss/apps.joa/content";
"CommQandAStatisticapps" = "http://spaceqa3-svc.corp.zaphida.com:8546/service/qastats";
"SerialNberapps" = "http://spaceqa3-svc.corp.zaphida.com:8341/jboss/apps.joa/serial";
};
}

# ./fetch
http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/OrderManagementapps-MEA.joa/accountInfo
http://egq3-wsf-5001.corp.zaphida.com:8531/service/staticcontent
http://spaceqa3-svc.corp.zaphida.com:9203/jboss/apps.joa/content
http://spaceqa3-svc.corp.zaphida.com:8546/service/qastats
http://spaceqa3-svc.corp.zaphida.com:8341/jboss/apps.joa/serial

Regards!

...JRF...
Allanm
Super Advisor

Re: Perl Help

Thanks JRF!!

that was simply awesome...

Two things I need to discuss further on this -

1) push @names, ( $res =~ m{^".+apps.*"\s*=\s*"(.+)"} );

The above string, shouldn't it produce

"AcctInfoapps" = "http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/OrderManagementapps-MEA.joa/accountInfo"

but produces just the url... want to understand how?

One more thing I want to do is to append a URI to the URL that I got from above -

2)Then append the uri cgi-bin/jboss/apps.joa/health?method=checkurl to each of the urls (from 1) and run wget against each URL.

Can you address these two things as well?

Thanks so much!
Allan


Allanm
Super Advisor

Re: Perl Help

Hi JRF,

Can you please answer my above questions.

Thanks,
Allan
Allanm
Super Advisor

Re: Perl Help

Hi JRF,

I was expecting result in the following format -

./fetch.pl

http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/apps.joa/health?method=checkurl
http://egq3-wsf-5001.corp.zaphida.com:8531/cgi-bin/jboss/apps.joa/health?method=checkurl
http://spaceqa3-svc.corp.zaphida.com:9203/cgi-bin/jboss/apps.joa/health?method=checkurl
http://spaceqa3-svc.corp.zaphida.com:8546/cgi-bin/jboss/apps.joahealth?method=checkurl
http://spaceqa3-svc.corp.zaphida.com:8341/cgi-bin/jboss/apps.joa/health?method=checkurl

I need the output to be stored in a data structure so I can run LWP/wget against each URL and get the health status output.

Sorry I am still learning perl and need to get a hang of it.

Your help is most appreciated.

Thanks,
Allan

James R. Ferguson
Acclaimed Contributor

Re: Perl Help

Hi Allan:

# cat ./fetch
#!/usr/bin/perl
use strict;
use warnings;
my $uri = q(cgi-bin/jboss/apps.joa/health?method=checkurl);
my $res;
my @names;
while ( $res = ) {
push @names, $res =~ m{^".+apps.*"\s*=\s*"(.+:\d+/)};
}
for (@names) {
my $wget = $_ . $uri;
print "$wget\n";
}
__DATA__
{
"_exp" = "81652254";
"apps" = {
"AcctInfoapps" = "http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/OrderManagementapps-MEA.joa/accountInfo";
"StatContentapps" = "http://egq3-wsf-5001.corp.zaphida.com:8531/service/staticcontent";
"ContappsB2B" = "http://spaceqa3-svc.corp.zaphida.com:9203/jboss/apps.joa/content";
"CommQandAStatisticapps" = "http://spaceqa3-svc.corp.zaphida.com:8546/service/qastats";
"SerialNberapps" = "http://spaceqa3-svc.corp.zaphida.com:8341/jboss/apps.joa/serial";
};
}

# ./fetch
http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/apps.joa/health?method=checkurl
http://egq3-wsf-5001.corp.zaphida.com:8531/cgi-bin/jboss/apps.joa/health?method=checkurl
http://spaceqa3-svc.corp.zaphida.com:9203/cgi-bin/jboss/apps.joa/health?method=checkurl
http://spaceqa3-svc.corp.zaphida.com:8546/cgi-bin/jboss/apps.joa/health?method=checkurl
http://spaceqa3-svc.corp.zaphida.com:8341/cgi-bin/jboss/apps.joa/health?method=checkurl

...the regular expression matches the URL that begins a line (^) with a double quote; followed by one or more characters (.+); the sequence "apps"; zero or more characters (.*); a double quote; optional whitespace (\s*); an equal sign; some more optional whitespace; and a double quote. The next sequence in parentheses is captured in a variable named $1. We look for one or more characters; a colon; one or more digits; and a forward slash. This is the URL we will stuff into our array.

m{^".+apps.*"\s*=\s*"(.+:\d+/)};

Having loaded an array with the URLs we want, we can loop through that array and concatenate (that's the dot operator) the URI you want to form a new variable to pass to a wget().

If you like, you could extract the URL and concatenate before stuffing your array variable, or simply extract and concatenate into a variable that you immediately use with wget().

Regards!

...JRF...