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