4 use vars qw($VERSION @ISA @EXPORT_OK $ssh $equalspace $DEBUG);
11 @EXPORT_OK = qw( ssh issh ssh_cmd sshopen2 sshopen3 );
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>;
24 $ssh_version =~ s/.*OpenSSH[-|_](\w+?)\.\w+?\.\w+?.*/$1/g;
25 if ($ssh_version == 1) {
33 Net::SSH - Perl extension for secure shell
37 use Net::SSH qw(ssh issh sshopen2 sshopen3);
39 ssh('user@hostname', $command);
41 issh('user@hostname', $command);
43 ssh_cmd('user@hostname', $command);
45 sshopen2('user@hostname', $reader, $writer, $command);
47 sshopen3('user@hostname', $writer, $reader, $error, $command);
51 Simple wrappers around ssh commands.
53 For an all-perl implementation that does not require the system B<ssh> command,
54 see L<Net::SSH::Perl> instead.
60 =item ssh [USER@]HOST, COMMAND [, ARGS ... ]
62 Calls ssh in batch mode.
67 my($host, @command) = @_;
68 my @cmd = ($ssh, '-o', 'BatchMode'.$equalspace.'yes', $host, @command);
69 warn "[Net::SSH::ssh] executing ". join(' ', @cmd). "\n"
74 =item issh [USER@]HOST, COMMAND [, ARGS ... ]
76 Prints the ssh command to be executed, waits for the user to confirm, and
77 (optionally) executes the command.
82 my($host, @command) = @_;
83 my @cmd = ($ssh, $host, @command);
84 print join(' ', @cmd), "\n";
90 =item ssh_cmd [USER@]HOST, COMMAND [, ARGS ... ]
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.
98 my($host, @command) = @_;
100 my $reader = IO::File->new();
101 my $writer = IO::File->new();
102 my $error = IO::File->new();
104 sshopen3( $host, $reader, $writer, $error, @command ) or die $!;
107 my $output_stream = <$writer>;
108 my $error_stream = <$error>;
110 if ( length $error_stream ) {
111 die "[Net:SSH::ssh_cmd] STDERR $error_stream";
114 return $output_stream;
118 =item sshopen2 [USER@]HOST, READER, WRITER, COMMAND [, ARGS ... ]
120 Connects the supplied filehandles to the ssh process (in batch mode).
125 my($host, $reader, $writer, @command) = @_;
126 open2($reader, $writer, $ssh, '-o', 'BatchMode'.$equalspace.'yes', $host, @command);
129 =item sshopen3 HOST, WRITER, READER, ERROR, COMMAND [, ARGS ... ]
131 Connects the supplied filehandles to the ssh process (in batch mode).
136 my($host, $writer, $reader, $error, @command) = @_;
137 open3($writer, $reader, $error, $ssh, '-o', 'BatchMode'.$equalspace.'yes', $host, @command);
141 print "Proceed [y/N]:";
142 my $x = scalar(<STDIN>);
150 use Net::SSH qw(sshopen2);
153 my $user = "username";
154 my $host = "hostname";
157 sshopen2("$user\@$host", *READER, *WRITER, "$cmd") || die "ssh: $!";
167 =head1 FREQUENTLY ASKED QUESTIONS
169 Q: How do you supply a password to connect with ssh within a perl script
170 using the Net::SSH module?
172 A: You don't. Use RSA or DSA keys. See the ssh-keygen(1) manpage.
174 Q: My script is "leaking" ssh processes.
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>.
181 Ivan Kohler <ivan-netssh_pod@420.am>
183 John Harrison <japh@in-ta.net> contributed an example for the documentation.
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.
188 Anthony Awtrey <tony@awtrey.com> contributed a fix for those still using
193 Copyright (c) 2002 Ivan Kohler.
194 Copyright (c) 2002 Freeside Internet Services, LLC
196 This program is free software; you can redistribute it and/or modify it under
197 the same terms as Perl itself.
203 Look at IPC::Session (also fsh)
207 For an all-perl implementation that does not require the system B<ssh> command,
208 see L<Net::SSH::Perl> instead.
210 ssh-keygen(1), ssh(1), L<IO::File>, L<IPC::Open2>, L<IPC::Open3>