如何运行需要 root 的 perl 脚本? [英] how can I run a perl script which needs root?

查看:68
本文介绍了如何运行需要 root 的 perl 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to execute a perl script located in /root/scripts/ from php using ssh from another computer or from the same computer. The thing is that I just don't know how to make php log into roots account to excecute only that file.

作为用户 $ 我无法访问/root/scripts/,所以我不能使用 execc 或 system 调用它.我曾尝试使用 ssh 登录 sudo su,但当我提供密码时,它挂起.

As user$ I can't access /root/scripts/, so I can't call it using excec or system. I have tried using ssh to log into sudo su, and when I give my password it hangs.

我把代码留在这里:

<?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


?>

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

I hope you can help me out here.

推荐答案

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

Usually you would tell sudo that the webserver user can execute the script without need to authenticate with a password. You'll have to add a line like the following in /etc/sudoers:

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

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

This would let the www-data user execute script.pl via sudo without having to enter a password.

然后使用 PHP 执行脚本

Then execute the script from PHP using

exec('sudo /root/scripts/script.pl');

这篇关于如何运行需要 root 的 perl 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆