Php 自动登录远程服务器上的shell以执行某些命令时出现问题
我想实现与远程服务器上的特定用户在shell上自动登录,并执行命令安装android应用程序。 我编写了一些代码,通过Php 自动登录远程服务器上的shell以执行某些命令时出现问题,php,shell,command,remote-server,Php,Shell,Command,Remote Server,我想实现与远程服务器上的特定用户在shell上自动登录,并执行命令安装android应用程序。 我编写了一些代码,通过PHP登录到远程服务器。我使用ssh2\u connect方法连接到服务器,使用公钥进行身份验证,它就可以工作了。现在我想安装一个apk文件。我需要执行以下命令 sudo用户 pm安装-r/path/to/file/on/remote 用户没有密码。我在PHP中的脚本如下所示 $con = ssh2_connect("xxx.xxx.xx.xxx", &quo
PHP
登录到远程服务器。我使用ssh2\u connect
方法连接到服务器,使用公钥进行身份验证,它就可以工作了。现在我想安装一个apk文件。我需要执行以下命令
sudo用户
pm安装-r/path/to/file/on/remote
PHP
中的脚本如下所示
$con = ssh2_connect("xxx.xxx.xx.xxx", "xy", array("hostkey" => "ssh-rsa"));
if(ssh2_auth_pubkey_file($con, "user", $pubKey, $privKey, "password"))
{
$dataToSend = file_get_contents($srcFile);
if($dataToSend === false) throw new Exception("Could not open local file: " . $srcFile);
if(fwrite( $sftpStream, $dataToSend) === false) throw new Exception("Could not send data from file: " . $srcFile);
$shell = ssh2_shell($con, "xterm");
fwrite($shell, "commands to login and install app");
fclose($shell);
echo "passed through!";
}
我没有找到解决我问题的办法。在我的本地pc上,它与
echo shell_exec("echo <password> | sudo -S <command>");
做我需要的一切。问题可能是所有这些“密码询问实用程序”(
sudo
这里)都坚持交互式tty,甚至拒绝要求密码。这有明显的安全原因。一种可能的解决方案是在sudoers
文件中显式地将该用户的pm install
命令列为白名单,并将其标记为不带密码的可执行文件。@arkascha我在endEm编辑了我的帖子,对不起,但是你读过我解释的问题和可能的解决方案吗?你最后的评论建议其他…@arkascha是的,我读了你的回复,我正在努力,谢谢你的建议。我只想为正在阅读本文的其他一些用户指定我的问题;)。远程服务器正在使用android,要找到文件应该存放的正确路径并不容易。问题可能是所有这些“密码询问实用程序”(sudo
此处)都坚持使用交互式tty,否则甚至拒绝询问密码。这有明显的安全原因。一种可能的解决方案是在sudoers
文件中显式地将该用户的pm install
命令列为白名单,并将其标记为不带密码的可执行文件。@arkascha我在endEm编辑了我的帖子,对不起,但是你读过我解释的问题和可能的解决方案吗?你最后的评论建议其他…@arkascha是的,我读了你的回复,我正在努力,谢谢你的建议。我只想为正在阅读本文的其他一些用户指定我的问题;)。远程服务器使用android,要找到文件应该存放的正确路径并不容易
$stream = ssh2_exec($con, "cd /path/to/anywhere; mkdir ./test; chmod 444 ./test/");
stream_set_blocking($stream, true);
echo stream_get_contents($stream) . "<br />";
ssh2_exec($con, "su system pm install -r " . $dstFile . "&");