2

我需要能够使用 PHP 更改服务器的 IP 地址。我正在尝试以用户身份使用ifconfig eth0 downwww-data确保它能够正常工作。到目前为止,我已经摆脱了 /var/run/network/ifstate 文件的权限问题,但现在我得到了一条权限被拒绝的行,内容为SIOCSIFFLAGS: Permission denied. 有没有解决的办法?如果不是,如何更改网页中服务器的 IP 地址?

php代码:

//if the ip has changed, bring down the network interface and bring it up with the new IP
if($ipConf != $ip) {
    $ifdownSuccess = exec("ifconfig eth0 down", $downOutput, $downRetvar);
    $ifupSuccess = exec("ifconfig eth0 up ".$ip, $upOutput, $upRetvar);
    //TODO: check for ifupSucess and revert to old ip if the command failed
    var_dump($downOutput);
    var_dump($downRetvar);
    var_dump($ifdownSuccess);
    var_dump($upOutput);
    var_dump($upRetvar);
    var_dump($ifupSuccess);
}

返回:

array(0) { } int(127) string(0) "" array(0) { } int(127) string(0) ""

有没有办法解决这个权限问题或我可以使用其他工具来做到这一点?

4

2 回答 2

3

我遇到了类似的问题,正在考虑以下解决方案:

1) php 页面读取 IP、网络掩码和网关,检查格式是否正确以及 IP 是否可行并将其写入文本文件

2)一个用任何东西写的cronjob,寻找那个文件,如果它在那里,它读入内容,解析它,并进行更改

这应该足够安全。

于 2012-06-21T18:58:54.627 回答
1

我想通了。答案是将www-data用户(或您的服务器用户的名称)添加到管理员组中usermod -a -G admin www-data。如果您看一下/etc/sudoers,您会注意到该组中的任何人都可以sudo使用sudo -n <command>. 快速更改代码:

//if the ip has changed, bring down the network interface and bring it up with the new IP
if($ipConf != $ip) {
    $ifdownSuccess = exec("sudo -n ifconfig eth0 down", $downOutput, $downRetvar);
    $ifupSuccess = exec("sudo -n ifconfig eth0 up ".$ip, $upOutput, $upRetvar);
    //TODO: check for ifupSucess and revert to old ip if the command failed
    var_dump($downOutput);
    var_dump($downRetvar);
    var_dump($ifdownSuccess);
    var_dump($upOutput);
    var_dump($upRetvar);
    var_dump($ifupSuccess);
}

我现在在做生意。能够通过 SSH 连接到新 IP 地址并通过新 IP 查看网页。

于 2011-11-25T17:24:13.467 回答