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
6 # Copyright 2005-10 by Don Armstrong <don@donarmstrong.com>.
8 # parse command line options
20 sshsendmail - Uses ssh to send a mail message to a different machine's copy of sendmail.
24 Stick this command in /usr/lib/nullmailer/sshsendmail.
28 foohost.com sshsendmail --identity=/var/mail/.ssh/id_rsa --username remotemail --sendmail-options='-baruser@foohost.com'
30 to /etc/nullmailer/remotes.
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
44 =item B<--identity, -i>
46 ssh identity to send to the server we're connecting to
50 Debug verbosity. (Default 0)
54 Display brief useage information.
66 use File::Basename qw(basename);
74 # XXX parse config file
76 my %options = (debug => 0,
82 'sendmail-options' => '',
85 GetOptions(\%options,'identity|i=s','username|l=s','daemon|d', 'syslog|s',
86 'sendmail-options|o=s',
89 pod2usage() if $options{help};
90 pod2usage({verbose=>2}) if $options{man};
92 $DEBUG = $options{debug};
95 print STDERR "${0}: Too few command-line arguments\n";
97 usage: ${0} [flags] remote-address < mail-file
98 Send an email message via ssh+sendmail
99 -p, --port=INT Set the port number on the remote host to connect to
100 -d, --daemon use syslog exclusively (Debian only)
101 -s, --syslog use syslog additionally (Debian only)
103 -h, --help Display this help and exit
108 my $hostname = shift @ARGV;
111 #throw away envelope sender
115 while (my $line = shift @message) {
116 last if $line eq "\n";
118 push @recipients,$line;
121 @recipients = qw(-t) if not @recipients;
123 my @ssh_arguments = ($hostname);
125 push @ssh_arguments, '-i', $options{identity} if defined $options{identity};
126 push @ssh_arguments, '-l', $options{username} if defined $options{username};
127 push @ssh_arguments, q(cat - | /usr/lib/sendmail ).$options{'sendmail-options'}.' '.join(' ',@recipients);
128 qx(ping -q -c 3 $hostname 2>/dev/null);
130 print STDERR "${0}: Failed: unable to ping $hostname\n";
133 my $ssh = new IO::Handle;
134 open($ssh,'|-','ssh',@ssh_arguments) or exit(17);
135 print {$ssh} @message or exit(17);
136 close $ssh or exit(17);
138 print STDERR "${0}: Failed: sendmail died for some reason\n";
142 print STDERR "${0}: Succeeded: Yeay\n";