编辑我的centos 6网络配置文件使用PHP代码,但网络重启后,我得到一个错误


edit my centos 6 network config file using PHP code but after network restart i get an error

我在PHP上使用一个简单的文件操作,以便编辑CentOS 6.7(/etc/sysconfig/network-scripts/ifcfg-eth0)上的网络接口配置文件),在更改任何值并保存到配置文件并尝试重新启动网络接口后,我得到这个错误:

does not seem to be present, delaying initialization.
[FAILED]

我的PHP代码是这样的:

<?php
// configuration
$file = '/etc/sysconfig/network-scripts/ifcfg-eth0';
// check if form has been submitted
if (isset($_POST['text']))
{
// save the text contents
file_put_contents($file, $_POST['text']);
// redirect to form again
header('Location: network.php');
exit();
}
// read the textfile
$text = file_get_contents($file);
?>
<!-- HTML form -->
<form action="" method="post">
<textarea style="width:50%; height:50%;" name="text"><?php echo htmlspecialchars($text) ?></textarea>
<input type="submit" />
<input type="reset" />
</form>

我需要通过命令设置手动调用网络脚本,并在设备设置中进行修改并保存,然后我将能够重新启动网络接口。感谢如果有人帮助我为什么这个问题发生,而如果我打开配置文件并手动编辑它不会导致这个问题。

很可能服务器运行的用户名(在大多数基于Red hat的发行版上默认为apache)没有权限写入/etc/sysconfig/network-scripts/ifcfg-eth0

你应该检查:

的返回值
file_put_contents($file, $_POST['text']);
从http://php.net/manual/en/function.file-put-contents.php

此函数返回写入文件的字节数,如果失败则返回FALSE。


我假设这个脚本只会由您或您信任的同事运行,因为没有对用户输入进行验证。缩进还可以使PHP代码更具可读性。