dongzu0742 2011-04-27 11:13
浏览 89
已采纳

从PHP修改smb.conf?

I need to add/remove SAMBA shares through PHP or via SSH (ssh link from PHP).

So far the only solution I've found is a class for primitively handling the file, which I haven't got very much faith in in terms of reliability. (http://www.phpclasses.org/package/1546-PHP-Parse-and-recreate-the-Samba-smb-conf-file.html)

Could you recommend a way to do it?

  • 写回答

2条回答 默认 最新

  • dtsjq28482 2011-04-27 12:15
    关注

    Can't find something useful, so I would recommend a likewise simplistic approach. Instead of finding a full-fledged INI parser, it's advisable to work in append-mostly mode. A simple regex would be sufficient to replace existing [share] sections without harming the rest of the smb.conf file.

    And you can use the testparm utility to probe for correctness before overwriting the real file.

    define("SMB_CONF", "/etc/samba/smb.conf");
    
    function add_share($section, $options) {
    
        // read old data
        #$old = parse_ini_file(SMB_CONF);
        $conf = file_get_contents(SMB_CONF);
    
        // merge new settings
        #if (isset($old[$section])) {
        #    $options = array_merge($old[$section], $options);
        #}
    
        // remove old share section, if it exists
        preg_replace("/^\[$section\]\s*
    (^(?!\[).*
    )+/m", "", $conf);
    
        // write out new ini file
        $conf .= "
    
    [$section]
    ";
        foreach ($options as $key=>$value) {
            $conf .= "$key = $value
    ";
        }
        $tmp = tempnam("/tmp/", "SMB");
        file_put_contents($tmp, $conf);
    
        // copy if it is syntactically correct
        if (strstr(`testparm -s $tmp 2>&1`, "OK")) {
            rename($tmp, SMB_CONF);
        }
    }
    

    Updated Okay, parse_ini_file does not work in either case. It trips over the smb.conf style. So you can only completely replace existing entries, not update them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求