Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script only works through telnet.. 2

Status
Not open for further replies.

MatthewP

Programmer
Jan 16, 2001
176
GB
I'm having this really odd problem.. I've written a script that works through telnet, but not through a browser. If I run the -c switch through telnet it comes up with 'syntax OK' - and if I run -w I don't get any problems.

But, If I call it through my browser, I get a 'page cannot be displayed' message. Not even an error 500. I've come back to this after nearly 3 months with a clear head, and still can't figure out why..

Basically, the script scans a directory of image files against a flat file database.. if they're in the directory and not in the database it means they're redundant, and should be automatically transferred to a directory called 'remove'. That simple.

It's listed below - I've commented where it stops working in browsers.

Thanks for any help!

Matt.

#!/usr/local/bin/perl

#set content type
print "Content-type:text/html\n\n";

#HTML header
print &quot;<html><head><title>w</title></head><body>Images to remove\n&quot;;

#check which products file to use
open (WHO,&quot;data/whichfile.txt&quot;);
$whichfile = <WHO>;
close (WHO);

print &quot;$whichfile is current file<br><br>\n&quot;;

#open current products file - store in @products
open (INFILE,&quot;data/$whichfile&quot;) or dienice(&quot;Can't open products file: $!&quot;);
@products = <INFILE>;
close (INFILE);
#open images directory - store in @allimages
opendir (IMG, &quot;../public_html/images&quot;) or dienice(&quot;Can't open image directory: $!&quot;);
@allimages = readdir(IMG);
closedir (IMG);
##########################################################
#all ok up to here in browsers - but the rest won't run..#
##########################################################

#for each image file in the directory
foreach $imgfile (@allimages) {
$foundfile=0;
foreach $line (@products) {
chomp($line);
($letsection,$name,$title,$format,$price,$descript,$stockno,$section,$img,$weight,$date,$label,$catno) = split(/\|/,$line);
if ($imgfile eq $img) {
$foundfile = 1;
}
}
if ($foundfile == 0) {
$iftrue=&quot;0&quot;;
$iftrue = substr($imgfile, 0 , 2);
if ($iftrue eq &quot;fe&quot;) {

print &quot;$imgfile<br>\n&quot;;
rename (&quot;../public_html/images/$imgfile&quot;, &quot;../public_html/remove/$imgfile&quot;);
}
}
}

print &quot;</body></html>\n&quot;;
 
Sorry, I don't have the answer to your question, because I was just about to post exactly the same question myself!!!

My script runs perfectly from the command line in telnet, but will not run when invoked from the &quot;location&quot; bar in a browser. In this case I do get a 500 &quot;internal server error&quot;.

All the script does is extract an email address from a text database and sends the same email to all the addresses using sendmail. I print a short &quot;Done&quot; message to the browser with the proper headers etc. when it's through.

Like I say, the entire script works perfectly. All emails come through. I've never run into this before.

Any possiblilities?

Roy
 
MathewP,
I suspect that your are asking the web server to do something for which it has insufficient permissions. The first line of your code looks like UNIX. I may be preaching to the choir. If so, sorry. If not,.....

When you run a piece of code from a command prompt, you are running it as yourself, with all the environmental variables for your environment in force. Stuff like paths and execute/read/write permissions are all relative to your login. When you run a piece of code via a browser, it is being run by another, hopefully very weak user, the web daemon. Your system administrator should have the web daemon setup with very limited abilities for security reasons. So,

Make sure that the web daemon has sufficient permissions to execute the file you are trying to run.

Make sure the web daemon can read and/ or write any files that are being used, as appropriate.

Make sure your script is in the 'cgi-bin' directory for the web server ( or a sub dir there in).

If you don't know how to check these things, we can add a little more detail.

You might do a little of what I call 'idiot checking'. That is not an insult. I frequently have to check my own idiocy X-). You can do an idiot check by making sure you can run a most simple CGI app and then expand it's function step by step to do what you want.

A ridiculously simple cgi app.

Code:
#!/usr/local/bin/perl
print &quot;Content-type: text/html\n\n&quot;;
print &quot;<HTML><HEAD><TITLE>A NEW CGI PAGE</TITLE></HEAD>\n&quot;;
print &quot;<BODY><P>Here is the text I want to see</P></BODY></HTML>&quot;;


'hope this helps.


keep the rudder amid ship and beware the odd typo
 
I think you have found MatthewP's problem. I would guess the offending line is this one:

rename (&quot;../public_html/images/$imgfile&quot;, &quot;../public_html/remove/$imgfile&quot;);

unless this directory is chmod 777 (unlikely) the browser is not going to be able to do stuff in these directories.

Unfortunately for me, I can't find the same kind of problem in my own script which follows. It's just too simple a script! the script is also permissioned 755.

#!usr/bin/perl
#### A program to pull email addresses and send a single email

$mailprog = '/usr/sbin/sendmail';
my $fbfile= $path.&quot;feedback.txt&quot;; #permissioned 755, a text database
$mailfrom = &quot;me\@mydomain.com&quot;;

$message= qq ^
The email message
^;

###### open the file and get email addresses ######################
open (FBFILE,&quot;$fbfile&quot;) || die &quot;Content-type: text/html\n\nCan't Open $fbfile(r): $!\n&quot;;
my(@LINES)=<FBFILE>;
close(FBFILE);
$SIZE=@LINES;

$prevname=&quot;&quot;;
$emailcounter=0;
$counter=0;

while ($counter < $SIZE) {
chomp($LINES[$counter]);
( $bidder) = split(/\|/,$LINES[$counter]); #other fields in the database left out
if ($bidder ne $prevname) { #skips duplicate entries
&send_email;
$emailcounter=$emailcounter+1;
}
$prevname = $bidder;
$counter =$counter+1;
};

print &quot;Content-type: text/html\n\n&quot;;

print qq ^
<HTML><Body>
Done. $emailcounter separate emails sent.
</Body></HTML>
^;

sub send_email {
#######################################################

# Open The Mail Program
open(MAIL,&quot;|$mailprog -t&quot;);

print MAIL &quot;To: $bidder\n&quot;;
print MAIL &quot;From: $mailfrom\n&quot;;
print MAIL &quot;Subject: We left you feedback!&quot;;
print MAIL &quot;\n\n&quot;;
print MAIL &quot;$message&quot;;
close (MAIL);
}

1;

###############

All my other cgi programs in this directory work. They all have the same first line and permissions, and the same sendmail location. I have triple checked the permissioning. there are no syntax or logical errors because it works perfectly from the command line.
 
Well.. the other 20 scripts in the cgi-bin work fine, so I guess it is as rlingen said - to do with the security permissions on those directories. As it happens global permissions aren't required so I can happily leave it as user only access. And at least I know why now so I'm happy. Thanks to you both!

Matt.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top