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();
14 rodolico 102
   if ( $$parameters{'report'} ) {
15 rodolico 103
      &writeFile( $saveFileName, $message ); # make a backup copy of the report
14 rodolico 104
      $postData{ $$parameters{'report'} } =  [ #'/tmp/theReport' => 'myReport'
105
                                                undef,
106
                                                'temp.report',
107
                                                'Content-Type' => 'application/text',
108
                                                'Content' => $message
109
                                             ];
1 rodolico 110
   } else {
14 rodolico 111
      return 5; # no key given for report
13 rodolico 112
   }
14 rodolico 113
   if ( $$parameters{'date'} ) {
114
      $postData{ $$parameters{'date'} } = $$parameters{'report date'};
115
   } else {
116
      return 6; # can't process because we have no report date
117
   }
118
   if ( $$parameters{'hostname'} ) {
119
      $postData{ $$parameters{'hostname'} } = $$parameters{'host name'};
120
   } else {
121
      return 7; # no hostname
122
   }
123
   if ( $$parameters{'client'} ) {
124
      $postData{ $$parameters{'client'} } = $$parameters{'client name'};
125
   } else {
126
      return 8; # no client name
127
   }
128
   if ( $$parameters{'serial'} ) {
129
      $postData{ $$parameters{'serial'} } = $$parameters{'serial number'};
130
   } else {
131
      return 9; # no serial number
132
   }
133
   my $result = sendData( $url, \%postData );
134
   if ( $result ) { # we got a response, so validate the transfer
15 rodolico 135
      #print $result->content . "\n";
136
      unless ( ( $result->content * 1) == length($message) ) { # report returned was not one we sent
137
         print STDERR "Server did not read file correctly\nWe sent " . length($message) . " bytes but server only saved $result->content\n";
138
         return 3;
139
      } 
14 rodolico 140
   } else {
141
      print STDERR "URL did not work. Full URI sent follows\n$url\n";
142
      return 4; # crud, could not even find the URL
143
   }
15 rodolico 144
   `rm $saveFileName`;
14 rodolico 145
   return 1;
1 rodolico 146
}
14 rodolico 147
 
148
1;
149
 
150
 
151
#