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

时间:2013-04-23 19:54:40

标签: php ssh root sudo phpseclib

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

由于用户$我无法访问/ 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


?>

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

2 个答案:

答案 0 :(得分:2)

通常你会告诉sudo webserver用户可以执行脚本而无需使用密码进行身份验证。您必须在/etc/sudoers中添加如下所示的行:

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

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

然后使用

从PHP执行脚本
exec('sudo /root/scripts/script.pl');

答案 1 :(得分:0)

  1. 回显$ ssh-&gt; write()调用并没有太大作用。

  2. sudo需要密码。以下是phpseclib网站上如何处理该问题的示例:http://phpseclib.sourceforge.net/ssh/examples.html#sudo

相关问题