Subversion Repositories camp_sysinfo_client_3

Rev

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

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