715eec1498477cce6b4b96a99a6776a9a0e61732
[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', $reader, $writer, $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 AUTHOR
117
118 Ivan Kohler <ivan-netssh_pod@420.am>
119
120 =head1 CREDITS
121
122  John Harrison <japh@in-ta.net> contributed an example for the documentation.
123
124 =head1 BUGS
125
126 Not OO.
127
128 Look at IPC::Session (also fsh)
129
130 =head1 SEE ALSO
131
132 ssh(1), L<IPC::Open2>, L<IPC::Open3>
133
134 =cut
135
136 1;
137