| 1 | # | ||
| 2 | # This file is part of StorageDisplay | ||
| 3 | # | ||
| 4 | # This software is copyright (c) 2014-2023 by Vincent Danjean. | ||
| 5 | # | ||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||
| 8 | # | ||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use 5.14.0; | ||
| 12 | |||
| 13 | # Implementation note: unused module for now | ||
| 14 | |||
| 15 | package StorageDisplay::Collect::CMD::Remote; | ||
| 16 | # ABSTRACT: Collect data on remote machine with SSH | ||
| 17 | our $VERSION = '2.06'; # VERSION | ||
| 18 | |||
| 19 | |||
| 20 | use StorageDisplay::Collect; | ||
| 21 | use Net::OpenSSH; | ||
| 22 | use Term::ReadKey; | ||
| 23 | END { | ||
| 24 | ReadMode('normal'); | ||
| 25 | } | ||
| 26 | use Moose; | ||
| 27 | use MooseX::NonMoose; | ||
| 28 | extends 'StorageDisplay::Collect::CMD'; | ||
| 29 | |||
| 30 | has 'ssh' => ( | ||
| 31 | is => 'ro', | ||
| 32 | isa => 'Net::OpenSSH', | ||
| 33 | required => 1, | ||
| 34 | ); | ||
| 35 | |||
| 36 | |||
| 37 | sub open_cmd_pipe { | ||
| 38 | my $self = shift; | ||
| 39 | my $ssh = $self->ssh; | ||
| 40 | my @cmd = @_; | ||
| 41 | print STDERR "[SSH]Running: ", join(' ', @cmd), "\n"; | ||
| 42 | my ($dh, $pid) = $ssh->pipe_out(@cmd) or | ||
| 43 | die "pipe_out method failed: " . $ssh->error." for '".join("' '", @cmd)."'\n"; | ||
| 44 | return $dh; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub open_cmd_pipe_root { | ||
| 48 | my $self = shift; | ||
| 49 | my @cmd = (qw(sudo -S -p), 'sudo password:'."\n", '--', @_); | ||
| 50 | ReadMode('noecho'); | ||
| 51 | my $dh = $self->open_cmd_pipe(@cmd); | ||
| 52 | my $c = ord($dh->getc); | ||
| 53 | $dh->ungetc($c); | ||
| 54 | ReadMode('normal'); | ||
| 55 | return $dh; | ||
| 56 | } | ||
| 57 | |||
| 58 | around BUILDARGS => sub { | ||
| 59 | my $orig = shift; | ||
| 60 | my $class = shift; | ||
| 61 | my $remote = shift; | ||
| 62 | |||
| 63 | my $ssh = Net::OpenSSH->new($remote); | ||
| 64 | $ssh->error and | ||
| 65 | die "Couldn't establish SSH connection: ". $ssh->error; | ||
| 66 | |||
| 67 | return $class->$orig( | ||
| 68 | 'ssh' => $ssh, | ||
| 69 | ); | ||
| 70 | }; | ||
| 71 | |||
| 72 | 1; | ||
| 73 | |||
| 74 | __END__ |