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