dongwu9972
2011-10-03 01:44
浏览 53
已采纳

PHP-CLI Sudo执行

I am running a cli-script, that requires a exec('sudo ...'); call. I know it is not safe on the web, but how can it be done in cli? The script is executed by a user known as "btcdbit", who is in the sudoers file.

图片转代码服务由CSDN问答提供 功能建议

我正在运行一个cli脚本,需要一个exec('sudo ...'); 呼叫。 我知道它在网络上不安全,但如何在cli中完成? 该脚本由名为“btcdbit”的用户执行,该用户位于sudoers文件中。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • duangang4001 2011-10-09 21:36
    最佳回答

    In my experience setting the NOPASSWD option doesn't always work and even if it does it seems unsafe. Seems to me that a better approach - if you're able to use it - would involve using phpseclib to do sudo through SSH. eg.

    <?php
    include('Net/SSH2.php');
    
    $sftp = new Net_SSH2('www.domain.tld');
    $sftp->login('username', 'password');
    
    echo $sftp->read('username@username:~$');
    $sftp->write("sudo ls -la
    ");
    $output = $sftp->read('#Password:|username@username:~\$#', NET_SSH2_READ_REGEX);
    echo $output;
    if (preg_match('#Password:#', $lines)) {
        $ssh->write("password
    ");
        echo $sftp->read('username@username:~$');
    }
    ?>
    

    The website "sudo in php" elaborates

    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题