a47cc48811f2396384ab564eddba27313b6ba535
[Net-SSH.git] / SSH.pm
1 package Net::SSH;
2
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT_OK $ssh $equalspace $DEBUG @ssh_options);
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.07';
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   @ssh_options = &_ssh_options unless @ssh_options;
63   my @cmd = ($ssh, @ssh_options, $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   @ssh_options = &_ssh_options unless @ssh_options;
146   open2($reader, $writer, $ssh, @ssh_options, $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   @ssh_options = &_ssh_options unless @ssh_options;
158   open3($writer, $reader, $error, $ssh, @ssh_options, $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 _ssh_options {
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   my @options = ( '-o', 'BatchMode'.$equalspace.'yes' );
180   if ( $ssh_version =~ /.*OpenSSH[-|_](\w+)\./ && $1 > 1 ) {
181     unshift @options, '-T';
182   }
183   @options;
184 }
185
186 =back
187
188 =head1 EXAMPLE
189
190   use Net::SSH qw(sshopen2);
191   use strict;
192
193   my $user = "username";
194   my $host = "hostname";
195   my $cmd = "command";
196
197   sshopen2("$user\@$host", *READER, *WRITER, "$cmd") || die "ssh: $!";
198
199   while (<READER>) {
200       chomp();
201       print "$_\n";
202   }
203
204   close(READER);
205   close(WRITER);
206
207 =head1 FREQUENTLY ASKED QUESTIONS
208
209 Q: How do you supply a password to connect with ssh within a perl script
210 using the Net::SSH module?
211
212 A: You don't.  Use RSA or DSA keys.  See the ssh-keygen(1) manpage.
213
214 Q: My script is "leaking" ssh processes.
215
216 A: See L<perlfaq8/"How do I avoid zombies on a Unix system">, L<IPC::Open2>,
217 L<IPC::Open3> and L<perlfunc/waitpid>.
218
219 =head1 AUTHORS
220
221 Ivan Kohler <ivan-netssh_pod@420.am>
222
223 John Harrison <japh@in-ta.net> contributed an example for the documentation.
224
225 Martin Langhoff <martin@cwa.co.nz> contributed the ssh_cmd command, and
226 Jeff Finucane <jeff@cmh.net> updated it and took care of the 0.04 release.
227
228 Anthony Awtrey <tony@awtrey.com> contributed a fix for those still using
229 OpenSSH v1.
230
231 =head1 COPYRIGHT
232
233 Copyright (c) 2002 Ivan Kohler.
234 Copyright (c) 2002 Freeside Internet Services, LLC
235 All rights reserved.
236 This program is free software; you can redistribute it and/or modify it under
237 the same terms as Perl itself.
238
239 =head1 BUGS
240
241 Not OO.
242
243 Look at IPC::Session (also fsh)
244
245 =head1 SEE ALSO
246
247 For an all-perl implementation that does not require the system B<ssh> command,
248 see L<Net::SSH::Perl> instead.
249
250 ssh-keygen(1), ssh(1), L<IO::File>, L<IPC::Open2>, L<IPC::Open3>
251
252 =cut
253
254 1;
255