4 use vars qw($VERSION @ISA @EXPORT_OK $ssh $equalspace $DEBUG @ssh_options);
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 @ssh_options = &_ssh_options unless @ssh_options;
63 my @cmd = ($ssh, @ssh_options, $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 $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 @ssh_options = &_ssh_options unless @ssh_options;
146 open2($reader, $writer, $ssh, @ssh_options, $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 @ssh_options = &_ssh_options unless @ssh_options;
158 open3($writer, $reader, $error, $ssh, @ssh_options, $host, @command);
162 print "Proceed [y/N]:";
163 my $x = scalar(<STDIN>);
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 if ( $ssh_version =~ /.*OpenSSH[-|_](\w+)\./ && $1 == 1 ) {
179 my @options = ( '-o', 'BatchMode'.$equalspace.'yes' )
180 if ( $ssh_version =~ /.*OpenSSH[-|_](\w+)\./ && $1 > 1 ) {
181 unshift @options, '-T';
190 use Net::SSH qw(sshopen2);
193 my $user = "username";
194 my $host = "hostname";
197 sshopen2("$user\@$host", *READER, *WRITER, "$cmd") || die "ssh: $!";
207 =head1 FREQUENTLY ASKED QUESTIONS
209 Q: How do you supply a password to connect with ssh within a perl script
210 using the Net::SSH module?
212 A: You don't. Use RSA or DSA keys. See the ssh-keygen(1) manpage.
214 Q: My script is "leaking" ssh processes.
216 A: See L<perlfaq8/"How do I avoid zombies on a Unix system">, L<IPC::Open2>,
217 L<IPC::Open3> and L<perlfunc/waitpid>.
221 Ivan Kohler <ivan-netssh_pod@420.am>
223 John Harrison <japh@in-ta.net> contributed an example for the documentation.
225 Martin Langhoff <martin@cwa.co.nz> contributed the ssh_cmd command, and
226 Jeff Finucane <jeff@cmh.net> updated it and took care of the 0.04 release.
228 Anthony Awtrey <tony@awtrey.com> contributed a fix for those still using
233 Copyright (c) 2002 Ivan Kohler.
234 Copyright (c) 2002 Freeside Internet Services, LLC
236 This program is free software; you can redistribute it and/or modify it under
237 the same terms as Perl itself.
243 Look at IPC::Session (also fsh)
247 For an all-perl implementation that does not require the system B<ssh> command,
248 see L<Net::SSH::Perl> instead.
250 ssh-keygen(1), ssh(1), L<IO::File>, L<IPC::Open2>, L<IPC::Open3>