4 use vars qw($VERSION @ISA @EXPORT_OK $ssh $DEBUG);
11 @EXPORT_OK = qw( ssh issh ssh_cmd sshopen2 sshopen3 );
20 Net::SSH - Perl extension for secure shell
24 use Net::SSH qw(ssh issh sshopen2 sshopen3);
26 ssh('user@hostname', $command);
28 issh('user@hostname', $command);
30 ssh_cmd('user@hostname', $command);
32 sshopen2('user@hostname', $reader, $writer, $command);
34 sshopen3('user@hostname', $writer, $reader, $error, $command);
38 Simple wrappers around ssh commands. For an all-perl implementation that does
40 the system `ssh' command, see Net::SSH::Perl.
47 =item ssh [USER@]HOST, COMMAND [, ARGS ... ]
49 Calls ssh in batch mode.
54 my($host, @command) = @_;
55 my @cmd = ($ssh, '-o', 'BatchMode=yes', $host, @command);
56 warn "[Net::SSH::ssh] executing ". join(' ', @cmd). "\n"
61 =item issh [USER@]HOST, COMMAND [, ARGS ... ]
63 Prints the ssh command to be executed, waits for the user to confirm, and
64 (optionally) executes the command.
69 my($host, @command) = @_;
70 my @cmd = ($ssh, $host, @command);
71 print join(' ', @cmd), "\n";
77 =item ssh_cmd [USER@]HOST, COMMAND [, ARGS ... ]
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.
85 my($host, @command) = @_;
87 my $reader = IO::File->new();
88 my $writer = IO::File->new();
89 my $error = IO::File->new();
91 sshopen3( $host, $reader, $writer, $error, @command ) or die $!;
94 my $output_stream = <$writer>;
95 my $error_stream = <$error>;
97 if ( length $error_stream ) {
98 die "[Net:SSH::ssh_cmd] STDERR $error_stream";
101 return $output_stream;
105 =item sshopen2 [USER@]HOST, READER, WRITER, COMMAND [, ARGS ... ]
107 Connects the supplied filehandles to the ssh process (in batch mode).
112 my($host, $reader, $writer, @command) = @_;
113 open2($reader, $writer, $ssh, '-o', 'BatchMode=yes', $host, @command);
116 =item sshopen3 HOST, WRITER, READER, ERROR, COMMAND [, ARGS ... ]
118 Connects the supplied filehandles to the ssh process (in batch mode).
123 my($host, $writer, $reader, $error, @command) = @_;
124 open3($writer, $reader, $error, $ssh, '-o', 'BatchMode=yes', $host, @command);
128 print "Proceed [y/N]:";
129 my $x = scalar(<STDIN>);
137 use Net::SSH qw(sshopen2);
140 my $user = "username";
141 my $host = "hostname";
144 sshopen2("$user\@$host", *READER, *WRITER, "$cmd") || die "ssh: $!";
154 =head1 FREQUENTLY ASKED QUESTIONS
156 Q: How do you supply a password to connect with ssh within a perl script
157 using the Net::SSH module?
159 A: You don't. Use RSA or DSA keys. See the ssh-keygen(1) manpage.
161 Q: My script is "leaking" ssh processes.
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>.
168 Ivan Kohler <ivan-netssh_pod@420.am>
170 John Harrison <japh@in-ta.net> contributed an example for the documentation.
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.
177 Copyright (c) 2002 Ivan Kohler.
178 Copyright (c) 2002 Freeside Internet Services, LLC
180 This program is free software; you can redistribute it and/or modify it under
181 the same terms as Perl itself.
187 Look at IPC::Session (also fsh)
191 For an all-perl implementation that does not require the system B<ssh> command,
192 see L<Net::SSH::Perl>.
194 ssh-keygen(1), ssh(1), L<IO::File>, L<IPC::Open2>, L<IPC::Open3>