I'm writing a script to automate deployment from my windows development PC to a shared hosting server.
I am getting different results depending on whether I execute the commands via Putty or PHP (both running on my PC).
In putty, when I log in to the server via SSH, I can run commands like:
cd /www/
ls -la #outputs contents of /www
But when I do it via PHP with phpseclib, as below, any cd
commands are totally ignored:
<?php
require_once __DIR__.'/vendor/autoload.php';
use phpseclib\Net\SSH2;
$ssh = new SSH2('ssh.mydomain.com');
if (!$ssh->login('mydomain.com', 'mypassword')) {
trigger_error("Login Failed", E_ERROR);
}
echo $ssh->exec('pwd');
$ssh->exec('cd /www/');
echo $ssh->exec('pwd'); // unchanged
echo $ssh->exec('ls -la'); // does NOT output contents of /www/
echo $ssh->exec('ls /www/ -la'); // DOES output contents of /www/
Specifying an absolute URL in the above is an acceptable workaround. However, the following is a major problem.
If I upload a file, stuff.zip
into /www/ and then try to extract it, the following works via Putty:
unzip /www/stuff.zip -d /www/
But if I try this via PHP:
echo $ssh->exec('unzip /www/stuff.zip -d /www/');
I get the error:
unzip: cannot find or open /www/stuff.zip, /www/stuff.zip.zip or /www/stuff.zip.ZIP.
I have tried chmod 777 /www/stuff.zip
but it makes no difference.
How can I diagnose/fix this problem?
Successive calls to exec()
If done on an interactive shell, the output you'd receive for the first pwd would (depending on how your system is setup) be different than the output of the second pwd. The above code snippet, however, will yield two identical lines.
The reason for this is that any "state changes" you make to the one-time shell are gone once the exec() has been ran and the channel has been deleted.
I see two options. Option 1:
Try the interactive shell examples with the read()
and write()
functions, which look pretty ugly to me.
Option 2:
Write your script as a shell script on the server and call the script with a single $ssh->exec()
call.
来源:https://stackoverflow.com/questions/31958954/why-would-ssh-commands-through-putty-work-differently-to-those-via-phps-phpsecl