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.

  • 写回答

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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?