change ip address of server using PHP

moonlightcheese picture moonlightcheese · Nov 25, 2011 · Viewed 10.8k times · Source

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 code:

//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);
}

returns:

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?

Answer

thaspius picture thaspius · Jun 21, 2012

I had a similar problem and am considering the following solution:

1) The php page reads in the IP, Netmask, and gateway, checking for proper formatting and whether the IP is viable and writes that to a text file

2) A cronjob written in whatever, looks for that file, and if it is there, it reads in the contents, parses it, and makes the changes

This should be sufficiently secure.