9 package Net::SSH::Perl::ProxiedIPC;
10 use strict; use warnings;
11 use Net::SSH::Perl::WithSocks;
16 my $class = ref $proto || $proto;
17 bless( { @_ }, $class );
21 $_[0]->{ssh} ||= $_[0]->_build_ssh
25 Net::SSH::Perl::WithSocks->new();
29 $_[0]->{ssh_env_vars} ||= $_[0]->_build_ssh_env_vars;
32 sub _build_ssh_env_vars {
34 # this needs work I think. First off, it won't work.
35 # +{ $_[0]->_firsthop_perlssh->eval(; 'chomp(my @env = `ssh-agent`); my %new_env; foreach (@env) { /^(.*?)=(.*)/; $ENV{$1} =$new_env{$1}=$2; } return %new_env;' ); }
39 my( $self, @hosts ) = @_;
40 my $ssh = $self->_ssh;
42 my $env_str = $self->_ssh_env_vars;
43 my $command = join ' ', (map "ssh -o StrictHostKeyChecking=no -A $_", @hosts), "perl";
44 $command = "sh -c '$env_str$command'";
45 my( $read, $write ) = $ssh->open2($command);
47 my $readfunc = sub { sysread( $read, $_[0], $_[1] ) };
48 my $writefunc = sub { syswrite( $write, $_[0] ) };
50 ($command, IPC::PerlSSH->new( Readfunc => $readfunc, Writefunc => $writefunc ));
56 my $ssh = Net::SSH::Perl->new('localhost');
57 $ssh->login('test', 'test');
59 my $pipc = Net::SSH::Perl::ProxiedIPC->new(ssh => $ssh);
61 my ($cmd, $perlssh) = $pipc->_open_perlssh;
63 is( ref $perlssh, "IPC::PerlSSH", "\$perlssh isa IPC::PerlSSH (via $cmd)" );
65 $perlssh->eval( "use POSIX qw(uname)" );
66 my @remote_uname = $perlssh->eval( "uname()" );
68 ## This is a really shitty idea for a test but fuck you.
69 is( $remote_uname[1], "minerva", 'localhost uname() returns minerva' );
71 my $homedir = $perlssh->eval( '$ENV{HOME}' );
72 fail( "we require a little sensibility in our \$ENV thank you." )
73 unless defined $homedir;
75 $perlssh->eval( "use File::HomeDir" );
76 my $homedir2 = $perlssh->eval( 'File::HomeDir->my_home' );
77 is( $homedir2, "/home/test", 'got $ENV{HOME} the smart way' );
79 my $new_env = $perlssh->eval( 'chomp(my @env = `ssh-agent`); my %new_env; foreach (@env) { /^(.*?)=([^;]+)/ or next; $ENV{$1} =$new_env{$1}=$2; } my $output; $output .= "$_=$new_env{$_} " foreach ( keys %new_env ); $output;' );
81 $pipc->{ssh_env_vars} = $new_env;
83 my @test_hosts = ( 'stagetwo@localhost', 'stagethree@localhost' );
84 my ($cmd2, $pssh2) = $pipc->_open_perlssh(@test_hosts);
85 is( ref $pssh2, "IPC::PerlSSH", "\$pssh2 isa IPC::PerlSSH (via $cmd2)" );
87 $pssh2->eval( "use POSIX qw(uname)" );
88 @remote_uname = $pssh2->eval( "uname()" );
89 is( $remote_uname[1], "minerva", 'uname() returns minerva three jumps into localhost!' );