lwp POST and proxies

S

sil

Hey all hoping someone can point me in the right direction on this.
I've set up a proxy server to log into another server on my LAN, this
other server is running ZenOSS and is configured to create reports and
email them upon login.... I figured I could leverage it for a coworker
or manager to auto generate a report based on a list so I created the
following: I have a file with names that reads like this:

# more names
name1
name2
name3


Need to take those names and do a POST using a proxy server... This is
what I have...

Doesn't work does anyone have a solution to read from a file and then
do a POST?

#!/usr/bin/perl -w
use strict;
use warnings;

use LWP;
my $data_file="names";
my $name;
my $browser = LWP::UserAgent->new;
$browser->agent('ZenOSS Report Generator');
$browser->proxy('10.10.15.20:3128');


open(FILE, $data_file) || die("Could not open file!");

while(<FILE>){

$name = $_;
print "Working on $name\n";
my $req = HTTP::Request->new(POST => "http://10.10.20.100/login.jsp?
UserID=$name&Password=password&x=27&y=28");
$req->content_type('application/x-www-form-urlencoded');

my $response = $browser->request($req);
print "Done: ", $response->status_line;
}

Thanks in advance
 
B

Ben Morrow

Quoth sil said:
Hey all hoping someone can point me in the right direction on this.
I've set up a proxy server to log into another server on my LAN, this
other server is running ZenOSS and is configured to create reports and
email them upon login.... I figured I could leverage it for a coworker
or manager to auto generate a report based on a list so I created the
following: I have a file with names that reads like this:

# more names
name1
name2
name3


Need to take those names and do a POST using a proxy server... This is
what I have...

Doesn't work does anyone have a solution to read from a file and then
do a POST?

What do you mean by 'doesn't work'?
#!/usr/bin/perl -w

-w is redundant with 'use warnings'.
use strict;
use warnings;

use LWP;
my $data_file="names";
my $name;
my $browser = LWP::UserAgent->new;
$browser->agent('ZenOSS Report Generator');
$browser->proxy('10.10.15.20:3128');


open(FILE, $data_file) || die("Could not open file!");

Use three-arg open.
Use lexical filehandles.
Say what you were trying to open, and why it failed.

open(my $FILE, '<', $data_file)
|| die("Could not open '$data_file': $!");

Personally I would prefer to use 'or' and omit all those parens, but
that's a matter of taste.
while(<FILE>){

$name = $_;

This is silly. You're just clobbering your global $_ for no reason.

while (my $name = <$FILE>) {

Ben
 
G

Guasco

Hi,
there is no post fields, and the link dont appear a post method.
try use get method

# my $req = HTTP::Request->new(POST => "http://10.10.20.100/login.jsp?
# UserID=$name&Password=password&x=27&y=28");
# $req->content_type('application/x-www-form-urlencoded');
#
# my $response = $browser->request($req);
my $response = $browser->get($req);


if it is a real post method then

my $req = HTTP::Request->new(POST => "http://10.10.20.100/
login.jsp",
["UserID",$name,"Password","password","x","27","y","28"]);
#array with field name, values
 
G

Guasco

Hi,
there is no post fields, and the link dont appear a post method.
try use get method

# my $req = HTTP::Request->new(POST => "http://10.10.20.100/login.jsp?
# UserID=$name&Password=password&x=27&y=28");
# $req->content_type('application/x-www-form-urlencoded');
#
# my $response = $browser->request($req);
my $response = $browser->get("http://10.10.20.100/login.jsp? UserID=
$name&Password=password&x=27&y=28");


if it is a real post method then

my $req = HTTP::Request->new(POST => "http://10.10.20.100/
login.jsp",
["UserID",$name,"Password","password","x","27","y","28"]);
#array with field name, values
 
J

John Bokma

Ben Morrow said:
Say what you were trying to open, and why it failed.

open(my $FILE, '<', $data_file)
|| die("Could not open '$data_file': $!");

I prefer to add reading (in this case), i.e,

die "Could not open '$data_file' for reading: $!";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top