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: Beginner:
writing to .pl file

 



dysolve
Novice

Jul 17, 2009, 7:42 AM

Post #1 of 8 (833 views)
writing to .pl file Can't Post

Hello all,

I found a perl script on the net that lets you upload gps info to a webserver using perl and then another user can download it as if they are connected to my gps remotely. I know the program worked in the past as my friend did have it working but my issue is after installing it on the server the script build gives you a list of tests to do to make sure it works, but when I run the first test it fails .. the program is called NETGPS below is some of the scripting ,the test i run and the error. from my testing it seems to be an issue with permission but I have all files and folder set to 755 (777 gives a 401 error)

the test i run is

Test Step 1

Test : Invalid Access

Input : http://servername/netgps.pl

Expected Result : GPSERROR:Invalid Parameters

but the result I get is : an internal server error or GPSERROR: username/password not supplied




dysolve
Novice

Jul 17, 2009, 7:44 AM

Post #2 of 8 (832 views)
Re: [dysolve] writing to .pl file [In reply to] Can't Post

the script is:

#!/usr/bin/perl

# netgps.pl
# Robert Pepper <robert@peppernet.org>
# Version 1.1
# Use at your own risk. Email problems to root@127.0.0.1

# ----- User defined
my $filedir = '/cgi/'; # NOT in the webroot!
my $maxlength = 10; # max length of usernames

# ----- Other variables, don't mess with them
my $username = '';
my $password = '';
my $mode = '';
my $result = 1;
my $returncode = '';
my $coords = '';
my $filename = '';
my %FORM = '';

#-----------------------------------
print "Content-type: text/plain\n\n";

&parseget();

($username,$password) = &getid($FORM{'un'},$FORM{'pw'});

# Filename controlled for security, can modify pre/suffix.
$filename = "ng-".$username.".txt";

# Decide what to do
if ($FORM{'cds'}) {
$mode = 'remote';
$coords = &validateNMEA($FORM{'cds'});
&remote;
$result = 'GPSOK';
} else {
$mode = 'recv';
$result = &receiver($username);
$result = "GPSOK$coords";
}

print "$result";

#----------------------------------
# ** Subs **
#----------------------------------

#----------------------------------
# It's all gone wrong!

sub failed() {
my $error = shift;
print "GPSERROR:$error:\n\n";
exit 0;
}

#----------------------------------
# Remote is calling, supplying coords.

sub remote() {
if (-e "$filedir$filename") {
my $filecont = '';
my $pwd2 = '';
open FNAME, "$filedir$filename" or &failed("Proc error: RCV-FN1");
$filecont = <FNAME> or &failed("Proc error: RCV-FN2");
close FNAME or &failed("Proc error: RCV-FN3");

($pwd2,) = split /\@/, $filecont;

if ($pwd2 ne $password) {
&failed("Incorrect password/unknown username.");
}
}

open FNAME, ">$filedir$filename" or &failed("{Proc error: REM-FN1");
print FNAME "$password\@$coords\n" or &failed("Proc error: REM-FN2");
close FNAME or &failed("Proc error: REM-FN3");
}

#----------------------------------
# Receiver is calling, wanting coords. Also validate password.

sub receiver() {
my $filecont = '';
my $pwd2 = '';
open FNAME, "$filedir$filename" or &failed("Proc error: RCV-FN1");
$filecont = <FNAME> or &failed("Proc error: RCV-FN2");
close FNAME or &failed("Proc error: RCV-FN3");

($pwd2,$coords) = split /\@/, $filecont;

if ($pwd2 ne $password) {
&failed("Incorrect password/unknown username.");
}
}

#----------------------------------
# Validate username

sub getid() {
($uname,$pwd) = @_;

# we don't want overly long names for security.
if (length($uname) > $maxlength or length($pwd) > $maxlength) {
&failed("Incorrect password/unknown username.");
}

# only allow a-z, A-Z and 0-9 as usernames.
if ($uname!~/^[a-zA-Z0-9]+$/) {
&failed("Incorrect password/unknown username.");
}

# be a bit more generous for passwords
if ($pwd!~/^[a-zA-Z0-9\-]+$/) {
&failed("Incorrect password/unknown username.");
}

return ($uname,$pwd);
}

#ERR:Invalid NMEA data: '$GPRMC,143238,V,3731.0466,S,14523.8828,E,37.8,191.0,201202,11.8,E,S*0B' Code IV:
#----------------------------------
# Validate NMEA string

sub validateNMEA() {
my $cds = shift;

# NMEA sentences should be no more than 82 chars
if (length($cds) > 82) {
&failed("Invalid NMEA data: '$cds' Code L");
}

# Regex to match four NMEA sentences
#if ( $cds=~/^(\$GP(RMC|GSA|GSV|GGA)[ABCDFVMNESW0-9\,\*\.]+)$/ ) {
if ( $cds=~/^(\$GP(RMC|GSA|GSV|GGA)[AVMNESW0-9\,\.]+\*[ABCDEF0-9][ABCDEF0-9]?)$/ ) {
return $1;
} else {
&failed("Invalid NMEA data: '$cds' Code IV");
}
}

#----------------------------------
# Parse GET data

sub parseget() {
my @pairs;
my $name, $value;
@pairs = split(/&/, $ENV{'QUERY_STRING'});
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<(([^>])*)>//g;
$FORM{$name} = $value;
}
}




thanks any any help would be great


shawnhcorey
Enthusiast


Jul 17, 2009, 8:15 AM

Post #3 of 8 (829 views)
Re: [dysolve] writing to .pl file [In reply to] Can't Post


In Reply To
Hello all,

I found a perl script on the net that lets you upload gps info to a webserver using perl and then another user can download it as if they are connected to my gps remotely. I know the program worked in the past as my friend did have it working but my issue is after installing it on the server the script build gives you a list of tests to do to make sure it works, but when I run the first test it fails .. the program is called NETGPS below is some of the scripting ,the test i run and the error. from my testing it seems to be an issue with permission but I have all files and folder set to 755 (777 gives a 401 error)


Are you running it on Windows? Some servers on Windows rely on the shebang rather than the registry to find perl. If you're running on Windows, find out where the PERL.EXE is and change the shebang to it. For example, if it's located at C:\PERL\BIN\PERL.EXE, make the first line in the script:

Code
#!c:/perl/bin/perl


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


KevinR
Veteran


Jul 17, 2009, 11:01 AM

Post #4 of 8 (826 views)
Re: [dysolve] writing to .pl file [In reply to] Can't Post

have you defined a name and password?

my $username = '';
my $password = '';
-------------------------------------------------


dysolve
Novice

Jul 17, 2009, 4:48 PM

Post #5 of 8 (820 views)
Re: [shawnhcorey] writing to .pl file [In reply to] Can't Post

I am running it on a unix webserver, and I have tried the test.pl and that works fine.


dysolve
Novice

Jul 17, 2009, 4:50 PM

Post #6 of 8 (819 views)
Re: [KevinR] writing to .pl file [In reply to] Can't Post

The second test he script maker wants done is to submit a username and password to the script but again it come back with a error, for some reason now I am only getting 500 internal server errors


dysolve
Novice

Jul 17, 2009, 6:47 PM

Post #7 of 8 (813 views)
Re: [dysolve] writing to .pl file [In reply to] Can't Post

ok, i did more testing and now i get the following "answer" to test one : GPSERROR:Incorrect password/unknown username.:

so i ran test two which is to supply a username and password

Test Step 2

Test:No user data found for Receiver.

Input:http://servername/netgps.pl?un=joe&pw=bloggs

Expected Result:GPSERROR:No user data found.

but the answer i get returned is "GPSERROR:Proc error: RCV-FN1: " so now I am stumped..




dysolve
Novice

Jul 17, 2009, 7:07 PM

Post #8 of 8 (812 views)
Re: [dysolve] writing to .pl file [In reply to] Can't Post

ok i just noticed this is the help file

Perl Script

It is assumed Perl is already installed on your webserver. If not, see Appendix

F for assistance.

1. Decide on a directory that is outside the web root, and that the UID

running perl has RWD access to.

2. Edit the Perl script, netgps.pl and change the variable defining the

directory to the directory you defined in Step 1.



3. Test (see below).



I am wondering if I have any issue with the UID running perl.


 
 


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

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