Subversion Repositories camp_sysinfo_client_3

Rev

Rev 233 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
26 rodolico 1
#! /usr/bin/env perl
1 rodolico 2
 
233 rodolico 3
#    Program: upload_http
1 rodolico 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/>.
233 rodolico 36
#
37
# version 1.1 RWR, 20240518
38
# Setting up so only UTF8 sent
1 rodolico 39
 
26 rodolico 40
use warnings;
41
use strict;
233 rodolico 42
use open ':std', ':encoding(utf8)' ; # force all I/O, including disk and STD?, to use utf-8
26 rodolico 43
 
233 rodolico 44
our $VERSION = '1.1';
26 rodolico 45
 
1 rodolico 46
eval {
14 rodolico 47
   use LWP;
1 rodolico 48
};
49
if ( $@ ) {
14 rodolico 50
   print STDERR "Could not load library LWP\n";
51
   exit 1; # error 1 means we could not find LWP
1 rodolico 52
}
53
 
14 rodolico 54
use Data::Dumper;
197 rodolico 55
use Scalar::Util qw( looks_like_number );
14 rodolico 56
 
1 rodolico 57
sub urlEncode {
58
   my $contents = shift;
59
   # escape report for URI
60
   eval {   # let's use URI::Escape if it is installed
61
      use URI::Escape;
62
   };
63
   if ( ! $@ ) { # we loaded URI::Escape
64
      return uri_escape( $contents );
65
   } else {
66
      # nope, URI::Escape was not installed (or died) 
67
      # so use our home grown thing
68
      # kind of a funky way to write it, but it should be very, very fast
69
      # will take the document passed and split it into array of individual 
70
      # characters -- split ( '', shift ) 
71
      # that then will go through map, which converts it to the for %xx 
72
      # required for URL encoding IF it is not alphanumeric.
73
      return join( '', 
74
            (
75
               map
76
                  { ($_ =~ m/[a-zA-Z0-9]/) ? $_ :  '%' . sprintf("%2.2x",ord( $_ ) ) } 
77
                  split ( '', $contents ) 
78
            ) 
79
         );
80
   } # if
81
}
82
 
14 rodolico 83
sub writeFile {
84
   my ( $filename, $contents ) = @_;
85
   open FILE,">$filename" or die "Could not write to $filename: $!\n";
86
   print FILE $contents;
87
   close FILE;
88
}
89
 
90
sub sendData {
91
   my ( $url, $parameters ) = @_;
15 rodolico 92
   # &writeFile( "/tmp/upload_http", Dumper( $parameters ) );
14 rodolico 93
   my $browser = LWP::UserAgent->new();
94
   my $response = $browser->post(
95
       $url,
96
       $parameters,
97
       'Content_Type' => 'form-data'
98
       );
99
   return $response;
100
}
101
 
15 rodolico 102
sub filename {
103
   return '/tmp/' . time . '.sysinfo';
104
}
14 rodolico 105
 
13 rodolico 106
sub doit {
107
   my ( $parameters, $message ) = @_;
108
   my $url = $$parameters{'URL'} or die "Could not find URL to send to\n";
109
 
14 rodolico 110
   my %postData;
15 rodolico 111
   my $saveFileName = &filename();
17 rodolico 112
   if ( $$parameters{'key for report'} ) {
15 rodolico 113
      &writeFile( $saveFileName, $message ); # make a backup copy of the report
17 rodolico 114
      $postData{ $$parameters{'key for report'} } =  
115
                              [ #'/tmp/theReport' => 'myReport'
116
                                 undef,
117
                                 'temp.report',
234 rodolico 118
                                 'Content-Type' => 'application/x-www-form-urlencoded',
17 rodolico 119
                                 'Content' => $message
120
                              ];
1 rodolico 121
   } else {
14 rodolico 122
      return 5; # no key given for report
13 rodolico 123
   }
17 rodolico 124
   if ( $$parameters{'key for date'} ) {
125
      $postData{ $$parameters{'key for date'} } = $$parameters{'report date'};
14 rodolico 126
   } else {
127
      return 6; # can't process because we have no report date
128
   }
17 rodolico 129
   if ( $$parameters{'key for hostname'} ) {
130
      $postData{ $$parameters{'key for hostname'} } = $$parameters{'host name'};
14 rodolico 131
   } else {
132
      return 7; # no hostname
133
   }
17 rodolico 134
   if ( $$parameters{'key for client'} ) {
135
      $postData{ $$parameters{'key for client'} } = $$parameters{'client name'};
14 rodolico 136
   } else {
137
      return 8; # no client name
138
   }
17 rodolico 139
   if ( $$parameters{'key for serial number'} ) {
140
      $postData{ $$parameters{'key for serial number'} } = $$parameters{'serial number'};
14 rodolico 141
   } else {
142
      return 9; # no serial number
143
   }
197 rodolico 144
   if ( $$parameters{'upload_type'} ) {
145
      $postData{ 'upload_type' } = $$parameters{'upload_type'};
146
   }
147
   $postData{'filename'} = 
148
      $$parameters{'report date'} . '_' . 
149
      $$parameters{'client name'} . '_' . 
150
      $$parameters{'host name'} . '_' . 
151
      $$parameters{'serial number'} . 
152
      '.yaml';
234 rodolico 153
   #print Dumper( \%postData ); die;
14 rodolico 154
   my $result = sendData( $url, \%postData );
197 rodolico 155
   if ( $result->is_success ) { # we got a response, so validate the transfer
156
      #print $result->status_line . "\n"; die;
157
      #unless ( ! looks_like_number( $result->content ) && ( $result->content * 1) == length($message) ) { # report returned was not one we sent
158
      #   print STDERR "Server did not read file correctly\nWe sent " . length($message) . " bytes but server only saved " . $result->content . "\n";
159
      #   return 3;
160
      #} 
14 rodolico 161
   } else {
162
      print STDERR "URL did not work. Full URI sent follows\n$url\n";
197 rodolico 163
      print STDERR "Return was " . $result->content . "\n";
14 rodolico 164
      return 4; # crud, could not even find the URL
165
   }
15 rodolico 166
   `rm $saveFileName`;
14 rodolico 167
   return 1;
1 rodolico 168
}
14 rodolico 169
 
170
1;
171
 
172
 
173
#