neversaint has asked for the wisdom of the Perl Monks concerning the following question:

Dear Masters,
I have the following problem. Here I want to get/put files from my PC at home into "host2" at any specified path. The problem is "host2" can only be accessed via "host1".
__________ _________ | | | | My PC ------->| host1 |----------->| host2 | |________| |_______| user: myname usr: myname pswd: foo pswd: bar
What I usually do manually is to access "host1" via console's SSH, then do sftp from inside "host1" to "host2".

Is there a way to do it automatically with Net::SFTP? Cause the following code I have only allowed me to access "host1".

use Net::SFTP; use Net::SSH::Perl; my $host1 = 'host1'; my $pass1 = 'foo'; # password for host1 my $path1 = '/somepath'; # not really necessary my $host2 = 'host2'; my $pass2 = 'bar'; # password for host1 my $path2 = '/somepath2'; # the actual path which I want to access my $user = 'myname'; # username for host1 and host2 are the same my $home = 'mycode.pl'; my $sftp = Net::SFTP->new($host1, "user" => $user, "password" => $pass1, "debug"=>1) || die 'cannot login $!\n'; $sftp->put($home,$path1)|| die "cannot open: $!";


---
neversaint and everlastingly indebted.......

Replies are listed 'Best First'.
Re: Howto Access Two Consecutive Hosts with Net::SFTP
by salva (Canon) on Nov 14, 2005 at 13:00 UTC
    if you can use keys for authentication instead of user/password pairs, then try Net::SFTP::Foreign:
    use Net::SFTP::Foreign; my $sftp = Net::SFTP::Foreign->new( open2_cmd => ['ssh', '-l', $proxy_user, $proxy_host, 'ssh', '-l', $user, $host, '-s', 'sftp'] ) || die 'cannot login $!\n'; $sftp->put($home,$path1) || die "cannot open: $!";
Re: Howto Access Two Consecutive Hosts with Net::SFTP
by davidrw (Prior) on Nov 14, 2005 at 13:33 UTC
    A more robust solution might be to set up port-fowarding on host1 (e.g. on port 8022 -> host2's port 22) so that you can scp "directly" to host2. If this isn't an option, then you can accomplish the same thing with a SSH tunnel -- you can set this up with Net::SSH::Perl and then use Net::SFTP on localhost:8022 to get directly to host2 port 22.
Re: Howto Access Two Consecutive Hosts with Net::SFTP
by Moron (Curate) on Nov 14, 2005 at 13:00 UTC
    It depends on what "automatically" means, but interpreting it one way, you could have a submission program on your home machine (which envelops the files with a header and trailer) and a forwarding daemon on host1 (which recognises that the trailer marks complete inward transmission of the file, strips the hdr/trlr off again and then sftp's it onward), using perhaps Net::Daemon to control the forwarding daemon from your PC.

    (Update: Alternatively, could send the file more directly to the daemon with Net::Daemon::SSL, but that seems actually trickier for a forwarding daemon. In the first solution above, the main purpose of the header is to specify or imply the destination path.)

    -M

    Free your mind

Re: Howto Access Two Consecutive Hosts with Net::SFTP
by marto (Cardinal) on Nov 14, 2005 at 12:22 UTC
    Hi neversaint,

    A quick look at NET::SFTP shows no automatic way to do this.
    Could you not use NET::SSH::Perl to connect to host1 and the sftp the files form host1 to host2?

    Hope this helps.

    Martin
      Dear marto,
      Could you not use NET::SSH::Perl to connect to host1 and the sftp the files form host1 to host2?
      Can you give small example of how to do it? Sorry I'm really new with NET module here. I couldn't think about how to go about it. Here is what I tried to do..and lost..
      use Net::SSH::Perl; my $host = 'host1'; my $host2 = 'host2'; my $user = 'edwardwi'; my $pass1 = 'foo'; my $pass2 = 'bar'; # command to do SFTP to host2 # Doesn't seem to work... my $sftp_cmd = Net::SFTP->new($host2, "user" => $user, "password" => $pass2, "debug"=>1) || die 'cannot login $!\n'; my $ssh = Net::SSH::Perl->new($host, port => 22); $ssh->login($user, $pass); # Also how can I call "put/get" command in this context? my ($stdout,$stderr,$exit) = $ssh->cmd($sftp_cmd);
      Is there a correct way to do it?

      ---
      neversaint and everlastingly indebted.......