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 MATLAB四叉树处理长方形tif文件
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成
  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B