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.
27 --host, -H host to upload image to
28 --dir, -D dir to place image (on host of -H set)
29 --import-options, -I options to import (default -window root)
30 --convert-options, -C options to convert (for scaling)
31 --scale, -s make scaled image (default)
32 --debug, -d debugging level (Default 0)
33 --help, -h display this help
34 --man, -m display manual
40 =item B<--identity, -i>
42 ssh identity to send to the server we're connecting to
46 Debug verbosity. (Default 0)
50 Display brief useage information.
62 Will pretty much do what you want
66 Will take a picture of a window you select.
71 use File::Basename qw(basename);
79 # XXX parse config file
81 my %options = (debug => 0,
87 log => '/var/mail/log',
88 'sendmail-options' => '',
91 GetOptions(\%options,'identity|i=s','username|l=s','daemon|d', 'syslog|s',
92 'sendmail-options|o=s','log|l=s',
95 pod2usage() if $options{help};
96 pod2usage({verbose=>2}) if $options{man};
98 $DEBUG = $options{debug};
101 print STDERR "${0}: Too few command-line arguments\n";
103 usage: ${0} [flags] remote-address < mail-file
104 Send an email message via ssh+sendmail
105 -p, --port=INT Set the port number on the remote host to connect to
106 -d, --daemon use syslog exclusively (Debian only)
107 -s, --syslog use syslog additionally (Debian only)
109 -h, --help Display this help and exit
114 my $hostname = shift @ARGV;
117 #throw away envelope sender
121 while (my $line = shift @message) {
122 last if $line eq "\n";
124 push @recipients,$line;
127 @recipients = qw(-t) if not @recipients;
129 my @ssh_arguments = ($hostname);
131 push @ssh_arguments, '-i', $options{identity} if defined $options{identity};
132 push @ssh_arguments, '-l', $options{username} if defined $options{username};
133 push @ssh_arguments, q(cat - | /usr/lib/sendmail ).$options{'sendmail-options'}.' '.join(' ',@recipients);
134 qx(ping -q -c 3 $hostname 2>/dev/null);
136 print STDERR "${0}: Failed: unable to ping $hostname\n";
139 my $ssh = new IO::Handle;
140 my $log = new IO::File $options{log},'w+' or exit(2);
141 print {$log} @message or exit(2);
142 print {$log} join(' ',('ssh',@ssh_arguments)) or exit(2);
143 open($ssh,'|-','ssh',@ssh_arguments) or exit(17);
144 print {$ssh} @message or exit(17);
145 close $ssh or exit(17);
147 print STDERR "${0}: Failed: sendmail died for some reason\n";
148 print {$log} "${0}: Failed: sendmail died for some reason\n";
152 print STDERR "${0}: Succeeded: Yeay\n";
153 print {$log} "${0}: Succeeded: Yeay\n";