Subversion Repositories camp_sysinfo_client_3

Rev

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