Subversion Repositories camp_sysinfo_client_3

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
26 rodolico 1
#! /usr/bin/env perl
1 rodolico 2
 
3
#    Program: upload_http.pl
4
#    Copyright (C) 2016  R. W. Rodolico
5
#
6
#    Description
7
#      Script will upload file passed on STDIN to URL defined in $server
8
#      using variable name $urlVarName.
9
#      Filename may also be passed on command line
10
#      It assumes the remote server will return the exact contents which
11
#      is then used to compare to what was sent
12
#      It will return the following codes
13
#        0 - success
14
#         1 - Could not find LWP::Simple module
15
#         2 - file not passed on STDIN
16
#         3 - Contents returned by server do not match our local copy
17
#         4 - URL call returned a "not found" error
14 rodolico 18
#         5 - no key given for report contents
19
#         6 - no key given for report date
20
#         7 - no key given for hostname
21
#         8 - no key given for client
22
#         9 - no key given for serial number
1 rodolico 23
#
24
#    This program is free software: you can redistribute it and/or modify
25
#    it under the terms of the GNU General Public License as published by
26
#    the Free Software Foundation, either version 3 of the License, or
27
#    (at your option) any later version.
28
#
29
#    This program is distributed in the hope that it will be useful,
30
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
31
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32
#    GNU General Public License for more details.
33
#
34
#    You should have received a copy of the GNU General Public License
35
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
36
 
26 rodolico 37
use warnings;
38
use strict;
39
 
40
our $VERSION = '1.0';
41
 
1 rodolico 42
eval {
14 rodolico 43
   use LWP;
1 rodolico 44
};
45
if ( $@ ) {
14 rodolico 46
   print STDERR "Could not load library LWP\n";
47
   exit 1; # error 1 means we could not find LWP
1 rodolico 48
}
49
 
14 rodolico 50
use Data::Dumper;
51
 
1 rodolico 52
sub urlEncode {
53
   my $contents = shift;
54
   # escape report for URI
55
   eval {   # let's use URI::Escape if it is installed
56
      use URI::Escape;
57
   };
58
   if ( ! $@ ) { # we loaded URI::Escape
59
      return uri_escape( $contents );
60
   } else {
61
      # nope, URI::Escape was not installed (or died) 
62
      # so use our home grown thing
63
      # kind of a funky way to write it, but it should be very, very fast
64
      # will take the document passed and split it into array of individual 
65
      # characters -- split ( '', shift ) 
66
      # that then will go through map, which converts it to the for %xx 
67
      # required for URL encoding IF it is not alphanumeric.
68
      return join( '', 
69
            (
70
               map
71
                  { ($_ =~ m/[a-zA-Z0-9]/) ? $_ :  '%' . sprintf("%2.2x",ord( $_ ) ) } 
72
                  split ( '', $contents ) 
73
            ) 
74
         );
75
   } # if
76
}
77
 
14 rodolico 78
sub writeFile {
79
   my ( $filename, $contents ) = @_;
80
   open FILE,">$filename" or die "Could not write to $filename: $!\n";
81
   print FILE $contents;
82
   close FILE;
83
}
84
 
85
sub sendData {
86
   my ( $url, $parameters ) = @_;
15 rodolico 87
   # &writeFile( "/tmp/upload_http", Dumper( $parameters ) );
14 rodolico 88
   my $browser = LWP::UserAgent->new();
89
   my $response = $browser->post(
90
       $url,
91
       $parameters,
92
       'Content_Type' => 'form-data'
93
       );
94
   return $response;
95
}
96
 
15 rodolico 97
sub filename {
98
   return '/tmp/' . time . '.sysinfo';
99
}
14 rodolico 100
 
13 rodolico 101
sub doit {
102
   my ( $parameters, $message ) = @_;
103
   my $url = $$parameters{'URL'} or die "Could not find URL to send to\n";
104
 
14 rodolico 105
   my %postData;
15 rodolico 106
   my $saveFileName = &filename();
17 rodolico 107
   if ( $$parameters{'key for report'} ) {
15 rodolico 108
      &writeFile( $saveFileName, $message ); # make a backup copy of the report
17 rodolico 109
      $postData{ $$parameters{'key for report'} } =  
110
                              [ #'/tmp/theReport' => 'myReport'
111
                                 undef,
112
                                 'temp.report',
113
                                 'Content-Type' => 'application/text',
114
                                 'Content' => $message
115
                              ];
1 rodolico 116
   } else {
14 rodolico 117
      return 5; # no key given for report
13 rodolico 118
   }
17 rodolico 119
   if ( $$parameters{'key for date'} ) {
120
      $postData{ $$parameters{'key for date'} } = $$parameters{'report date'};
14 rodolico 121
   } else {
122
      return 6; # can't process because we have no report date
123
   }
17 rodolico 124
   if ( $$parameters{'key for hostname'} ) {
125
      $postData{ $$parameters{'key for hostname'} } = $$parameters{'host name'};
14 rodolico 126
   } else {
127
      return 7; # no hostname
128
   }
17 rodolico 129
   if ( $$parameters{'key for client'} ) {
130
      $postData{ $$parameters{'key for client'} } = $$parameters{'client name'};
14 rodolico 131
   } else {
132
      return 8; # no client name
133
   }
17 rodolico 134
   if ( $$parameters{'key for serial number'} ) {
135
      $postData{ $$parameters{'key for serial number'} } = $$parameters{'serial number'};
14 rodolico 136
   } else {
137
      return 9; # no serial number
138
   }
139
   my $result = sendData( $url, \%postData );
140
   if ( $result ) { # we got a response, so validate the transfer
15 rodolico 141
      #print $result->content . "\n";
142
      unless ( ( $result->content * 1) == length($message) ) { # report returned was not one we sent
143
         print STDERR "Server did not read file correctly\nWe sent " . length($message) . " bytes but server only saved $result->content\n";
144
         return 3;
145
      } 
14 rodolico 146
   } else {
147
      print STDERR "URL did not work. Full URI sent follows\n$url\n";
148
      return 4; # crud, could not even find the URL
149
   }
15 rodolico 150
   `rm $saveFileName`;
14 rodolico 151
   return 1;
1 rodolico 152
}
14 rodolico 153
 
154
1;
155
 
156
 
157
#