network managment

This commit is contained in:
Devdatt Bhatt 2026-06-07 23:35:38 +00:00
parent b408dc90e5
commit 13658ddd4c
1 changed files with 217 additions and 85 deletions

View File

@ -1,5 +1,4 @@
<?php <?php
/* /*
Urmi you happy me happy licence Urmi you happy me happy licence
@ -8,93 +7,226 @@ Copyright (c) 2026 shreebhattji
License text: License text:
https://github.com/shreebhattji/Urmi/blob/main/licence.md https://github.com/shreebhattji/Urmi/blob/main/licence.md
*/ */
include 'header.php' ?> include 'header.php';
// Load network configuration
$config_file = '/etc/urmi/network.json';
$network_config = [];
if (file_exists($config_file)) {
$config_data = file_get_contents($config_file);
$network_config = json_decode($config_data, true);
}
// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['action'])) {
$interface = $_POST['interface'];
$action = $_POST['action'];
if ($action === 'save') {
// Save configuration
$config = [
'interface' => $interface,
'method' => $_POST['method'],
'ip' => $_POST['ip'] ?? '',
'netmask' => $_POST['netmask'] ?? '',
'gateway' => $_POST['gateway'] ?? '',
'dns' => $_POST['dns'] ?? ''
];
$network_config[$interface] = $config;
file_put_contents($config_file, json_encode($network_config, JSON_PRETTY_PRINT));
} elseif ($action === 'activate') {
// Activate interface
exec("sudo ip link set $interface up", $output, $return_code);
} elseif ($action === 'deactivate') {
// Deactivate interface
exec("sudo ip link set $interface down", $output, $return_code);
}
}
}
// Get network interfaces excluding specific ones
$interfaces = [];
$output = [];
exec('ip addr show', $output);
$current_interface = null;
$interface_data = [];
foreach ($output as $line) {
// Match interface name
if (preg_match('/^\d+:\s+([a-zA-Z0-9]+):/', $line, $matches)) {
$current_interface = $matches[1];
// Skip interfaces we want to exclude
if (strpos($current_interface, 'enx') === 0) {
$current_interface = null;
continue;
}
if ($current_interface === 'lo') {
$current_interface = null;
continue;
}
// Check if interface is a bridge or docker interface
if (strpos($current_interface, 'docker') === 0 ||
strpos($current_interface, 'br-') === 0 ||
strpos($current_interface, 'veth') === 0) {
$current_interface = null;
continue;
}
$interface_data[$current_interface] = [
'name' => $current_interface,
'ip' => '',
'mac' => '',
'status' => 'down',
'config' => $network_config[$current_interface] ?? null
];
}
// Extract IP address
if ($current_interface && preg_match('/inet\s+(\d+\.\d+\.\d+\.\d+)/', $line, $matches)) {
$interface_data[$current_interface]['ip'] = $matches[1];
}
// Extract MAC address
if ($current_interface && preg_match('/link\/ether\s+([a-f0-9:]+)/', $line, $matches)) {
$interface_data[$current_interface]['mac'] = $matches[1];
}
// Check if interface is up
if ($current_interface && strpos($line, 'state UP') !== false) {
$interface_data[$current_interface]['status'] = 'up';
}
}
?>
<div class="container"> <div class="container">
<h2>Network Interfaces</h2> <h2>Network Interfaces</h2>
<table class="table table-striped">
<thead> <!-- Interface Tabs -->
<tr> <ul class="nav nav-tabs" id="interfaceTabs" role="tablist">
<th>Interface Name</th> <?php $first = true; foreach ($interface_data as $interface): ?>
<th>IP Address</th> <li class="nav-item" role="presentation">
<th>MAC Address</th> <button class="nav-link <?php echo $first ? 'active' : ''; ?>"
<th>Status</th> id="tab-<?php echo $interface['name']; ?>"
</tr> data-bs-toggle="tab"
</thead> data-bs-target="#<?php echo $interface['name']; ?>"
<tbody> type="button"
<?php role="tab">
// Get network interfaces excluding specific ones <?php echo htmlspecialchars($interface['name']); ?>
$interfaces = []; </button>
</li>
// Get all interfaces using ip command <?php $first = false; endforeach; ?>
$output = []; </ul>
exec('ip addr show', $output);
<!-- Tab Content -->
$current_interface = null; <div class="tab-content" id="interfaceTabContent">
$interface_data = []; <?php $first = true; foreach ($interface_data as $interface): ?>
<div class="tab-pane fade <?php echo $first ? 'show active' : ''; ?>"
foreach ($output as $line) { id="<?php echo $interface['name']; ?>"
// Match interface name role="tabpanel">
if (preg_match('/^\d+:\s+([a-zA-Z0-9]+):/', $line, $matches)) { <div class="card mt-3">
$current_interface = $matches[1]; <div class="card-header">
<h5><?php echo htmlspecialchars($interface['name']); ?></h5>
// Skip interfaces we want to exclude </div>
if (strpos($current_interface, 'enx') === 0) { <div class="card-body">
$current_interface = null; <div class="row">
continue; <div class="col-md-6">
} <p><strong>IP Address:</strong> <?php echo htmlspecialchars($interface['ip'] ?: 'N/A'); ?></p>
<p><strong>MAC Address:</strong> <?php echo htmlspecialchars($interface['mac'] ?: 'N/A'); ?></p>
if ($current_interface === 'lo') { <p><strong>Status:</strong>
$current_interface = null; <span class="badge bg-<?php echo $interface['status'] === 'up' ? 'success' : 'secondary'; ?>">
continue; <?php echo htmlspecialchars($interface['status']); ?>
} </span>
</p>
// Check if interface is a bridge or docker interface </div>
if (strpos($current_interface, 'docker') === 0 || <div class="col-md-6">
strpos($current_interface, 'br-') === 0 || <form method="post" action="">
strpos($current_interface, 'veth') === 0) { <input type="hidden" name="interface" value="<?php echo htmlspecialchars($interface['name']); ?>">
$current_interface = null;
continue; <div class="mb-3">
} <label class="form-label">Configuration Method</label>
<div class="form-check">
$interface_data[$current_interface] = [ <input class="form-check-input" type="radio" name="method" id="dhcp-<?php echo $interface['name']; ?>"
'name' => $current_interface, value="dhcp" <?php echo ($interface['config']['method'] ?? '') === 'dhcp' ? 'checked' : ''; ?>>
'ip' => '', <label class="form-check-label" for="dhcp-<?php echo $interface['name']; ?>">
'mac' => '', DHCP
'status' => 'down' </label>
]; </div>
} <div class="form-check">
<input class="form-check-input" type="radio" name="method" id="static-<?php echo $interface['name']; ?>"
// Extract IP address value="static" <?php echo ($interface['config']['method'] ?? '') === 'static' ? 'checked' : ''; ?>>
if ($current_interface && preg_match('/inet\s+(\d+\.\d+\.\d+\.\d+)/', $line, $matches)) { <label class="form-check-label" for="static-<?php echo $interface['name']; ?>">
$interface_data[$current_interface]['ip'] = $matches[1]; Static IP
} </label>
</div>
// Extract MAC address </div>
if ($current_interface && preg_match('/link\/ether\s+([a-f0-9:]+)/', $line, $matches)) {
$interface_data[$current_interface]['mac'] = $matches[1]; <div class="mb-3" id="static-ip-fields-<?php echo $interface['name']; ?>"
} style="<?php echo ($interface['config']['method'] ?? '') === 'static' ? 'display: block;' : 'display: none;'; ?>">
<label class="form-label">IP Address</label>
// Check if interface is up <input type="text" class="form-control" name="ip"
if ($current_interface && strpos($line, 'state UP') !== false) { value="<?php echo htmlspecialchars($interface['config']['ip'] ?? ''); ?>"
$interface_data[$current_interface]['status'] = 'up'; placeholder="192.168.1.100">
}
} <label class="form-label mt-2">Netmask</label>
<input type="text" class="form-control" name="netmask"
// Display the filtered interfaces value="<?php echo htmlspecialchars($interface['config']['netmask'] ?? ''); ?>"
foreach ($interface_data as $interface) { placeholder="255.255.255.0">
if (!empty($interface['ip']) || !empty($interface['mac'])) {
echo "<tr>"; <label class="form-label mt-2">Gateway</label>
echo "<td>" . htmlspecialchars($interface['name']) . "</td>"; <input type="text" class="form-control" name="gateway"
echo "<td>" . htmlspecialchars($interface['ip']) . "</td>"; value="<?php echo htmlspecialchars($interface['config']['gateway'] ?? ''); ?>"
echo "<td>" . htmlspecialchars($interface['mac']) . "</td>"; placeholder="192.168.1.1">
echo "<td>" . htmlspecialchars($interface['status']) . "</td>";
echo "</tr>"; <label class="form-label mt-2">DNS Server</label>
} <input type="text" class="form-control" name="dns"
} value="<?php echo htmlspecialchars($interface['config']['dns'] ?? ''); ?>"
?> placeholder="8.8.8.8">
</tbody> </div>
</table>
<div class="d-flex justify-content-between">
<button type="submit" name="action" value="save" class="btn btn-primary">Save Configuration</button>
<div>
<button type="submit" name="action" value="activate"
class="btn btn-success <?php echo $interface['status'] === 'up' ? 'disabled' : ''; ?>">
Activate
</button>
<button type="submit" name="action" value="deactivate"
class="btn btn-danger <?php echo $interface['status'] === 'down' ? 'disabled' : ''; ?>">
Deactivate
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php $first = false; endforeach; ?>
</div>
</div> </div>
<script>
// Toggle static IP fields based on method selection
document.querySelectorAll('input[name^="method"]').forEach(radio => {
radio.addEventListener('change', function() {
const interfaceName = this.name.replace('method', '');
const staticFields = document.getElementById(`static-ip-fields-${interfaceName}`);
if (this.value === 'static') {
staticFields.style.display = 'block';
} else {
staticFields.style.display = 'none';
}
});
});
</script>
<?php include 'footer.php' ?> <?php include 'footer.php' ?>