Very Frustrating

S

schnibitz

Hi everyone, the following code works from my linux shell as root or
the 'nobody' accounts:
-----------------------------

#!/usr/bin/perl
#use strict;
#use LWP::Simple;
use LWP::UserAgent;
#use URI::URL;

print "Content-type: text/html\n\n";
print <<HTML;
<html>
<head>
<title>TEST</title>
</head>
<body>
HTML

my $url_variable = $input{'URL'};

my $ua = new LWP::UserAgent;
$ua->timeout(120);
my $request = new HTTP::Request('GET', 'http://www.yahoo.com');
my $response = $ua->request($request);
my $content = $response->content();
print "$content";
print <<HTML;
</body>
</html>
</head>
HTML
---------------------------------

BUT when I hit that same .pl file with my browser I get the following
error:

500 Can't connect to www.yahoo.com:80 (Bad hostname 'www.yahoo.com')

Can someone help me please? I'm doing something idiotic, I know, and
anything you could do to help would be appreciated.

-G
 
S

schnibitz

You lose all of the benfits of that pragma when you comment
it out like that.

Thank you, I've tried both ways BTW, and it doesn't give me any more
helpful messages. I checked DNS on my server. I can ping the
address, and it resolves to an IP just fine. I've checked the perl
resolver by issuing a special perl command, and it too resolves just
fine. I can wget the site without issue, my /etc/hosts file has two
measly entries, so I don't think that a corrupt hosts file has
anything to do with it. Why would it work command-line, but not when
invoked through the browser?
 
S

schnibitz

maybe there's something restricted about the web server?

if it works from the command-line then there's nothing
obviously wrong with the perl logic: maybe there's
something to do with the web server config that breaks
things?

hth
t

Hi there, great suggestion, and I'll be doing that from now on. I
added all of that in, and I'm still not getting any helpful errors. I
don't understand why it works in the shell, but not when invoked from
a browser. I know that the browser hits that file from a different
user (nobody I think) and I've tried invoking it from the "nobody"
acct BTW.

Here is the updated code with your suggested additions, and minor
clarifications:

_______________________________________
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
#use LWP::Simple;
use LWP::UserAgent;
#use URI::URL;

my $content = 0;
my $error = 0;

print "Content-type: text/html\n\n";
print <<HTML;
<html>
<head>
<title>TEST</title>
</head>
<body>
HTML
&getpage;
print "$content";
print "$error";
print <<HTML;
</body>
</html>
</head>
HTML

sub getpage {
my $ua = new LWP::UserAgent;
$ua->timeout(120);
my $request = new HTTP::Request('GET', 'http://www.yahoo.com');
my $response = $ua->request($request);
if ($response->is_success) {
$content = $response->content;
}
else {
$error = $response->status_line, "\n";
}
}

_____________________________________
Any other things I can try?
 
S

schnibitz

maybe there's something restricted about the web server?

if it works from the command-line then there's nothing
obviously wrong with the perl logic: maybe there's
something to do with the web server config that breaks
things?

hth
t

Hadn't thought of that, I'll check into that possibility. Thank you!
 
S

schnibitz

If you "think" wrong, then the fact that it runs as nobody is irrelevant.

What is relevant is if it runs from the command line as the same use
that your CGI program runs as.

So the first step is to determine what user your CGI programs run as.

    #!/usr/bin/perl
    print "Content-Type: text/plain\n\n";
    system 'whoami';


    perldoc -q vars

Hi Tad, fair enough, I removed the quotes. I normally don't use them,
but I started just trying anything (yeah I'm that desperate). Anyway,
I added the "system 'whoami';" command, and it shows "apache" instead
of "nobody". So I su to "apache" and ran the script again.
Flawless. Not a single error or anything thrown that I can see.
Still getting "bad hostname" error when accessed from the browser.
 
S

schnibitz

Hi Tad, fair enough, I removed the quotes.  I normally don't use them,
but I started just trying anything (yeah I'm that desperate).  Anyway,
I added the "system 'whoami';" command, and it shows "apache" instead
of "nobody".  So I su to "apache" and ran the script again.
Flawless.  Not a single error or anything thrown that I can see.
Still getting "bad hostname" error when accessed from the browser.

Okay, one other quick thing . . . So when I add the IP for www.yahoo.com
into the /etc/hosts file, here's what I get when I run it from my
browser now:

0500 Can't connect to www.yahoo.com:80 (Permission denied)

Different message. Anyone make anything of that?
 
S

schnibitz

Hi Tad, fair enough, I removed the quotes.  I normally don't use them,
but I started just trying anything (yeah I'm that desperate).  Anyway,
I added the "system 'whoami';" command, and it shows "apache" instead
of "nobody".  So I su to "apache" and ran the script again.
Flawless.  Not a single error or anything thrown that I can see.
Still getting "bad hostname" error when accessed from the browser.

Okay, last post I swear. That last error got me to realize that
selinux was hurting me. It would not allow my script to open up a
port. Thank you to everyone for bearing with me, and hopefully this
will help some poor soul out there.
 

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,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top