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