]> git.donarmstrong.com Git - bin.git/blob - sshsendmail
clarify licensing of sshsendmail
[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 ss [options]
25
26  Options:
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
35
36 =head1 OPTIONS
37
38 =over
39
40 =item B<--identity, -i>
41
42 ssh identity to send to the server we're connecting to
43
44 =item B<--debug, -d>
45
46 Debug verbosity. (Default 0)
47
48 =item B<--help, -h>
49
50 Display brief useage information.
51
52 =item B<--man, -m>
53
54 Display this manual.
55
56 =back
57
58 =head1 EXAMPLES
59
60   ss
61
62 Will pretty much do what you want
63
64   ss -I
65
66 Will take a picture of a window you select.
67
68 =cut
69
70 use User;
71 use File::Basename qw(basename);
72 use IO::Handle;
73 use IO::File;
74
75 use vars qw($DEBUG);
76
77 $0 = basename($0);
78
79 # XXX parse config file
80
81 my %options = (debug              => 0,
82                help               => 0,
83                man                => 0,
84                host               => undef,
85                identity           => undef,
86                username           => undef,
87                log                => '/var/mail/log',
88                'sendmail-options' => '',
89               );
90
91 GetOptions(\%options,'identity|i=s','username|l=s','daemon|d', 'syslog|s',
92            'sendmail-options|o=s','log|l=s',
93            'help|h|?','man|m');
94
95 pod2usage() if $options{help};
96 pod2usage({verbose=>2}) if $options{man};
97
98 $DEBUG = $options{debug};
99
100 if (not @ARGV) {
101      print STDERR "${0}: Too few command-line arguments\n";
102      print <<END;
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)
108
109   -h, --help      Display this help and exit
110 END
111      exit(1);
112 }
113
114 my $hostname = shift @ARGV;
115
116 my @message = <>;
117 #throw away envelope sender
118 shift @message;
119 my @recipients;
120
121 while (my $line = shift @message) {
122      last if $line eq "\n";
123      chomp $line;
124      push @recipients,$line;
125 }
126
127 @recipients = qw(-t) if not @recipients;
128
129 my @ssh_arguments = ($hostname);
130
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);
135 if ($?) {
136      print STDERR "${0}: Failed: unable to ping $hostname\n";
137      exit (9);
138 }
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);
146 if ($?) {
147      print STDERR "${0}: Failed: sendmail died for some reason\n";
148      print {$log} "${0}: Failed: sendmail died for some reason\n";
149      exit (17);
150 }
151 else {
152      print STDERR "${0}: Succeeded: Yeay\n";
153      print {$log} "${0}: Succeeded: Yeay\n";
154      exit 0;
155 }