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