dilip.patel has asked for the wisdom of the Perl Monks concerning the following question:

Is there any way to directly copy file from server 1 to serve 2 without doing login to any of that server? I had tried rcp command but it was not working.

So, I am using this method , doing login to server 1 with ssh from my machine. And then doing scp from server 1 to server 2. If there is any other way to do it then please suggest.

The same thing I want to implementing by script, which will run on my machine and do login to server 1 and copy file from server 1 to server 2. Both server are password protected and I know password of it. I am able to do login to server 1 with "Net::OpenSSH" but I am not find any option in Net::OpenSSH to copy file from server 1 to server 2. Please help.

  • Comment on Copy file from one sereve to another server directly.

Replies are listed 'Best First'.
Re: Copy file from one sereve to another server directly.
by blindluke (Hermit) on Nov 10, 2014 at 14:11 UTC

    Take a look at the Net::SCP module. This should give you the tools you need. To quote the example from the module documentation:

    use Net::SCP; $scp = Net::SCP->new( { "host"=>$hostname, "user"=>$username } ); $scp->scp($source, $destination);

    Still, it assumes that you can login to the host without prompting for password (you have exchanged the SSH keys). If you want to automate the password prompt, there is a another module: Net::SCP::Expect. This should work as a starting point.

    #!/usr/bin/perl use strict; use v5.14; use Net::SCP::Expect; my $user = 'u'; my $pass = 'p'; my $host = '127.0.0.1'; my $src_path = "/tmp/test/"; my $dst_path = "/tmp/"; my $s = Net::SCP::Expect->new; $s->login($user, $pass); $s->scp($src_path,"$host:$dst_path");

    Pick one to your liking, and good luck with the task ahead of you.

    UPDATE: added Net::SCP::Expect example.

    - Luke

      I'm pretty sure that in this case the data will still go via your machine.

      Direct server-to-server copying will probably be much faster. If the servers are within the same data centre, you might achieve speeds of up to several hundred MB per second. Even if they're not, most servers have pretty meaty connections, so you might achieve 50 to 200 MB per second.

      The basic trick is to set up passwordless authentication from server 1 to server 2, and passwordless authentication from your client machine to server 1. Then it's just a matter of:

      ssh 'user1@server1' 'scp' '/path/to/source' 'user@server2:/path/to/des +t'

      Note that the above should still work with password authentication, but you'll be prompted to enter server1's password, then server 2's password.

        I'm pretty sure that in this case the data will still go via your machine.

        I was assuming that OP wants to run the SCP script on one of the servers, and wrote my examples based on this assumption. Thank you for your reply, and making the critical advice about setting up the passwordless authentication - this should always be step 1.

        Still, you are making another assumption - it's possible that the task OP is trying to solve is caused by the fact that both servers don't have network access to each other. A good example (of a very bad practice) would be copying production data to a test environment, when the security rules prohibit direct network access between production and development. In such cases, traffic usually goes through the workstation of some unfortunate sysadmin. Not that I've ever seen it happen. It's so good that security policies are never violated in IT. ;)

        - Luke

      Thanks, but Net::SCP::Expect is working only for remote to local and local to remote.

      I want something which should work for remote to remote also.

        tobyink has already guessed your intentions correctly, and I can only point you towards his good advice.

        - Luke

Re: Copy file from one sereve to another server directly.
by salva (Canon) on Nov 10, 2014 at 22:19 UTC
    After connecting to server1 using Net::OpenSSH you can just use Expect to launch scp there against server2 and handle the password authentication.

    You can see in Net::OpenSSH documentation how to combine it with Expect.

    Another alternative is to configure public key authentication from your machine to both server1 and server2 and then use the authentication agent forwarding feature of SSH to enable ssh'ing from server1 to server2 without storing any keys in server1 (it is usually a bad idea to enable password-less authentication between servers as that can be easily exploitable by any attacker to move freely between servers).

      Hi, Salva

      For tempporary, I had implemented it by using Net::SFTP::Foreign. I had created 2 separate object of Net::SFTP::Foreign, one for reading from server1 and another for writing on server2. But its taking too much time(For 50mb file it is taking ~30min). Since data from server1 is coming to local machine and then it going to server2.

      I had read how to combine Net::OpenSSH with Expect but I am not getting how to use it for scp. Please give me an example of how to use Expect for scp. If you dont mind.

        Update: ignore me. tobyink already said the same thing.

        Assuming that keys are set up properly for passwordless ssh/scp access like so:

        local ---SSH--> server1 ---SCP--> server2

        If I were to do this from the command line, I would do something along the lines of:

        local$ ssh server1 'scp $file server2:$remote_file'

        Implementing this in perl is left as an exercise for the reader :-)

        --MidLifeXis

Re: Copy file from one sereve to another server directly.
by wjw (Priest) on Nov 10, 2014 at 18:10 UTC

    I think that if you create authorization keys (.ssh) between server1 and server2, and then between server 1 and you local machine, you might be able to do what you want without having to manually log in. I may be wrong, but I seem to recall establishing a cron driven daily backup of a db from one server to another using this method...

    Just a thought...

    Update: I see TobyInc already alluded to this previously. Did not read carefully enough..

    ...the majority is always wrong, and always the last to know about it...

    Insanity: Doing the same thing over and over again and expecting different results...

    A solution is nothing more than a clearly stated problem...otherwise, the problem is not a problem, it is simply an inconvenient fact