File Coverage

blib/lib/IPC/ShellCmd/SSH.pm
Criterion Covered Total %
statement 9 30 30.0
branch 0 20 0.0
condition 0 18 0.0
subroutine 3 5 60.0
pod 1 2 50.0
total 13 75 17.3


line stmt bran cond sub pod time code
1             package IPC::ShellCmd::SSH;
2              
3 1     1   1117 use strict;
  1         2  
  1         38  
4 1     1   5 use Carp qw(croak);
  1         2  
  1         46  
5 1     1   4 use base qw(IPC::ShellCmd::ShBase);
  1         2  
  1         499  
6              
7             =head1 NAME
8              
9             IPC::ShellCmd::SSH - Chain ssh-ing to a host before running the command
10              
11             =head1 SYNOPSIS
12              
13             $cmd_obj->chain_prog(
14             IPC::ShellCmd::SSH->new(
15             User => 'cpanbuild',
16             Host => '10.0.0.1'
17             )
18             );
19              
20             =head1 DESCRIPTION
21              
22             =head2 IPC::ShellCmd::SSH->B(Host => I<$host>, [I<$opt> => I<$val>, ...])
23              
24             The only external method for this is the constructor. This sets up the
25             various arguments that are going to be used to generate the command-line.
26              
27             Other methods on this are used by L, but it should only ever be
28             used inside of the B method on a L object.
29              
30             The only required argument is the host.
31              
32             =over 4
33              
34             =item B I
35              
36             Specifies the host to ssh to. Since this is done by invoking the command-line
37             ssh client, this can be a short hostname that is part of the local ssh config.
38              
39             =item B
40              
41             Specifies the username on the remote host
42              
43             =item B
44              
45             Specifies the port to connect to on the remote host
46              
47             =item B
48              
49             If specified, then if true will enable agent forwarding (say for dealing with
50             a bastion host), and if false will explicitly disable it. If not specified it
51             will be the ssh default.
52              
53             =item B
54              
55             If specified, then if true will enable X11 forwarding, and if false will disable
56             it. If not specified, this will be the ssh default.
57              
58             =item B
59              
60             If specified, then if true will force allocation of a tty, and if false will
61             disable it. If not specified, this will be the ssh default.
62              
63             =item B
64              
65             Specifies the ssh private key to use.
66              
67             =back
68              
69             =cut
70              
71             sub new {
72 0     0 1   my $package = shift;
73 0           my %args = @_;
74              
75 0 0         croak "Must specify a Host argument"
76             unless defined $args{Host};
77              
78 0           my $self = bless { args => \%args }, $package;
79              
80 0           return $self;
81             }
82              
83             sub chain {
84 0     0 0   my $self = shift;
85 0           my $cmd = shift;
86 0           my $args = shift;
87              
88 0           my $cmd_string = $self->generate_sh_cmd($cmd, $args);
89              
90 0           my @ssh_args = ('ssh');
91              
92 0 0         push (@ssh_args, "-l", $self->{args}->{User})
93             if(defined $self->{args}->{User});
94              
95 0 0         push (@ssh_args, "-p", $self->{args}->{Port})
96             if(defined $self->{args}->{Port});
97              
98 0 0         push (@ssh_args, "-i", $self->{args}->{IdentityFile})
99             if(defined $self->{args}->{IdentityFile});
100              
101 0 0 0       push (@ssh_args, "-A")
102             if(defined $self->{args}->{ForwardAgent} && $self->{args}->{ForwardAgent});
103              
104 0 0 0       push (@ssh_args, "-a")
105             if(defined $self->{args}->{ForwardAgent} && !$self->{args}->{ForwardAgent});
106              
107 0 0 0       push (@ssh_args, "-X")
108             if(defined $self->{args}->{ForwardX11} && $self->{args}->{ForwardX11});
109              
110 0 0 0       push (@ssh_args, "-x")
111             if(defined $self->{args}->{ForwardX11} && !$self->{args}->{ForwardX11});
112              
113 0 0 0       push (@ssh_args, "-t")
114             if(defined $self->{args}->{AllocateTty} && $self->{args}->{AllocateTty});
115              
116 0 0 0       push (@ssh_args, "-T")
117             if(defined $self->{args}->{AllocateTty} && !$self->{args}->{AllocateTty});
118              
119 0           push (@ssh_args, $self->{args}->{Host}, $cmd_string);
120              
121 0           return @ssh_args;
122             }
123              
124             =head1 BUGS
125              
126             I don't know of any, but that doesn't mean they're not there.
127              
128             =head1 AUTHORS
129              
130             See L for authors.
131              
132             =head1 LICENSE
133              
134             See L for the license.
135              
136             =cut
137              
138             1;