# $Id: sshattach.pm,v 1.2 2005/02/28 06:48:52 joostvb Exp $ # $Source: /var/cvs/ad1810.com/packages/ad1810-ssh/contrib/sshattach.pm,v $ # # Author: Rudi Sluijtman, 2001 # License: GPL # # Date: Fri, 13 Apr 2001 23:38:00 +0200 # Message-Id: <200104132138.f3DLc0x23334@nerys.ehv.lx> # From: Rudi Sluijtman # To: joostvb@xxxxxxxxx # Subject: ssh-attach # package sshattach ; sub new { my $package = shift ; my $params=shift ; my $self ; $self->{'ssh'}="ssh"; $self->{'scp'}="scp" ; $self->{'rsync'}="rsync" ; if ( defined $params->{'port'} ) { $self->{'ssh'} .= " -p $params->{'port'}" ; $self->{'scp'} .= " -P $params->{'port'}" ; } bless $self,$package ; my @dirs=; for ( @dirs ) { if ( -d ) { my @sock=<$_/*> ; for my $s ( @sock ) { if ( -S $s ) { system( "SSH_AUTH_SOCK=$s ssh-add -l >/dev/null 2>/dev/null" ) ; if ( $? == 0 ) { $ENV{'SSH_AUTH_SOCK'}=$s ; $ENV{'RSYNC_RSH'}=$self->{'ssh'} ; return $self ; } } } } } die "sshattach init failed!\n"; } sub sh($$) { my $self = shift ; my $cmd=shift ; my @out ; open SSH, "$self->{'ssh'} $cmd |" or die "no $self->{'ssh'} $cmd : $!\n" ; while ( ) { chop; push @out, $_ ; } close SSH ; return @out ; } sub cp($$) { my $self = shift ; my $cmd = shift ; system "$self->{'scp'} $cmd" ; } sub rsync($$) { my $self=shift ; my $cmd=shift; my @out ; open SSH, "$self->{'rsync'} $cmd |" or die "no $self->{'rsync'} $cmd: $!\n"; while ( ) { chop; push @out, $_ ; } close SSH ; return @out ; } 1;