PHP ssh2_exec, executing multiple commands at once

孤街浪徒 提交于 2019-12-24 20:35:22

问题


I'm using ssh2_exec to execute a few commands from PHP on my CentOS servers, and I have used it many times, but today, when I tried making it do 4 or 5 commands at once with &&, it did not work. Here is the code below:

if (!($stream = ssh2_exec($con, 'cp -rf /home/shared/Tekkit_Server_3.1.2.zip /home/servers2/'.$mUsername.' && unzip /home/servers2/'.$mUsername.'/Tekkit_Server_3.1.2.zip && rm -rf /home/servers2/'.$mUsername.'/mods/mod_NetherOres.jar && mv -f /home/servers2/'.$mUsername.'/Tekkit.jar /home/servers2/'.$mUsername.'/craftbukkit.jar'))) {
    echo "fail: unable to execute command\n";
}

It copy's the ZIP over to the right directory, but then does nothing from there, any ideas?

Thanks!

EDIT

I tried doing just:

 if (!($stream = ssh2_exec($con, 'unzip /home/servers2/'.$mUsername.'/Tekkit_Server_3.1.2.zip'))){
                        echo "fail: unable to execute command\n";
 }

and it returned: fail: unable to execute command

Other commands work fine, and when I run the command in the terminal it works fine.


回答1:


I tested your example and it looks like it works if you supply -d option to unzip command and specify the directory where the file should be unpacked:

if (!($stream = ssh2_exec($con, 'unzip /home/servers2/'.$mUsername.'/Tekkit_Server_3.1.2.zip -d /home/servers2/'.$mUsername))){
    echo "fail: unable to execute command\n";
}


来源:https://stackoverflow.com/questions/13387641/php-ssh2-exec-executing-multiple-commands-at-once

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!