CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
change syntax for variables within wget

 



jeffersno1
Novice

Aug 12, 2014, 5:08 PM

Post #1 of 5 (11180 views)
change syntax for variables within wget Can't Post

hi guys,

I need some help with some syntax.

So what i'm looking to do is run a cron task every night and in the script will be a bunch of commands to get files off a server and store them for other monitoring systems.

I need to put epoc time in the filename that im downloading and this is where i'm getting stuck...

in brief this is the line with the issue:

Code
    my $response = $ua->get('http://webserver/graphs/epdf/download.pl?rrd=01_allpass_kt&time1=$epoc&time2=$yest');     ### THIS DOESNT WORK - trouble setting variables here :( 

#my $response = $ua->get('http://webserver/graphs/epdf/download.pl?rrd=01_allpass_kt&time1=1407681301&time2=1407854101'); ##### THIS WORKS


Here is my script so far: please be aware i've copied some of this off the internet


Code
#!/usr/bin/perl 

use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Cookies;
use POSIX;
use File::Basename;

my $epoc = time(); ## set time to now
print "time now $epoc\n"; ## check above works
my $yest = ($epoc - 86400); ## set time to 24hrs earlier
print "yest - $yest\n"; ## check above works

sub main
{
# Create the fake browser (user agent).
my $ua = LWP::UserAgent->new();

# Accept cookies. You don't need to supply
# any options to new() here, but just for
# kicks we'll save the cookies to a file.
my $cookies = HTTP::Cookies->new(
file => "cookies.txt",
autosave => 1,
);

$ua->cookie_jar($cookies);

# Pretend to be Internet Explorer.
$ua->agent("Windows IE 7");
# or maybe .... $ua->agent("Mozilla/8.0");

# Get some HTML.
my $response = $ua->get('http://webserver/graphs/epdf/download.pl?rrd=01_allpass_kt&time1=$epoc&time2=$yest'); ### THIS DOESNT WORK - trouble setting variables here :(

#my $response = $ua->get('http://webserver/graphs/epdf/download.pl?rrd=01_allpass_kt&time1=1407681301&time2=1407854101'); ##### THIS WORKS
unless($response->is_success) {
print "Error: " . $response->status_line;
}

# Let's save the output.
my $save = "/tmp/allpass.xls";

unless(open SAVE, '>' . $save) {
die "nCannot create save file '$save'n";
}

# Without this line, we may get a
# 'wide characters in print' warning.
binmode(SAVE, ":utf8");

print SAVE $response->decoded_content;
close SAVE;

print "Saved " .
length($response->decoded_content) .
" bytes of data to '$save'.";
}
main();


Any help is greatly appreciated.

Many thanks

Jeffers


(This post was edited by jeffersno1 on Aug 18, 2014, 9:47 AM)


FishMonger
Veteran / Moderator

Aug 12, 2014, 5:49 PM

Post #2 of 5 (11178 views)
Re: [jeffersno1] change syntax for variables within wget [In reply to] Can't Post

The single quotes is preventing var interpolation.


Code
my $response = $ua->get("http://webserver/graphs/epdf/download.pl?rrd=01_allpass_kt&time1=$epoc&time2=$yest");



jeffersno1
Novice

Aug 18, 2014, 9:39 AM

Post #3 of 5 (10826 views)
Re: [FishMonger] change syntax for variables within wget [In reply to] Can't Post

Hi FishMonger

Thanks for your reply, that doesn't seem to work

I can get the variables to print out ok but not when within the get command

I tried your suggestion

Code
my $response = $ua->get("http://smcweb/graphs/epdf/download.pl?rrd=01_allpass_kt&time1=$epoc&time2=$yest");


if i run the command manually i get the following output:

$ ./files.pl
time now 1408379527
yest - 1408293127
Saved 0 bytes of data to '/tmp/allpass.xls'.

I even try to save the file and it outputs nothing

but when i try putting in some epoch values i can get the download to work....


Code
my $response = $ua->get('http://someweb/graphs/tool/download.pl?rrd=01_allinfo&time1=1407681301&time2=1407854101');


$ ./files.pl
time now 1408379676
yest - 1408293276
Saved 9932 bytes of data to '/tmp/allinfo.xls'.


any other suggestions ?

thank again


FishMonger
Veteran / Moderator

Aug 18, 2014, 10:02 AM

Post #4 of 5 (10818 views)
Re: [jeffersno1] change syntax for variables within wget [In reply to] Can't Post

It's hard to troubleshoot when you're being inconsistent.

The working and non working examples don't match and are using different values for epoch and yest (time1 and time2).

Take the working url where you hard coded the time values and assign those values to the vars then use those vars in the $ua->get statement.


Laurent_R
Veteran / Moderator

Aug 19, 2014, 9:51 AM

Post #5 of 5 (10708 views)
Re: [jeffersno1] change syntax for variables within wget [In reply to] Can't Post

One thing you could try is to put the param string into an intermediate variable.


Code
my $get_param = "http://smcweb/graphs/epdf/download.pl?rrd=01_allpass_kt&time1=$epoc&time2=$yest"; 
print "$get_param\n"; # check how it is populated
my $response = $ua->get($get_param);

In principle, it should not really change anything, but sometimes this type of trick works, and, besides, at least you know exactly what parameter you passed to the method invocation.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives