Rev 3 | Blame | Last modification | View Log | Download | RSS feed
#! /usr/bin/perl -w
# Program: upload_http.pl
# Copyright (C) 2016 R. W. Rodolico
#
# Description
# Script will upload file passed on STDIN to URL defined in $server
# using variable name $urlVarName.
# Filename may also be passed on command line
# It assumes the remote server will return the exact contents which
# is then used to compare to what was sent
# It will return the following codes
# 0 - success
# 1 - Could not find LWP::Simple module
# 2 - file not passed on STDIN
# 3 - Contents returned by server do not match our local copy
# 4 - URL call returned a "not found" error
# 5 - a filename was passed on the cli but was not able to be opened
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
my $urlVarName = 'report';
my $server = 'http://10.111.110.72/modules/sysinfo/upload_sysinfo_report.php';
eval {
use LWP::Simple;
};
if ( $@ ) {
print STDERR "Could not load library LWP::Simple\n";
exit 1; # error 1 means we could not find LWP::Simple
}
sub urlEncode {
my $contents = shift;
# escape report for URI
eval { # let's use URI::Escape if it is installed
use URI::Escape;
};
if ( ! $@ ) { # we loaded URI::Escape
return uri_escape( $contents );
} else {
# nope, URI::Escape was not installed (or died)
# so use our home grown thing
# kind of a funky way to write it, but it should be very, very fast
# will take the document passed and split it into array of individual
# characters -- split ( '', shift )
# that then will go through map, which converts it to the for %xx
# required for URL encoding IF it is not alphanumeric.
return join( '',
(
map
{ ($_ =~ m/[a-zA-Z0-9]/) ? $_ : '%' . sprintf("%2.2x",ord( $_ ) ) }
split ( '', $contents )
)
);
} # if
}
# figure out where we are reading from
if ( $filename = shift ) { # they passed a file name on the command line
if ( open DATA, $filename ) {
$report = do {local $/=undef; <DATA>}; # join( '',<DATA> )
close DATA;
} else {
print "Unable to open $filename: $!\n" ;
exit 5;
};
} else {
$report = do {local $/=undef; <>}; #join( '', <> );
}
unless ( $report ) { # Could not be read
print STDERR "Could not read report\n";
exit 2;
}
# send it to the server and get the result
my $result = get( "$server?$urlVarName=" . urlEncode( $report ) );
if ( $result ) { # we got a response, so validate the transfer
unless ( $result eq $report ) { # report returned was not one we sent
print STDERR "Server did not read file correctly\n";
exit 3;
}
} else {
print STDERR "URL $server did not work\n";
exit 4; # crud, could not even find the URL
}
print $result; # only used for debuggin
print STDERR "Ok\n";
exit 0; # all good
Generated by GNU Enscript 1.6.5.90.