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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl script runs but doesn't create a file

Status
Not open for further replies.

NewtownGuy

Technical User
Joined
Jul 27, 2007
Messages
146
Location
US
Hello,

I thought I had this problem licked, but apparently not.

I'm launching a perl script from a browser but the script does not create any file output. The script passes the perl syntax check ok. I'm not getting any errors from the IE6 browser or the Apache 2 HTTP server (Listen port = 8000). I know the script is trying to run because if I try to launch a non-existant script, I get error messagea from both the browser and the HTTP server. There are no error messages with my script.

I put in a write to a test file to try to see what's happening, but nothing gets written to the file. But, at least a test file is created IF I launch the script from the command line. No test file is created when I invoke the script from the browser. I run at root. I set the script's permissions to 777.

My browser creates an invocation of the form:

Code:
[URL unfurl="true"]http://192.168.192.103:8000/cgi-bin/serialscript.pl?value=123456[/URL]

Here's my script:

Code:
#!/usr/bin/perl
# output a sequence of 
# (a) sync -- an initial 8-bit whose value is always 0xFF,
# (b) n 8-bit values, each of which is sent to this script as two hex
# characters by caller, and 
# (c) one final 8-bit value, the checksum, which is the modulo 256 sum
# of the n 8-bit values sent by caller, to a COM port

use CGI qw/:standard/;

use strict;

my $cgi = new CGI;

my $value = $cgi->param('value');

# Output of CGI Script
# - dummy no-value returned to web page to avoid error messages
print header(-status=>'204 No Content'); 
close (STDOUT); # Ensure nothing else gets sent.

open(TEST, ">/var/[URL unfurl="true"]www/cgi-bin/test.txt");[/URL]
print TEST $value;
close(TEST);

# Open output file and redirect to COM port
open(OUT, ">/dev/ttyS0") or  "Can't open: $!"; 
# open(OUT, ">/var/[URL unfurl="true"]www/cgi-bin/log.txt")[/URL] or "Can't open: $!";
binmode(OUT);
flock (OUT, 2); # Lock the file to protect it from writing by others

# Output one fixed 8-bit value whose value is 0xFF
print OUT chr(hex('FF')); 

# each 8-bit value to be output is sent by caller as two hex characters,
# so the number of bytes to be output (not counting the leading sync and
# trailing checksum bytes) is half the number of characters sent by
# caller

my $bytes = length($value) / 2; #each 8-bit value to be output is sent by caller as two hex characters, so the number of bytes to be output (not counting the leading sync and trailing checksum bytes) is half the number of characters sent by caller

# Initialize checksum, which will be computed as the sum of
# the $bytes (number of) 8-bit values output
my $checksum = 0; 

for (my $i = 0; ($i < $bytes); $i++) { 
	my $hex1 = substr($value, (2 x $i), 1); #get m'th character
	my $hex2 = substr($value, (2 x $i) + 1, 1); #get (m+1)'th character
	my $hex1_hex2 = hex("$hex1$hex2"); #combine two hex characters into one byte from left to right
	print OUT chr($hex1_hex2); #output one 8-bit value from two hex characters
	my $checksum = ($checksum + $hex1_hex2) % 256; #add 8-bit value from new character to $checksum. Only compute $checksum to 8 bits since the calculation is modulo 256
}

# Output checksum as the final byte
print OUT chr($checksum);

# Unlock the file
flock(OUT, 8); 
close(OUT);

Can someone tell me why I'm not creating any output when I launch it from the browser ?

Can someone tell me how to pass parameters to the script when I launch it from the command line for testing ?

Thank you.

-- NewtownGuy
 
open(TEST, ">/var/
to
open(TEST, ">/var/ or die "Can't open test.txt : $!\n";

probably a permissions issue.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
is this a complete path?

open(TEST, ">/var/
Can someone tell me how to pass parameters to the script when I launch it from the command line for testing ?

The CGI module documents how to pass command line arguments to your CGI scripts. Lok for "-debug" in the "pragmas" section of the documentation.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
REPLY to travs69 (MIS)

Thank you for the suggestion to add an "or die..." clause. However, I did this and the effect is still the same -- no files are written when the script is invoked from a browser running on a different machine.

Yes, it sounds like a permissions problem to me -- but which permissions do I need to change to be able to write the files ?

The perl script has permissions of 777. I access the script anonymously (I suppose) when it is referenced by IE running on a different machine and accessing the Apache HTTPD server on the machine where I'm trying to write a test file, and, more importantly, where I'm trying to write a file to /dev/ttyS0.

-- NewtownGuy


 
After you run it look in the apache error log and it will tell you if you are having permission problems. You will need write permissions to the file from the user the web server runs as (normally
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
PERMISSION DENIED

I checked /var/log/httpd/error_log, and it gives me this error message:

[Sun Aug 12 17:20:00 2007] [error] [client 192.168.192.128] Can't open test.txt : Permission denied, referer:
So, I have a permissions problem for sure. How do I fix it ? Do I need to do anything so that I can also write to ttyS0 ?

-- NewtownGuy
 
TO: travs69 (MIS)

Our comments about permissions crossed in Cyberspace.

The line from error_log doesn't seem to identify the user that is a web browser. How do I verify the user's name ? Would all users -- even if there are several at the same time -- have the same name ?

Once I know the user's default name, do I need to issue a chmod in a Linux startup script so it's always active ? If so, which startup script and what text do I need to add ?

Thank you.

-- NewtownGuy
 
You can do a ps -ef | grep httpd (on a unix/linux box) and see the user that is running the httpd daemon (if you don't already know). That is the user that will be doing all the writing/reading for apache no matter who is logged in via the web page. You should also test all your scripts as this user (if you can) as that will tell you in advance if you are going to have problems once you try and use it.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
TO: travs69 (MIS)

Thank you.

Here's the result of the ps command:

Code:
[root@001 conf]# ps -ef | grep httpd
root      2283     1  0 Aug11 ?        00:00:00 /usr/sbin/httpd
apache    2338  2283  0 Aug11 ?        00:00:00 /usr/sbin/httpd
apache    2339  2283  0 Aug11 ?        00:00:00 /usr/sbin/httpd
apache    2340  2283  0 Aug11 ?        00:00:00 /usr/sbin/httpd
apache    2341  2283  0 Aug11 ?        00:00:00 /usr/sbin/httpd
apache    2342  2283  0 Aug11 ?        00:00:00 /usr/sbin/httpd
apache    2343  2283  0 Aug11 ?        00:00:00 /usr/sbin/httpd
apache    2344  2283  0 Aug11 ?        00:00:00 /usr/sbin/httpd
apache    2345  2283  0 Aug11 ?        00:00:00 /usr/sbin/httpd
[root@001 conf]#

I tried logging in as apache, but I have no idea what the password is since I didn't set up the apache login since it was setup by Linux by default. All I know is that it's a part of the apache group, according to /etc/httpd/conf/httpd.conf. I tried no password, but that didn't work. How do I find out what the password is ?

-- NewtownGuy
 
do you have root? If not your out of luck.. If so just log in as root and do a su - apache or just create the file you need to and give it permissions of 775 or chown it to the apache user.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top