gpt4 book ai didi

php - 如何运行需要root的perl脚本?

转载 作者:行者123 更新时间:2023-12-02 14:23:58 25 4
gpt4 key购买 nike

我需要使用另一台计算机或同一台计算机上的ssh从php执行/ root / scripts /中的perl脚本。问题是,我只是不知道如何使php登录到roots帐户仅执行该文件。

作为user $,我无法访问/ root / scripts /,因此无法使用excec或system调用它。我尝试使用ssh登录sudo su,当我输入密码时挂起。

我将代码留在这里:

<?php
include('Net/SSH2.php');


$ssh = new Net_SSH2('IP');
if (!$ssh->login('user', 'password')) {
exit('Login Failed');
}else{
echo ("login suscess");
}
echo $ssh->read(']$');

echo $ssh->write("sudo su\n");

echo $ssh->read('user:');
//the code normally runs if I dont write the lines below

$ssh->write('password\n');//here I try to get ssh the root pass but php wont respond after this
echo $ssh->read('user]#');//normally this would work to show the root prompt but I don't know what happens


?>

我希望你能在这里帮助我。

最佳答案

通常,您会告诉sudo,Web服务器用户可以执行脚本而无需使用密码进行身份验证。您必须在/etc/sudoers中添加以下内容:

www-data ALL=(ALL:ALL) NOPASSWD: /root/scripts/script.pl

这将使www-data用户可以通过sudo执行 script.pl,而无需输入密码。

然后使用以下命令从PHP执行脚本
exec('sudo /root/scripts/script.pl');

关于php - 如何运行需要root的perl脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16178015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com