catch all the BatchMode s/ /=/
[Net-SSH.git] / SSH.pm
1 package Net::SSH;
2
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT_OK $ssh $DEBUG);
5 use Exporter;
6 use IPC::Open2;
7 use IPC::Open3;
8
9 @ISA = qw(Exporter);
10 @EXPORT_OK = qw( ssh issh sshopen2 sshopen3 );
11 $VERSION = '0.02';
12
13 $DEBUG = 0;
14
15 $ssh = "ssh";
16
17 =head1 NAME
18
19 Net::SSH - Perl extension for secure shell
20
21 =head1 SYNOPSIS
22
23   use Net::SSH qw(ssh issh sshopen2 sshopen3);
24
25   ssh('user@hostname', $command);
26
27   issh('user@hostname', $command);
28
29   sshopen2('user@hostname', $reader, $writer, $command);
30
31   sshopen3('user@hostname', $writer, $reader, $error, $command);
32
33 =head1 DESCRIPTION
34
35 Simple wrappers around ssh commands.
36
37 =head1 SUBROUTINES
38
39 =over 4
40
41 =item ssh [USER@]HOST, COMMAND [, ARGS ... ]
42
43 Calls ssh in batch mode.
44
45 =cut
46
47 sub ssh {
48   my($host, @command) = @_;
49   my @cmd = ($ssh, '-o', 'BatchMode=yes', $host, @command);
50   warn "[Net::SSH::ssh] executing ". join(' ', @cmd). "\n"
51     if $DEBUG;
52   system(@cmd);
53 }
54
55 =item issh [USER@]HOST, COMMAND [, ARGS ... ]
56
57 Prints the ssh command to be executed, waits for the user to confirm, and
58 (optionally) executes the command.
59
60 =cut
61
62 sub issh {
63   my($host, @command) = @_;
64   my @cmd = ($ssh, $host, @command);
65   print join(' ', @cmd), "\n";
66   if ( &_yesno ) {
67     system(@cmd);
68   }
69 }
70
71 =item sshopen2 [USER@]HOST, READER, WRITER, COMMAND [, ARGS ... ]
72
73 Connects the supplied filehandles to the ssh process (in batch mode).
74
75 =cut
76
77 sub sshopen2 {
78   my($host, $reader, $writer, @command) = @_;
79   open2($reader, $writer, $ssh, '-o', 'BatchMode=yes', $host, @command);
80 }
81
82 =item sshopen3 HOST, WRITER, READER, ERROR, COMMAND [, ARGS ... ]
83
84 Connects the supplied filehandles to the ssh process (in batch mode).
85
86 =cut
87
88 sub sshopen3 {
89   my($host, $writer, $reader, $error, @command) = @_;
90   open3($writer, $reader, $error, $ssh, '-o', 'BatchMode=yes', $host, @command);
91 }
92
93 sub _yesno {
94   print "Proceed [y/N]:";
95   my $x = scalar(<STDIN>);
96   $x =~ /^y/i;
97 }
98
99 =back
100
101 =head1 EXAMPLE
102
103   use Net::SSH qw(sshopen2);
104   use strict;
105
106   my $user = "username";
107   my $host = "hostname";
108   my $cmd = "command";
109
110   sshopen2("$user\@$host", *READER, *WRITER, "$cmd") || die "ssh: $!";
111
112   while (<READER>) {
113       chomp();
114       print "$_\n";
115   }
116
117   close(READER);
118   close(WRITER);
119
120 =head1 FREQUENTLY ASKED QUESTIONS
121
122 Q: How do you supply a password to connect with ssh within a perl script
123 using the Net::SSH module?
124
125 A: You don't.  Use RSA or DSA keys.  See the ssh-keygen(1) manpage.
126
127 Q: My script is "leaking" ssh processes.
128
129 A: See L<perlfaq8/"How do I avoid zombies on a Unix system">, L<IPC::Open2>,
130 L<IPC::Open3> and L<perlfunc/waitpid>.
131
132 =head1 AUTHOR
133
134 Ivan Kohler <ivan-netssh_pod@420.am>
135
136 =head1 CREDITS
137
138  John Harrison <japh@in-ta.net> contributed an example for the documentation.
139
140 =head1 COPYRIGHT
141
142 Copyright (c) 2000 Ivan Kohler.
143 Copyright (c) 2000 Silicon Interactive Software Design.
144 Copyright (c) 2000 Freeside Internet Services, LLC
145 All rights reserved.
146 This program is free software; you can redistribute it and/or modify it under
147 the same terms as Perl itself.
148
149 =head1 BUGS
150
151 Not OO.
152
153 Look at IPC::Session (also fsh)
154
155 =head1 SEE ALSO
156
157 ssh-keygen(1), ssh(1), L<IPC::Open2>, L<IPC::Open3>
158
159 =cut
160
161 1;
162