4 use vars qw($VERSION @ISA @EXPORT_OK $ssh $equalspace $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);
35 args => [ '-arg1', '-arg2' ],
36 stdin_string => "string\n",
39 sshopen2('user@hostname', $reader, $writer, $command);
41 sshopen3('user@hostname', $writer, $reader, $error, $command);
45 Simple wrappers around ssh commands.
47 For an all-perl implementation that does not require the system B<ssh> command,
48 see L<Net::SSH::Perl> instead.
54 =item ssh [USER@]HOST, COMMAND [, ARGS ... ]
56 Calls ssh in batch mode.
61 my($host, @command) = @_;
62 &_check_ssh_version unless defined $equalspace;
63 my @cmd = ($ssh, '-o', 'BatchMode'.$equalspace.'yes', $host, @command);
64 warn "[Net::SSH::ssh] executing ". join(' ', @cmd). "\n"
69 =item issh [USER@]HOST, COMMAND [, ARGS ... ]
71 Prints the ssh command to be executed, waits for the user to confirm, and
72 (optionally) executes the command.
77 my($host, @command) = @_;
78 my @cmd = ($ssh, $host, @command);
79 print join(' ', @cmd), "\n";
85 =item ssh_cmd [USER@]HOST, COMMAND [, ARGS ... ]
87 =item ssh_cmd OPTIONS_HASHREF
89 Calls ssh in batch mode. Throws a fatal error if data occurs on the command's
90 STDERR. Returns any data from the command's STDOUT.
92 If using the hashref-style of passing arguments, possible keys are:
97 args (optional, arrayref)
98 stdin_string (optional) - written to the command's STDIN
103 my($host, $stdin_string, @command);
106 $host = $opt->{host};
107 $host = $opt->{user}. '@'. $host if exists $opt->{user};
108 @command = ( $opt->{command} );
109 push @command, @{ $opt->{args} } if exists $opt->{args};
110 my $stdin_string = $opt->{stdin_string};
112 ($host, @command) = @_;
116 my $reader = IO::File->new();
117 my $writer = IO::File->new();
118 my $error = IO::File->new();
120 sshopen3( $host, $writer, $reader, $error, @command ) or die $!;
122 print $writer $stdin_string if defined $stdin_string;
126 my $output_stream = <$reader>;
127 my $error_stream = <$error>;
129 if ( length $error_stream ) {
130 die "[Net:SSH::ssh_cmd] STDERR $error_stream";
133 return $output_stream;
137 =item sshopen2 [USER@]HOST, READER, WRITER, COMMAND [, ARGS ... ]
139 Connects the supplied filehandles to the ssh process (in batch mode).
144 my($host, $reader, $writer, @command) = @_;
145 &_check_ssh_version unless defined $equalspace;
146 open2($reader, $writer, $ssh, '-o', 'BatchMode'.$equalspace.'yes', $host, @command);
149 =item sshopen3 HOST, WRITER, READER, ERROR, COMMAND [, ARGS ... ]
151 Connects the supplied filehandles to the ssh process (in batch mode).
156 my($host, $writer, $reader, $error, @command) = @_;
157 &_check_ssh_version unless defined $equalspace;
158 open3($writer, $reader, $error, $ssh, '-o', 'BatchMode'.$equalspace.'yes', $host, @command);
162 print "Proceed [y/N]:";
163 my $x = scalar(<STDIN>);
167 sub _check_ssh_version {
168 my $reader = IO::File->new();
169 my $writer = IO::File->new();
170 my $error = IO::File->new();
171 open3($writer, $reader, $error, $ssh, '-V');
172 my $ssh_version = <$error>;
174 $ssh_version =~ s/.*OpenSSH[-|_](\w+?)\.\w+?\.\w+?.*/$1/g;
175 if ($ssh_version == 1) {
186 use Net::SSH qw(sshopen2);
189 my $user = "username";
190 my $host = "hostname";
193 sshopen2("$user\@$host", *READER, *WRITER, "$cmd") || die "ssh: $!";
203 =head1 FREQUENTLY ASKED QUESTIONS
205 Q: How do you supply a password to connect with ssh within a perl script
206 using the Net::SSH module?
208 A: You don't. Use RSA or DSA keys. See the ssh-keygen(1) manpage.
210 Q: My script is "leaking" ssh processes.
212 A: See L<perlfaq8/"How do I avoid zombies on a Unix system">, L<IPC::Open2>,
213 L<IPC::Open3> and L<perlfunc/waitpid>.
217 Ivan Kohler <ivan-netssh_pod@420.am>
219 John Harrison <japh@in-ta.net> contributed an example for the documentation.
221 Martin Langhoff <martin@cwa.co.nz> contributed the ssh_cmd command, and
222 Jeff Finucane <jeff@cmh.net> updated it and took care of the 0.04 release.
224 Anthony Awtrey <tony@awtrey.com> contributed a fix for those still using
229 Copyright (c) 2002 Ivan Kohler.
230 Copyright (c) 2002 Freeside Internet Services, LLC
232 This program is free software; you can redistribute it and/or modify it under
233 the same terms as Perl itself.
239 Look at IPC::Session (also fsh)
243 For an all-perl implementation that does not require the system B<ssh> command,
244 see L<Net::SSH::Perl> instead.
246 ssh-keygen(1), ssh(1), L<IO::File>, L<IPC::Open2>, L<IPC::Open3>