source: ProjectBuilder/projects/casparbuster/devel/lib/CasparBuster/SSH.pm@ 1667

Last change on this file since 1667 was 1667, checked in by Bruno Cornec, 12 years ago
  • Simplify Makefile by removing .pm modules in it
  • Adds a new SSH.pm module to share between cb and cbusterize the Net:SSH2 initialization and closing phases
  • cb now uses SSH.pm
File size: 2.4 KB
Line 
1#
2# Manages SSH communications for cb
3#
4# $Id$
5#
6#
7package CasparBuster::SSH;
8
9use strict;
10use Carp;
11use ProjectBuilder::Base;
12
13# Global vars
14# Inherit from the "Exporter" module which handles exporting functions.
15#
16use vars qw($VERSION $REVISION @ISA @EXPORT);
17use Exporter;
18#
19# Export, by default, all the functions into the namespace of
20# any code which uses this module.
21
22our @ISA = qw(Exporter);
23our @EXPORT = qw(cb_ssh_init cb_ssh_close);
24
25=pod
26
27=head1 NAME
28
29CasparBuster::SSH - module dealing with SSH for CasparBuster
30
31=head1 DESCRIPTION
32
33This modules provides functions to allow SSH communications in the CasparBuster environment
34
35=head1 SYNOPSIS
36
37 use CasparBuster::SSH;
38 my $ssh = cb_ssh_init($remote,$machine);
39 cb_ssh_close($ssh);
40
41=head1 USAGE
42
43=over 4
44
45=item B<cb_ssh_init>
46
47This function returns the SSH handle from Net::SSH2 after having created the object + authentification
48
49The first parameter is the remote account name
50The second parameter is the remote machine to connect to
51The third parameter is the debug level
52
53=cut
54
55sub cb_ssh_init {
56
57my $remote = shift;
58my $machine = shift;
59my $debug = shift || 0;
60
61pb_log(1,"DEBUG: First time so we need to create the SSH::Net2 object\n");
62my $ssh2 = Net::SSH2->new();
63pb_log(3,"DEBUG: New SSH2 object created\n");
64$ssh2->debug(1) if ($debug > 2);
65pb_log(3,"DEBUG: SSH2 debug mode on\n");
66
67$ssh2->connect($machine) || confess "Unable to connect to $remote\@$machine: $!";
68pb_log(3,"DEBUG: SSH2 connection done\n");
69
70my $hdir = (getpwnam(getpwuid($<)))[7];
71confess "Unable to connect to $remote\@$machine: $!" if (not $ssh2->auth_publickey($remote,"$hdir/.ssh/id_dsa.pub","$hdir/.ssh/id_dsa"));
72pb_log(3,"DEBUG: SSH2 auth done\n");
73
74my $chan = $ssh2->channel();
75confess "Unable to create channel for $remote\@$machine: $!" if (not defined $chan);
76pb_log(3,"DEBUG: SSH2 chan called\n");
77
78$chan->exec("mkdir -p $ENV{'PBTMP'}");
79pb_log(3,"DEBUG: Created $ENV{'PBTMP'} on $remote\@$machine\n");
80$chan->close;
81
82return($ssh2);
83}
84
85=over 4
86
87=item B<cb_ssh_close>
88
89This function closes properly the SSH comunication and remove the temp dir created remotely
90The first parameter is the Net::SSH2 handle returned by cb_ssh_init
91
92=cut
93
94
95sub cb_ssh_close {
96
97my $ssh2 = shift;
98
99my $chan = $ssh2->channel();
100confess "Unable to create channel $!" if (not defined $chan);
101pb_log(3,"DEBUG: SSH2 chan called\n");
102
103$chan->exec("rm -rf $ENV{'PBTMP'}");
104pb_log(3,"DEBUG: Removing remotely $ENV{'PBTMP'}\n");
105$chan->close();
106
107$ssh2->disconnect();
108}
109
1101;
Note: See TracBrowser for help on using the repository browser.