使用PHP更改服务器的IP地址 [英] change ip address of server using PHP

查看:552
本文介绍了使用PHP更改服务器的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i need to be able to change the IP address of a server using PHP. i'm trying to use ifconfig eth0 down as the www-data user to make sure it will work. so far, i've gotten rid of a permissions issue on /var/run/network/ifstate file, but now i get a permission denied line that reads SIOCSIFFLAGS: Permission denied. is there a way around this? if not, how do you change the IP address of a server in a web page?

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

是否可以解决此权限问题,或者可以使用其他工具来解决此问题?

is there a way around this permissions issue or another tool i can use to do this?

推荐答案

我发现了这一点.答案是使用usermod -a -G admin www-datawww-data用户(或服务器用户的任何名称)添加到管理组中.如果查看/etc/sudoers,您会注意到该组中的任何人都可以执行sudo命令,而无需使用sudo -n <command>进行密码提示.进行了快速的代码更改:

i figured this out. the answer was to add the www-data user (or whatever the name of your server user is) to the admin group with usermod -a -G admin www-data. if you take a look at /etc/sudoers, you'll notice that anyone in this group can perform sudo commands without a password prompt using sudo -n <command>. made a quick code change:

//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查看网页.

and i'm now in business. was able to connect on the new IP address via SSH and view webpages via the new IP as well.

这篇关于使用PHP更改服务器的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆