开发者

Executing shell commands using PHP, e.g. shell_exec() etc., on a remote host?

Is it possible to execute shell commands on a remote computer (not localhost)? For instance things like

$output = shell_exec("unzip filename.zip");

You can assume that you have the login credentials of a user account on the remote machine, as well as the remote root username, pass开发者_StackOverflowword, and cpanel remote access key.


If you mean "a remote computer" as in "not the client computer", the answer is an unqualified yes; commands run via PHP's exec function will execute on the web server.

If you mean "not the web server", the answer is a slighty-hazier yes. You can only directly execute commands on the server running PHP. However, those commands can then run others on remote machines via mechanisms such as SSH. So, for example, if your web server has passwordless ssh access to the remote machine (a very bad idea), this would work: exec('ssh otherhost someremotecommand');. What solution fits for you depends on your desired usage.


I know this question is pretty old but for the viewers of this question.

You can also use ssh2_exec. http://php.net/manual/en/function.ssh2-exec.php

<?php

$ip = 'ip_address'; 
$user = 'username'; 
$pass = 'password'; 

$connection = ssh2_connect($ip); 
ssh2_auth_password($connection,$user,$pass); 
$shell = ssh2_shell($connection,"bash");

?>


Yes, and there is no need to change any code to do so. However, if your server puts PHP into safe mode (the lesser shared hosting plans often do) you may not be able to do this.

Tip: You can use this shorthand to get the output of a command:

$output = `command here`
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜