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