change ip address of server using PHP

ぐ巨炮叔叔 提交于 2019-12-01 14:09:48

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!