Perl Help

Hi All,

I have the following script and I need to add more functionality to it -

#!/usr/bin/perl
use strict;
use warnings;
my $res;
my @names;
while ( $res = <DATA> ) {
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";
};
}

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

2) print/echo appname ( e.g. SerialNberapps )and again run LWP(wget,curl) against each url from 2.

Can you help in modifying the existing script so it does the above two?

Thanks,
Jacki.

$
$
$ cat -n apps.pl
     1  #!perl
     2  use strict;
     3  use warnings;
     4  my $append = "/cgi-bin/jboss/apps.joa/health?method=checkurl";
     5  my %x;
     6  while (<DATA>) {
     7    /^"(.+apps.*)"\s*=\s*"(.+)"/ and $x{$1} = "\"$2$append\"";
     8  }
     9  while (my ($key, $value) = each %x) {
    10    print "Appname : $key\n";
    11    print "curl $value\n";
    12    # If you want to actually invoke curl, then uncomment the line below
    13    # system("curl $value");
    14  }
    15  __DATA__
    16  {
    17  "_exp" = "81652254";
    18  "apps" = {
    19  "AcctInfoapps" = "http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/OrderManagementapps-MEA.joa/accountInfo";
    20  "StatContentapps" = "http://egq3-wsf-5001.corp.zaphida.com:8531/service/staticcontent";
    21  "ContappsB2B" = "http://spaceqa3-svc.corp.zaphida.com:9203/jboss/apps.joa/content";
    22  "CommQandAStatisticapps" = "http://spaceqa3-svc.corp.zaphida.com:8546/service/qastats";
    23  "SerialNberapps" = "http://spaceqa3-svc.corp.zaphida.com:8341/jboss/apps.joa/serial";
    24  };
    25  }
    26
$
$
$ perl apps.pl
Appname : CommQandAStatisticapps
curl "http://spaceqa3-svc.corp.zaphida.com:8546/service/qastats/cgi-bin/jboss/apps.joa/health?method=checkurl"
Appname : ContappsB2B
curl "http://spaceqa3-svc.corp.zaphida.com:9203/jboss/apps.joa/content/cgi-bin/jboss/apps.joa/health?method=checkurl"
Appname : StatContentapps
curl "http://egq3-wsf-5001.corp.zaphida.com:8531/service/staticcontent/cgi-bin/jboss/apps.joa/health?method=checkurl"
Appname : SerialNberapps
curl "http://spaceqa3-svc.corp.zaphida.com:8341/jboss/apps.joa/serial/cgi-bin/jboss/apps.joa/health?method=checkurl"
Appname : AcctInfoapps
curl "http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/OrderManagementapps-MEA.joa/accountInfo/cgi-bin/jboss/apps.joa/health?method=checkurl"
$
$
$

tyler_durden

Thanks Tyler, it works great!!

One change I want though here is that instead of appending to the whole URL it should append to http://...:port

SO the output I want is -

http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/apps.joa/health?method=checkurl

instead of -

http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/OrderManagementapps-MEA.joa/accountInfo/cgi-bin/jboss/apps.joa/health?method=checkurl

Thanks,
Jack.

$
$
$ cat -n apps.pl
     1  #!perl
     2  use strict;
     3  use warnings;
     4  my $append = "cgi-bin/jboss/apps.joa/health?method=checkurl";
     5  my %x;
     6  while (<DATA>) {
     7    m{^"(.+apps.*)"\s*=\s*"(http://.*?/).*"} and $x{$1} = "\"$2$append\"";
     8  }
     9  while (my ($key, $value) = each %x) {
    10    print "Appname : $key\n";
    11    print "curl $value\n";
    12    # If you want to actually invoke curl, then uncomment the line below
    13    # system("curl $value");
    14  }
    15  __DATA__
    16  {
    17  "_exp" = "81652254";
    18  "apps" = {
    19  "AcctInfoapps" = "http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/OrderManagementapps-MEA.joa/accountInfo";
    20  "StatContentapps" = "http://egq3-wsf-5001.corp.zaphida.com:8531/service/staticcontent";
    21  "ContappsB2B" = "http://spaceqa3-svc.corp.zaphida.com:9203/jboss/apps.joa/content";
    22  "CommQandAStatisticapps" = "http://spaceqa3-svc.corp.zaphida.com:8546/service/qastats";
    23  "SerialNberapps" = "http://spaceqa3-svc.corp.zaphida.com:8341/jboss/apps.joa/serial";
    24  };
    25  }
    26
$
$
$ perl apps.pl
Appname : CommQandAStatisticapps
curl "http://spaceqa3-svc.corp.zaphida.com:8546/cgi-bin/jboss/apps.joa/health?method=checkurl"
Appname : ContappsB2B
curl "http://spaceqa3-svc.corp.zaphida.com:9203/cgi-bin/jboss/apps.joa/health?method=checkurl"
Appname : StatContentapps
curl "http://egq3-wsf-5001.corp.zaphida.com:8531/cgi-bin/jboss/apps.joa/health?method=checkurl"
Appname : SerialNberapps
curl "http://spaceqa3-svc.corp.zaphida.com:8341/cgi-bin/jboss/apps.joa/health?method=checkurl"
Appname : AcctInfoapps
curl "http://spaceqa3-svc.corp.zaphida.com:7322/cgi-bin/jboss/apps.joa/health?method=checkurl"
$
$
$

tyler_durden

1 Like

Thanks Tyler

Your the man!!:b:

Can you explain a little about this line -

m{^"(.+apps.*)"\s*=\s*"(http://.*?/).*"} and $x{$1} = "\"$2$append\"";

specially "\"$2...

perlre - perldoc.perl.org

Each ( ) pair creates a backreference that can later be accessed (until next regex) using $1, $2, $3 ... $n. So, the first ^"(.+apps.*)"\s*= captures everything after the first double-quotes until before the last double-quotes, that which comes before zero or more amount of spaces and literal =. After that again the undetermined amount of spaces and single double-quotes. After that it practically the same thing with the URI as it was with the "key", except that he uses .*? to make * ungreedy in the capture. Without ungreedy matching more than the hostname would have been captured.

Using and is a short-hand so he doesn't have to wrap the m{} in an if-block. In $x{$1} he's using the first backreference as key in the hash, and "\"$2$append\"" means interpolating two values, $2 (2nd backreference) and $append into a single string surrounded by double-quotes.

1 Like

In addition to the explanation posted above, I'd like to add that this -

$x{$1} = "\"$2$append\"";

was done so that the value of each hash key would be of the form

"http://someURL"

instead of

http://someURL

That's because I wasn't sure if the URL in the curl invocation requires double-quotes. If it doesn't, i.e. if the following works -

curl http://someURL

then you could change the code to -

$x{$1} = "$2$append";

tyler_durden

1 Like