Perl code to call variables hostname, portnumber into another variable - perl

#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use XML::Twig;
use HTTP::Request;
my #joblist = ('Testing','Integrity','TEST','Team_test','test','TEST_1','Update_Outlook');
my #score;
foreach my $job_name (#joblist) {
my $url_a = 'http://myhost:8080/job/$job_name/api/xml';
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
my $response = $ua->get($url_a);
if ($response->is_success) {
my $content = $response->decoded_content; # or whatever
XML::Twig->new( twig_roots => { 'healthReport/score' => sub { push #score, $_->text; } }) ->parseurl($url_a);
foreach my $var (#score) {
print "$var \n";
}
}
else {
die $response->status_line;
}
}
In above perl code I am calling $job_name into another variable $url_a.
But I'm getting following error.
404 Not Found at health.pl line 25.
Could someone please help me on this.Thanks.

Try this version:
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use XML::Twig;
use HTTP::Request;
my #joblist = qw|Testing Integrity TEST Team_test test TEST_1 Update_Outlook|;
my #score;
foreach my $job_name (#joblist) {
my $url_a = join("/","http://myhost:8080/job",$job_name,"api/xml");
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
my $response = $ua->get($url_a);
if ($response->is_success) {
my $content = $response->decoded_content; # or whatever
XML::Twig->new( twig_roots => { 'healthReport/score' => sub { push #score, $_->text; } }) ->parseurl($url_a);
foreach my $var (#score) {
print "$var \n";
}
}
else {
printf STDERR "ERROR job: %s, result: %s\n",
$job_name, $response->status_line;
}
}

Related

Pass parameter with import, but it override the export in Perl

I am trying to pass parameter into packages with import but it override my export. I saw some suggested $main:debugLevel in Debugger.pm but it doesn't work. How to fix this?
main.pl
our $debugLevel = 5;
our $dDebug=TRUE;
our $dPkg=__PACKAGE__;
our $dMsg="";
use MyPkg::Debugger qw( :all );
# ^-- how do I pass in the variables declared above?
...
dPrintLog(4, 'testsub', 'msg', $mydata);
# ^-- this generate error, if i put in the "import" sub in the Debugger.pm
Debugger.pm
use strict;
package MyPkg::Debugger;
our $VERSION = 1.00;
our #ISA = qw(Exporter);
our #EXPORT_OK = qw(dPrintLog );
our %EXPORT_TAGS = (
all => \#EXPORT_OK
);
use DateTime::Format::Strptime;
use POSIX qw(strftime);
use Data::Dumper;
our $debugLevel = 5;
our $dDebug=TRUE;
our $dPkg=__PACKAGE__;
our $dMsg="";
sub import {
my ($debugLevel , $dDebug, $dMsg, $data) = #_;
}
sub dPrintLog {
my ($level, $sub, $msg, $data) = #_;
if ($level == 5) {
print "L:" . $level . ";" . "Pkg:" . $sub . ";". "Msg: " . $msg . "\n";
print ' '.Dumper($data) unless (!defined $data);
}elsif ($level == 3){
}elsif ($level == 1){
}else{
}
}
Read Exporter's documentation carefully. It contains all the information you need.
App.pl
#!/usr/bin/perl
use warnings;
use strict;
use MyDbg (':all', 5); # Try removing the 5.
dPrintLog(undef, 'message');
MyDbg.pm
package MyDbg;
use warnings;
use strict;
our #ISA = qw(Exporter);
our #EXPORT_OK = qw(dPrintLog );
our %EXPORT_TAGS = ( all => \#EXPORT_OK );
use Exporter ();
my $debugLevel = 3;
sub import {
my ($class, $tag, $level) = #_;
$debugLevel = $level if $level;
$class->Exporter::export_to_level(1, $class, $tag);
}
sub dPrintLog {
my ($level, $msg) = #_;
$level ||= $debugLevel;
print { 5 => "L:$level $msg",
3 => "$msg",
1 => substr $msg, 0 ,1,
}->{$level}, "\n";
}
__PACKAGE__
Interestingly, it doesn't work if you remove the #ISA line. You have to declare the export subroutine to make it work (based on my experiments, no documentation found):
sub export { Exporter::export(#_) }
or
*export = *Exporter::export{CODE};
or, even
sub export { goto &Exporter::export }

Detect a broken link (web) in perl

I'm trying to detect if a link is broken or not, as in if it's a web address I could paste into my browser and find a web page. I've tried two methods so far that I found online and both are giving me false positives (LWP::UserAgent and LWP::Simple).
#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
my $url1 = 'http://www.gutenberg.org';
my $url2 = 'http://www.gooasdfzzzle.com.no/thisisnotarealsite';
my $ua = LWP::UserAgent->new;
$ua->agent("Mozilla/8.0"); # Pretend to be Mozilla
my $req = HTTP::Request->new(GET => "$url1");
my $res = $ua->request($req);
if ($res->is_success) {
print "Success!\n";
} else {
print "Error: " . $res->status_line . "\n";
}
$req = HTTP::Request->new(GET => "$url2");
$res = $ua->request($req);
if ($res->is_success) {
print "Success!\n";
} else {
print "Error: " . $res->status_line . "\n";
}
Which is giving me output of:
Success!
Success!
and then there's
#!/usr/bin/perl -w
use strict;
use LWP::Simple;
my $url1 = 'http://www.gutenberg.org';
my $url2 = 'http://www.gooasdfzzzle.com.no/thisisnotarealsite';
if (head("$url1")) {
print "Yes\n";
} else {
print "No\n";
}
if (head("$url2")) {
print "Yes\n";
} else {
print "No\n";
}
Which is giving me an output of:
Yes
Yes
Am I missing something here?
Your code worked fine for me, I can only see a problem if your running behind a VPN or gateway as previous stated. Always use strict and warnings, and here is an alternative way so you are not initializing a new Request object everytime you want to check for a valid link.
use strict;
use warnings;
use LWP::UserAgent;
sub check_url {
my ($url) = #_;
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(HEAD => $url);
my $res = $ua->request($req);
return $res->status_line if $res->is_error;
return "Success: $url";
}

How to overwrite a function used in a module-method?

#!/usr/bin/env perl
use warnings;
use 5.012;
use utf8;
use WWW::Mechanize::Cached;
use Some::Module qw(some_method);
my $url = '...';
my $result = some_method( $url );
The some_method() uses itself get() form LWP::Simple.
How could I overwrite the get() with my my_get() in this script?
sub my_get {
my $url;
my $mech = WWW::Mechanize::Cached->new();
$mech->get( $url );
my $content = $mech->content( format => 'text' );
return $content;
}
sub WWW::Mechanize::Cached::get {
# your code
}
OR, if the get method is actually, as you imply in the question, is inherited from LWP::Simple -
sub LWP::Simple::get {
# your code
}

Use proxy with perl script

I want to use a proxy with this perl script but I'm not sure how to make it use a proxy.
#!/usr/bin/perl
use IO::Socket;
$remote = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => "localhost",
PeerPort => "8080",
)
or die "cannot connect";
print $remote "GET / HTTP/1.0\n\n";
while ( <$remote> ) { print }
Use the LWP::UserAgent module, which has built-in proxy support.
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
$ENV{HTTP_proxy} = "http://ip:port";
$ua->env_proxy; # initialize from environment variables
my $req = HTTP::Request->new(GET => 'http://google.com/');
print $ua->request($req)->as_string;
delete $ENV{HTTP_PROXY};
Straight from one of my scripts:
use LWP::UserAgent;
my($ua) = LWP::UserAgent->new;
if ($opts->{'proxy'}) {
my($ip) = Sys::HostIP->hostip;
if (($ip =~ m{^16\.143\.}) ||
($ip =~ m{^161\.}) ||
($ip =~ m{^164\.})) {
$ua->proxy(http => 'http://localhost:8080');
}
else {
$ua->proxy(http => "");
}
}
else {
$ua->env_proxy;
}
#***** get current entry *****
my($req) = HTTP::Request->new(GET => "http://stackoverflow.com/questions/1746614/use-proxy-with-perl-script");
my($raw) = $ua->request($req)->content;

How can I write a simple HTTP proxy in Perl?

I don't want to use the HTTP::Proxy package because I want to dump out a couple requests. My one liner looks like this, but breaks on trying to pass the header in:
perl -MData::Dumper -MHTTP::Daemon -MHTTP::Status -MLWP::UserAgent -e 'my $ua = LWP::UserAgent->new;my $d=new HTTP::Daemon(LocalPort=>1999);print "Please contact me at: <", $d->url, ">\n";while (my $c = $d->accept) {while (my $r = $c->get_request) {if ($r->method eq 'GET' and $r->url->path eq "/uploader") {$c->send_response("whatever.");print Dumper($r);}else{$response=$ua->request($r->method,"http://localhost:1996".$r->uri,$r->headers,$r->content);$c->send_response($response);}}}'
formatted, that's:
#perl -e '
use Data::Dumper;
use HTTP::Daemon;
use HTTP::Status;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $d=new HTTP::Daemon(LocalPort=>1999);
print "Please contact me at: < ", $d->url, " >\n";
while (my $c = $d->accept) {
while (my $r = $c->get_request) {
if ($r->method eq 'GET' and $r->url->path eq "/uploaded") {
$c->send_response("whatever.");
print Dumper($r);
} else {
$response = $ua -> request(
$r->method,
"http://localhost:1996" . $r->uri,
$r->headers,
$r->content);
$c->send_response($response);
}
}
}#'
So I can't just pass in the request, because I need to change the host, and I can't just pass in the headers it seems... so what should I do to keep it short.
So can anyone make this a better one-liner?
Aw shoot, I fixed it with this:
#perl -e '
use Data::Dumper;
use HTTP::Daemon;
use HTTP::Status;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $d=new HTTP::Daemon(LocalPort=>1999);
print "Please contact me at: < ", $d->url, " >\n";
while (my $c = $d->accept) {
while (my $r = $c->get_request) {
if ($r->method eq "GET" and $r->url->path eq "/uploaded") {
$c->send_response("whatever.");
print Dumper($r);
} else {
$response = $ua -> request( HTTP::Request->new(
$r->method,
"http://localhost:1996" . $r->uri,
$r->headers,
$r->content));
$c->send_response($response);
}
}
}#'
note the HTTP::Request->new yeah... so it works, it's a tad slow. but that's okay