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.
44 =item ssh [USER@]HOST, COMMAND [, ARGS ... ]
46 Calls ssh in batch mode.
51 my($host, @command) = @_;
52 my @cmd = ($ssh, '-o', 'BatchMode=yes', $host, @command);
53 warn "[Net::SSH::ssh] executing ". join(' ', @cmd). "\n"
58 =item issh [USER@]HOST, COMMAND [, ARGS ... ]
60 Prints the ssh command to be executed, waits for the user to confirm, and
61 (optionally) executes the command.
66 my($host, @command) = @_;
67 my @cmd = ($ssh, $host, @command);
68 print join(' ', @cmd), "\n";
74 =item ssh_cmd [USER@]HOST, COMMAND [, ARGS ... ]
76 Calls ssh in batch mode. Dies if data occurs on the error stream. Warns
77 of data on the output stream.
82 my($host, @command) = @_;
84 my $reader = IO::File->new();
85 my $writer = IO::File->new();
86 my $error = IO::File->new();
88 sshopen3( $host, $reader, $writer, $error, @command ) or die $!;
91 my $output_stream = <$writer>;
92 my $error_stream = <$error>;
93 if ( length $error_stream ) {
94 die "[Net:SSH::ssh_cmd] STDERR $error_stream";
96 if ( length $output_stream ) {
97 warn "[Net::SSH::ssh_cmd] STDOUT $output_stream";
102 =item sshopen2 [USER@]HOST, READER, WRITER, COMMAND [, ARGS ... ]
104 Connects the supplied filehandles to the ssh process (in batch mode).
109 my($host, $reader, $writer, @command) = @_;
110 open2($reader, $writer, $ssh, '-o', 'BatchMode=yes', $host, @command);
113 =item sshopen3 HOST, WRITER, READER, ERROR, COMMAND [, ARGS ... ]
115 Connects the supplied filehandles to the ssh process (in batch mode).
120 my($host, $writer, $reader, $error, @command) = @_;
121 open3($writer, $reader, $error, $ssh, '-o', 'BatchMode=yes', $host, @command);
125 print "Proceed [y/N]:";
126 my $x = scalar(<STDIN>);
134 use Net::SSH qw(sshopen2);
137 my $user = "username";
138 my $host = "hostname";
141 sshopen2("$user\@$host", *READER, *WRITER, "$cmd") || die "ssh: $!";
151 =head1 FREQUENTLY ASKED QUESTIONS
153 Q: How do you supply a password to connect with ssh within a perl script
154 using the Net::SSH module?
156 A: You don't. Use RSA or DSA keys. See the ssh-keygen(1) manpage.
158 Q: My script is "leaking" ssh processes.
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>.
165 Ivan Kohler <ivan-netssh_pod@420.am>
169 John Harrison <japh@in-ta.net> contributed an example for the documentation.
173 Copyright (c) 2000 Ivan Kohler.
174 Copyright (c) 2000 Silicon Interactive Software Design.
175 Copyright (c) 2000 Freeside Internet Services, LLC
177 This program is free software; you can redistribute it and/or modify it under
178 the same terms as Perl itself.
184 Look at IPC::Session (also fsh)
188 ssh-keygen(1), ssh(1), L<IO::File>, L<IPC::Open2>, L<IPC::Open3>