35d20b38f515d4ceab8aa081b61ed3b6d15b91c9
[Net-SSH.git] / SSH.pm
1 package Net::SSH;
2
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT_OK $ssh $equalspace $DEBUG);
5 use Exporter;
6 use IO::File;
7 use IPC::Open2;
8 use IPC::Open3;
9
10 @ISA = qw(Exporter);
11 @EXPORT_OK = qw( ssh issh ssh_cmd sshopen2 sshopen3 );
12 $VERSION = '0.06';
13
14 $DEBUG = 0;
15
16 $ssh = "ssh";
17
18 =head1 NAME
19
20 Net::SSH - Perl extension for secure shell
21
22 =head1 SYNOPSIS
23
24   use Net::SSH qw(ssh issh sshopen2 sshopen3);
25
26   ssh('user@hostname', $command);
27
28   issh('user@hostname', $command);
29
30   ssh_cmd('user@hostname', $command);
31   ssh_cmd( {
32     user => 'user',
33     host => 'host.name',
34     command => 'command',
35     args => [ '-arg1', '-arg2' ],
36     stdin_string => "string\n",
37   } );
38
39   sshopen2('user@hostname', $reader, $writer, $command);
40
41   sshopen3('user@hostname', $writer, $reader, $error, $command);
42
43 =head1 DESCRIPTION
44
45 Simple wrappers around ssh commands.
46
47 For an all-perl implementation that does not require the system B<ssh> command,
48 see L<Net::SSH::Perl> instead.
49
50 =head1 SUBROUTINES
51
52 =over 4
53
54 =item ssh [USER@]HOST, COMMAND [, ARGS ... ]
55
56 Calls ssh in batch mode.
57
58 =cut
59
60 sub ssh {
61   my($host, @command) = @_;
62   &_check_ssh_version unless defined $equalspace;
63   my @cmd = ($ssh, '-o', 'BatchMode'.$equalspace.'yes', $host, @command);
64   warn "[Net::SSH::ssh] executing ". join(' ', @cmd). "\n"
65     if $DEBUG;
66   system(@cmd);
67 }
68
69 =item issh [USER@]HOST, COMMAND [, ARGS ... ]
70
71 Prints the ssh command to be executed, waits for the user to confirm, and
72 (optionally) executes the command.
73
74 =cut
75
76 sub issh {
77   my($host, @command) = @_;
78   my @cmd = ($ssh, $host, @command);
79   print join(' ', @cmd), "\n";
80   if ( &_yesno ) {
81     system(@cmd);
82   }
83 }
84
85 =item ssh_cmd [USER@]HOST, COMMAND [, ARGS ... ]
86
87 =item ssh_cmd OPTIONS_HASHREF
88
89 Calls ssh in batch mode.  Throws a fatal error if data occurs on the command's
90 STDERR.  Returns any data from the command's STDOUT.
91
92 If using the hashref-style of passing arguments, possible keys are:
93
94   user (optional)
95   host (requried)
96   command (required)
97   args (optional, arrayref)
98   stdin_string (optional) - written to the command's STDIN
99
100 =cut
101
102 sub ssh_cmd {
103   my($host, $stdin_string, @command);
104   if ( ref($_[0]) ) {
105     my $opt = shift;
106     $host = $opt->{host};
107     $host = $opt->{user}. '@'. $host if exists $opt->{user};
108     @command = ( $opt->{command} );
109     push @command, @{ $opt->{args} } if exists $opt->{args};
110     $stdin_string = $opt->{stdin_string};
111   } else {
112     ($host, @command) = @_;
113     undef $stdin_string;
114   }
115
116   my $reader = IO::File->new();
117   my $writer = IO::File->new();
118   my $error  = IO::File->new();
119
120   sshopen3( $host, $writer, $reader, $error, @command ) or die $!;
121
122   print $writer $stdin_string if defined $stdin_string;
123   close $writer;
124
125   local $/ = undef;
126   my $output_stream = <$reader>;
127   my $error_stream = <$error>;
128
129   if ( length $error_stream ) {
130     die "[Net:SSH::ssh_cmd] STDERR $error_stream";
131   }
132
133   return $output_stream;
134
135 }
136
137 =item sshopen2 [USER@]HOST, READER, WRITER, COMMAND [, ARGS ... ]
138
139 Connects the supplied filehandles to the ssh process (in batch mode).
140
141 =cut
142
143 sub sshopen2 {
144   my($host, $reader, $writer, @command) = @_;
145   &_check_ssh_version unless defined $equalspace;
146   open2($reader, $writer, $ssh, '-o', 'BatchMode'.$equalspace.'yes', $host, @command);
147 }
148
149 =item sshopen3 HOST, WRITER, READER, ERROR, COMMAND [, ARGS ... ]
150
151 Connects the supplied filehandles to the ssh process (in batch mode).
152
153 =cut
154
155 sub sshopen3 {
156   my($host, $writer, $reader, $error, @command) = @_;
157   &_check_ssh_version unless defined $equalspace;
158   open3($writer, $reader, $error, $ssh, '-o', 'BatchMode'.$equalspace.'yes', $host, @command);
159 }
160
161 sub _yesno {
162   print "Proceed [y/N]:";
163   my $x = scalar(<STDIN>);
164   $x =~ /^y/i;
165 }
166
167 sub _check_ssh_version {
168   my $reader = IO::File->new();
169   my $writer = IO::File->new();
170   my $error  = IO::File->new();
171   open3($writer, $reader, $error, $ssh, '-V');
172   my $ssh_version = <$error>;
173   chomp($ssh_version);
174   if ( $ssh_version =~ /.*OpenSSH[-|_](\w+)\./ && $1 == 1 ) {
175     $equalspace = " ";
176   } else {
177     $equalspace = "=";
178   }
179 }
180
181 =back
182
183 =head1 EXAMPLE
184
185   use Net::SSH qw(sshopen2);
186   use strict;
187
188   my $user = "username";
189   my $host = "hostname";
190   my $cmd = "command";
191
192   sshopen2("$user\@$host", *READER, *WRITER, "$cmd") || die "ssh: $!";
193
194   while (<READER>) {
195       chomp();
196       print "$_\n";
197   }
198
199   close(READER);
200   close(WRITER);
201
202 =head1 FREQUENTLY ASKED QUESTIONS
203
204 Q: How do you supply a password to connect with ssh within a perl script
205 using the Net::SSH module?
206
207 A: You don't.  Use RSA or DSA keys.  See the ssh-keygen(1) manpage.
208
209 Q: My script is "leaking" ssh processes.
210
211 A: See L<perlfaq8/"How do I avoid zombies on a Unix system">, L<IPC::Open2>,
212 L<IPC::Open3> and L<perlfunc/waitpid>.
213
214 =head1 AUTHORS
215
216 Ivan Kohler <ivan-netssh_pod@420.am>
217
218 John Harrison <japh@in-ta.net> contributed an example for the documentation.
219
220 Martin Langhoff <martin@cwa.co.nz> contributed the ssh_cmd command, and
221 Jeff Finucane <jeff@cmh.net> updated it and took care of the 0.04 release.
222
223 Anthony Awtrey <tony@awtrey.com> contributed a fix for those still using
224 OpenSSH v1.
225
226 =head1 COPYRIGHT
227
228 Copyright (c) 2002 Ivan Kohler.
229 Copyright (c) 2002 Freeside Internet Services, LLC
230 All rights reserved.
231 This program is free software; you can redistribute it and/or modify it under
232 the same terms as Perl itself.
233
234 =head1 BUGS
235
236 Not OO.
237
238 Look at IPC::Session (also fsh)
239
240 =head1 SEE ALSO
241
242 For an all-perl implementation that does not require the system B<ssh> command,
243 see L<Net::SSH::Perl> instead.
244
245 ssh-keygen(1), ssh(1), L<IO::File>, L<IPC::Open2>, L<IPC::Open3>
246
247 =cut
248
249 1;
250