使用PHP更改服务器的IP地址
我需要能够使用 PHP 更改服务器的 IP 地址。我正在尝试使用 ifconfig eth0 down
作为 www-data
用户来确保它能够正常工作。到目前为止,我已经解决了 /var/run/network/ifstate 文件上的权限问题,但现在我收到一条权限被拒绝的行,内容为 SIOCSIFFLAGS: Permission returned
。有办法解决这个问题吗?如果没有,如何更改网页中服务器的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) ""
有没有办法解决此权限问题或我可以使用其他工具来执行此操作?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了类似的问题,正在考虑以下解决方案:
1)php页面读取IP,网络掩码和网关,检查格式是否正确以及IP是否可行并将其写入文本文件
2)编写的cronjob不管怎样,查找该文件,如果存在,它会读取内容,解析它,并进行更改
这应该足够安全。
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.
我想通了。答案是使用
usermod -a -G admin www-data
将www-data
用户(或服务器用户的任何名称)添加到管理员组。如果您查看/etc/sudoers
,您会发现该组中的任何人都可以使用sudo -n < 执行
。快速更改代码:sudo
命令,而无需密码提示;命令>我现在开始营业了。能够通过 SSH 连接到新的 IP 地址并通过新的 IP 查看网页。
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 withusermod -a -G admin www-data
. if you take a look at/etc/sudoers
, you'll notice that anyone in this group can performsudo
commands without a password prompt usingsudo -n <command>
. made a quick code change: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.