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