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