I'm attempting to copy a file between two remote servers via PHP using PHPSecLib.
SSH Keys exist between domain.tld and domain2.tld, and locally performing this SCP from domain.tld works fine.
Trimmed down version of my code:
echo "Copying file... ";
$ssh = new Net_SSH2('domain.tld');
$ssh->exec("scp /home/file.tar.gz [email protected]:/home/file.tar.gz");
exit("Failed at line: ".__LINE__);
In the example above, the script would finish after 5 seconds, echoing "Done!" at the end, however the copy has not completed and is still running for approx another 2 minutes.
Unfortuantely, I wish to remotely do things with the file that is being copied in this manner, thus waiting for it to complete is necessary.
I've investigated using PHPSecLib Net_SCP as well, unfortunately this cannot perform a remote -> remote copy, and I need to avoid using the local server as an intermediary for the copy process (Due to distance/bandwidth constraints).
I've attempted to re-write the above using $ssh->read() and $ssh->write(), however with limited understanding of how these procedures are supposed to work, I had no success.
Is anybody able to shine some light on this issue for me?
Answered the problem myself.
It turns out that Net_SSH2 will automatically timeout an exec if no STDOUT is received within a default value.
The answer was to call
$ssh->setTimeout(60000) (or some other equally ridiculous number) before the exec statement.
Are you using public key auth? If you're using password auth then you'd need an interactive prompt. Or you could use the sourcefile attribute too.
I tried it just now using public key auth and it worked just fine.