http://www.perlmonks.org?node_id=217983


in reply to tar from another server

The following works for me. (I just have to answer ssh's "password:" prompt on my local keyboard, 'cuz I don't have those nifty ssh keys for the hosts I connect to from home, but that's no problem.)

I decided to make a simple, general-purpose command line utility out of it, because I actually do this sort of transfer often enough myself to make it worthwhile (now I don't have to remember to put in all those parens, etc, on the command line).

update: added more error checking and support for the "-l username" ssh option; also allow for "$src_path" to be empty (i.e., in login user's home directory).

#!/usr/bin/perl # Program: ssh-tar.perl # Written by: dave graff # Purpose: make it easy to tar from remote-host to local-host use strict; my $Usage = "Usage: $0 hostname [-l username] path-to-tar local-dir-to +-save-to\n"; my $ssh_user = ""; if ( @ARGV > 2 && $ARGV[0] eq "-l" ) { $ssh_user = "$ARGV[0] $ARGV[1]"; shift; shift; } die $Usage unless ( @ARGV == 3 and -d $ARGV[2] and -w $ARGV[2] ); my $local_path = pop; chdir $local_path or die "can't chdir to $local_path: $!\n"; my $src_host = $ARGV[0]; my $divide = rindex( $ARGV[1], "/" ); my $src_path = ( $divide < 0 ) ? "" : substr( $ARGV[1], 0, $divide ); my $src_targ = substr( $ARGV[1], $divide +1 ); my $cmd = "ssh $ssh_user $src_host 'cd $src_path && tar cf - $src_targ +' | tar xf -"; print "command line is:\n\n $cmd\n\n"; system( $cmd );