initial checkin of module files for proper perl installation
[freeside.git] / FS / FS / SSH.pm
1 package FS::SSH;
2
3 use strict;
4 use vars qw(@ISA @EXPORT_OK $ssh $scp);
5 use Exporter;
6 use IPC::Open2;
7 use IPC::Open3;
8
9 @ISA = qw(Exporter);
10 @EXPORT_OK = qw(ssh scp issh iscp sshopen2 sshopen3);
11
12 $ssh="ssh";
13 $scp="scp";
14
15 =head1 NAME
16
17 FS::SSH - Subroutines to call ssh and scp
18
19 =head1 SYNOPSIS
20
21   use FS::SSH qw(ssh scp issh iscp sshopen2 sshopen3);
22
23   ssh($host, $command);
24
25   issh($host, $command);
26
27   scp($source, $destination);
28
29   iscp($source, $destination);
30
31   sshopen2($host, $reader, $writer, $command);
32
33   sshopen3($host, $reader, $writer, $error, $command);
34
35 =head1 DESCRIPTION
36
37   Simple wrappers around ssh and scp commands.
38
39 =head1 SUBROUTINES
40
41 =over 4
42
43 =item ssh HOST, COMMAND 
44
45 Calls ssh in batch mode.
46
47 =cut
48
49 sub ssh {
50   my($host,$command)=@_;
51   my(@cmd)=($ssh, "-o", "BatchMode yes", $host, $command);
52 #       print join(' ',@cmd),"\n";
53 #0;
54   system(@cmd);
55 }
56
57 =item issh HOST, COMMAND
58
59 Prints the ssh command to be executed, waits for the user to confirm, and
60 (optionally) executes the command.
61
62 =cut
63
64 sub issh {
65   my($host,$command)=@_;
66   my(@cmd)=($ssh, $host, $command);
67   print join(' ',@cmd),"\n";
68   if ( &_yesno ) {
69         ###print join(' ',@cmd),"\n";
70     system(@cmd);
71   }
72 }
73
74 =item scp SOURCE, DESTINATION
75
76 Calls scp in batch mode.
77
78 =cut
79
80 sub scp {
81   my($src,$dest)=@_;
82   my(@cmd)=($scp,"-Bprq",$src,$dest);
83 #       print join(' ',@cmd),"\n";
84 #0;
85   system(@cmd);
86 }
87
88 =item iscp SOURCE, DESTINATION
89
90 Prints the scp command to be executed, waits for the user to confirm, and
91 (optionally) executes the command.
92
93 =cut
94
95 sub iscp {
96   my($src,$dest)=@_;
97   my(@cmd)=($scp,"-pr",$src,$dest);
98   print join(' ',@cmd),"\n";
99   if ( &_yesno ) {
100         ###print join(' ',@cmd),"\n";
101     system(@cmd);
102   }
103 }
104
105 =item sshopen2 HOST, READER, WRITER, COMMAND
106
107 Connects the supplied filehandles to the ssh process (in batch mode).
108
109 =cut
110
111 sub sshopen2 {
112   my($host,$reader,$writer,$command)=@_;
113   open2($reader,$writer,$ssh,'-o','Batchmode yes',$host,$command);
114 }
115
116 =item sshopen3 HOST, WRITER, READER, ERROR, COMMAND
117
118 Connects the supplied filehandles to the ssh process (in batch mode).
119
120 =cut
121
122 sub sshopen3 {
123   my($host,$writer,$reader,$error,$command)=@_;
124   open3($writer,$reader,$error,$ssh,'-o','Batchmode yes',$host,$command);
125 }
126
127 sub _yesno {
128   print "Proceed [y/N]:";
129   my($x)=scalar(<STDIN>);
130   $x =~ /^y/i;
131 }
132
133 =head1 BUGS
134
135 Not OO.
136
137 scp stuff should transparantly use rsync-over-ssh instead.
138
139 =head1 SEE ALSO
140
141 L<ssh>, L<scp>, L<IPC::Open2>, L<IPC::Open3>
142
143 =cut
144
145 1;
146