155 lines
5.4 KiB
PHP
155 lines
5.4 KiB
PHP
<?php include('partialpage/header.php');?>
|
|
<?php include('partialpage/sidebar.php');?>
|
|
|
|
<script>
|
|
$(document).ready(function() {
|
|
checkdhcp();
|
|
$("#dhcp, #static").change(function () {
|
|
checkdhcp();
|
|
});
|
|
});
|
|
|
|
function checkdhcp(){
|
|
if ($("#static").is(":checked")) {
|
|
$("#ipaddress").removeAttr("disabled");
|
|
$("#subnet").removeAttr("disabled");
|
|
$("#gateway").removeAttr("disabled");
|
|
$("#primarydns").removeAttr("disabled");
|
|
$("#secondarydns").removeAttr("disabled");
|
|
}
|
|
if ($("#dhcp").is(":checked")) {
|
|
$("#ipaddress").attr("disabled", "disabled");
|
|
$("#subnet").attr("disabled", "disabled");
|
|
$("#gateway").attr("disabled", "disabled");
|
|
$("#primarydns").attr("disabled", "disabled");
|
|
$("#secondarydns").attr("disabled", "disabled");
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<?php
|
|
$path = '/etc/network/interfaces';
|
|
|
|
function clearnetworksettings($filename) {
|
|
if (file_exists($filename)){
|
|
if (!is_writable($filename)) {
|
|
$command = "sudo chmod 666 $filename";
|
|
exec($command);
|
|
}
|
|
$data = file_get_contents($filename);
|
|
$replace_match = '/^.*(?:address|netmask|gateway|dns-nameservers).*$(?:\r\n|\n)?/m';
|
|
$data = preg_replace($replace_match, '', $data);
|
|
file_put_contents($filename, $data);
|
|
}
|
|
}
|
|
|
|
if($_SERVER['REQUEST_METHOD'] == 'POST' ){
|
|
clearnetworksettings($path);
|
|
|
|
if ($_POST['network'] == 'dhcp'){
|
|
shell_exec("sudo sed -i 's/static/dhcp/' /etc/network/interfaces");
|
|
shell_exec("sudo /etc/init.d/./S45ifplugd restart");
|
|
$message = "Network set to dhcp";
|
|
}
|
|
if ($_POST['network'] == 'static'){
|
|
shell_exec("sudo sed -i 's/dhcp/static/' /etc/network/interfaces");
|
|
|
|
if (file_exists($path)){
|
|
if (!is_writable($path)) {
|
|
$command = "sudo chmod 666 $path";
|
|
exec($command);
|
|
}
|
|
$fp = fopen($path, 'a');
|
|
if(!$fp){
|
|
echo 'file is not opend';
|
|
}
|
|
fwrite($fp, " address " .$_POST['ipaddress'] .PHP_EOL);
|
|
fwrite($fp, " netmask " .$_POST['subnet'] .PHP_EOL);
|
|
fwrite($fp, " gateway " .$_POST['gateway'] .PHP_EOL);
|
|
fwrite($fp, " dns-nameservers " .$_POST['primarydns'] .PHP_EOL);
|
|
fwrite($fp, " dns-nameservers " .$_POST['secondarydns'] .PHP_EOL);
|
|
fclose($fp);
|
|
}
|
|
|
|
shell_exec("sudo /etc/init.d/./S45ifplugd restart");
|
|
$message = "Network set to static";
|
|
}
|
|
}else{
|
|
//$message = "get";
|
|
};
|
|
|
|
$interface = shell_exec('cat /etc/network/interfaces');
|
|
$dhcp = false;
|
|
if (strpos($interface, 'dhcp') !== false) {
|
|
$dhcp = true;
|
|
}
|
|
|
|
$ipaddress = shell_exec("ifconfig eth0 | grep 'inet addr' | cut -d':' -f2 | cut -d' ' -f1");
|
|
$subnet = shell_exec("ifconfig eth0 | grep 'Mask' | cut -d':' -f4");
|
|
$gateway = shell_exec("route | grep 'default' | awk {'print $2'}");
|
|
$primarydns = shell_exec("cat /etc/resolv.conf | grep 'nameserver' | head -n 1 | cut -d' ' -f2");
|
|
$secondarydns = shell_exec("cat /etc/resolv.conf | grep 'nameserver' | head -n 2 | tail -n 1 | cut -d' ' -f2");
|
|
?>
|
|
|
|
<!-- !PAGE CONTENT! -->
|
|
<div class="w3-main" style="margin-left:340px;margin-right:40px">
|
|
|
|
<!-- Header -->
|
|
<div class="w3-container" style="margin-top:80px" id="showcase">
|
|
<h1 class="w3-jumbo"><b>Network</b></h1>
|
|
<br>
|
|
<div style="border:5px solid red; width:100px" class="w3-round"></div>
|
|
<br>
|
|
</div>
|
|
|
|
<?php if(isset($message)){
|
|
echo "<!-- post result messages -->";
|
|
echo "<div class='w3-container' id='resultmessage' style='margin-top:75px'>";
|
|
echo "<p>$message</p>";
|
|
echo "</div>";
|
|
};?>
|
|
|
|
<p>Network configuration.</p>
|
|
<div class="w3-row">
|
|
<div class="w3-col l6">
|
|
<form action="network.php" method="POST">
|
|
<div class="w3-section">
|
|
<input class="w3-radio" type="radio" name="network" value="dhcp" id="dhcp" <?php if($dhcp){echo 'checked';}?>>
|
|
<label>Use DHCP</label>
|
|
</div>
|
|
<div class="w3-section">
|
|
<input class="w3-radio" type="radio" name="network" value="static" id="static" <?php if(!$dhcp){echo 'checked';}?>>
|
|
<label>Use static IP settings</label>
|
|
</div>
|
|
|
|
<div class="w3-section">
|
|
<label class="w3-label">IP address</label>
|
|
<input class="w3-input w3-border" type="text" name="ipaddress" id="ipaddress" value="<?php echo $ipaddress ?>" required>
|
|
</div>
|
|
<div class="w3-section">
|
|
<label class="w3-label">Subnet mask</label>
|
|
<input class="w3-input w3-border" type="text" name="subnet" id="subnet" value="<?php echo $subnet ?>" required>
|
|
</div>
|
|
<div class="w3-section">
|
|
<label class="w3-label">Gateway</label>
|
|
<input class="w3-input w3-border" type="text" name="gateway" id="gateway" value="<?php echo $gateway ?>" required>
|
|
</div>
|
|
<div class="w3-section">
|
|
<label class="w3-label">Primary DNS server</label>
|
|
<input class="w3-input w3-border" type="text" name="primarydns" id="primarydns" value="<?php echo $primarydns ?>" required>
|
|
</div>
|
|
<div class="w3-section">
|
|
<label class="w3-label">Secondary DNS server</label>
|
|
<input class="w3-input w3-border" type="text" name="secondarydns" id="secondarydns" value="<?php echo $secondarydns ?>" required>
|
|
</div>
|
|
|
|
<button type="submit" class="w3-button w3-block w3-padding-large w3-red w3-margin-bottom">Change network settings</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- End page content -->
|
|
</div>
|
|
|
|
<?php include('partialpage/footer.php');?>
|