#! /usr/bin/perl # sshsendmail uses ssh to send a mail message to a different machine, # and is released under the terms of the GPL version 2, or any later # version, at your option. See the file README and COPYING for more # information. # Copyright 2005-10 by Don Armstrong . # parse command line options # connect to host use warnings; use strict; use Getopt::Long; use Pod::Usage; =head1 NAME sshsendmail - Uses ssh to send a mail message to a different machine's copy of sendmail. =head1 SYNOPSIS Stick this command in /usr/lib/nullmailer/sshsendmail. Then add foohost.com sshsendmail --identity=/var/mail/.ssh/id_rsa --username remotemail --sendmail-options='-baruser@foohost.com' to /etc/nullmailer/remotes. Options: --identity, -i ssh identity to use to connect to the server --username, -l remote username --sendmail-options, -o options to pass to sendmail --debug, -d debugging level (Default 0) --help, -h display this help --man, -m display manual =head1 OPTIONS =over =item B<--identity, -i> ssh identity to send to the server we're connecting to =item B<--debug, -d> Debug verbosity. (Default 0) =item B<--help, -h> Display brief useage information. =item B<--man, -m> Display this manual. =back =cut use User; use File::Basename qw(basename); use IO::Handle; use IO::File; use vars qw($DEBUG); $0 = basename($0); # XXX parse config file my %options = (debug => 0, help => 0, man => 0, host => undef, identity => undef, username => undef, 'sendmail-options' => '', ); GetOptions(\%options,'identity|i=s','username|l=s','daemon|d', 'syslog|s', 'sendmail-options|o=s', 'help|h|?','man|m'); pod2usage() if $options{help}; pod2usage({verbose=>2}) if $options{man}; $DEBUG = $options{debug}; if (not @ARGV) { print STDERR "${0}: Too few command-line arguments\n"; print <; #throw away envelope sender shift @message; my @recipients; while (my $line = shift @message) { last if $line eq "\n"; chomp $line; push @recipients,$line; } @recipients = qw(-t) if not @recipients; my @ssh_arguments = ($hostname); push @ssh_arguments, '-i', $options{identity} if defined $options{identity}; push @ssh_arguments, '-l', $options{username} if defined $options{username}; push @ssh_arguments, q(cat - | /usr/lib/sendmail ).$options{'sendmail-options'}.' '.join(' ',@recipients); qx(ping -q -c 3 $hostname 2>/dev/null); if ($?) { print STDERR "${0}: Failed: unable to ping $hostname\n"; exit (9); } my $ssh = new IO::Handle; open($ssh,'|-','ssh',@ssh_arguments) or exit(17); print {$ssh} @message or exit(17); close $ssh or exit(17); if ($?) { print STDERR "${0}: Failed: sendmail died for some reason\n"; exit (17); } else { print STDERR "${0}: Succeeded: Yeay\n"; exit 0; }