]> git.donarmstrong.com Git - bin.git/blob - sshsendmail
added sshsendmail
[bin.git] / sshsendmail
1 #! /usr/bin/perl
2
3 # parse command line options
4
5 # connect to host
6
7 use warnings;
8 use strict;
9
10 use Getopt::Long;
11 use Pod::Usage;
12
13 =head1 NAME
14
15 sshsendmail - Uses ssh to send a mail message to a different machine's copy of sendmail.
16
17 =head1 SYNOPSIS
18
19 ss [options]
20
21  Options:
22   --host, -H host to upload image to
23   --dir, -D dir to place image (on host of -H set)
24   --import-options, -I options to import (default -window root)
25   --convert-options, -C options to convert (for scaling)
26   --scale, -s make scaled image (default)
27   --debug, -d debugging level (Default 0)
28   --help, -h display this help
29   --man, -m display manual
30
31 =head1 OPTIONS
32
33 =over
34
35 =item B<--identity, -i>
36
37 ssh identity to send to the server we're connecting to
38
39 =item B<--debug, -d>
40
41 Debug verbosity. (Default 0)
42
43 =item B<--help, -h>
44
45 Display brief useage information.
46
47 =item B<--man, -m>
48
49 Display this manual.
50
51 =back
52
53 =head1 EXAMPLES
54
55   ss
56
57 Will pretty much do what you want
58
59   ss -I
60
61 Will take a picture of a window you select.
62
63 =cut
64
65 use User;
66 use File::Basename qw(basename);
67 use IO::Handle;
68 use IO::File;
69
70 use vars qw($DEBUG);
71
72 $0 = basename($0);
73
74 # XXX parse config file
75
76 my %options = (debug              => 0,
77                help               => 0,
78                man                => 0,
79                host               => undef,
80                identity           => undef,
81                username           => undef,
82                log                => '/var/mail/log',
83                'sendmail-options' => '',
84               );
85
86 GetOptions(\%options,'identity|i=s','username|l=s','daemon|d', 'syslog|s',
87            'sendmail-options|o=s','log|l=s',
88            'help|h|?','man|m');
89
90 pod2usage() if $options{help};
91 pod2usage({verbose=>2}) if $options{man};
92
93 $DEBUG = $options{debug};
94
95 if (not @ARGV) {
96      print STDERR "${0}: Too few command-line arguments\n";
97      print <<END;
98 usage: ${0} [flags] remote-address < mail-file
99 Send an email message via ssh+sendmail
100   -p, --port=INT  Set the port number on the remote host to connect to
101   -d, --daemon    use syslog exclusively  (Debian only)
102   -s, --syslog    use syslog additionally (Debian only)
103
104   -h, --help      Display this help and exit
105 END
106      exit(1);
107 }
108
109 my $hostname = shift @ARGV;
110
111 my @message = <>;
112 #throw away envelope sender
113 shift @message;
114 my @recipients;
115
116 while (my $line = shift @message) {
117      last if $line eq "\n";
118      chomp $line;
119      push @recipients,$line;
120 }
121
122 @recipients = qw(-t) if not @recipients;
123
124 my @ssh_arguments = ($hostname);
125
126 push @ssh_arguments, '-i', $options{identity} if defined $options{identity};
127 push @ssh_arguments, '-l', $options{username} if defined $options{username};
128 push @ssh_arguments, q(cat - | /usr/lib/sendmail ).$options{'sendmail-options'}.' '.join(' ',@recipients);
129 qx(ping -q -c 3 $hostname 2>/dev/null);
130 if ($?) {
131      print STDERR "${0}: Failed: unable to ping $hostname\n";
132      exit (9);
133 }
134 my $ssh = new IO::Handle;
135 my $log = new IO::File $options{log},'w+' or exit(2);
136 print {$log} @message or exit(2);
137 print {$log} join(' ',('ssh',@ssh_arguments)) or exit(2);
138 open($ssh,'|-','ssh',@ssh_arguments) or exit(17);
139 print {$ssh} @message or exit(17);
140 close $ssh or exit(17);
141 if ($?) {
142      print STDERR "${0}: Failed: sendmail died for some reason\n";
143      print {$log} "${0}: Failed: sendmail died for some reason\n";
144      exit (17);
145 }
146 else {
147      print STDERR "${0}: Succeeded: Yeay\n";
148      print {$log} "${0}: Failed: sendmail died for some reason\n";
149      exit 0;
150 }