snoopy76
snoopy76

Reputation: 305

SCP command line and PHP exec() not doing the same thing

When I run this on the command line:

scp -i private.ppk -P 22 foo.txt [email protected]:/home/someuser/foo.txt

Everything works fine. foo.txt gets transferred to the remote server without any problems. I need to run the exact same command from PHP. So, I have the following code:

$command = 'scp -i private.ppk -P 22 foo.txt [email protected]:/home/someuser/foo.txt';
exec($command);

This doesn't work. The PHP file is being executed in the same directory as private.ppk. When I var_dump the results of the exec, I get a NULL.

I'm wondering if this is a permission problem. I know who I am running as on the command line, but I am not sure who is running the script. Could that be the problem? Or is it something completely different?

Upvotes: 0

Views: 5864

Answers (1)

neubert
neubert

Reputation: 16792

I think it is a permission issue. SSH clients usually refuse to connect unless the permissions have been set such that third parties (such as the user "nobody") can't read the keys.

That said, it is a bit odd that you're using a *.ppk - which is the extension PuTTY keys use - which OpenSSH doesn't support.

That said, my recommendation would be to use phpseclib's SCP client. You can just have the keys as a string in the PHP file itself at that point instead of having to have them live in the filesystem. More info:

https://github.com/phpseclib/phpseclib/blob/master/phpseclib/Net/SCP.php

Upvotes: 1

Related Questions