Auslesen von Netzwerkinformationen

This commit is contained in:
2023-05-02 22:03:09 +02:00
parent f2938cf701
commit ec8a954b0b
3 changed files with 63 additions and 79 deletions

View File

@@ -1,60 +1,51 @@
<?php <?php
if (!defined('PICONTROL')) exit(); if (!defined('PICONTROL')) exit();
(include_once LIBRARY_PATH.'main/rpi.function.php') or die('Error: 0x0010'); (include_once LIBRARY_PATH . 'main/rpi.function.php') or die('Error: 0x0010');
(include_once LIBRARY_PATH.'network/network.function.php') or die('Error: 0x0011'); (include_once LIBRARY_PATH . 'network/network.function.php') or die('Error: 0x0011');
$tpl->setHeaderTitle(_t('Netzwerk')); $tpl->setHeaderTitle(_t('Netzwerk'));
if (isset($_GET['hostname'])) if (isset($_GET['hostname'])) {
{ if (isset($_POST['submit']) && $_POST['submit'] != '') {
if (isset($_POST['submit']) && $_POST['submit'] != '') if (isset($_POST['hostname']) && trim($pHostname = trim($_POST['hostname'])) != '') {
{ if (preg_match('/^([a-z][a-z0-9\-\.]*[^\-]){1,24}$/im', $pHostname)) {
if (isset($_POST['hostname']) && trim($pHostname = trim($_POST['hostname'])) != '')
{
if (preg_match('/^([a-z][a-z0-9\-\.]*[^\-]){1,24}$/im', $pHostname))
{
if (($status = editHostname($pHostname)) === true) if (($status = editHostname($pHostname)) === true)
$tpl->msg('success', _t('Hostname gespeichert'), _t('Damit die &Auml;nderung wirksam wird, muss dein Raspberry Pi neu gestartet werden. <a href="%s">Jetzt neu starten.</a>', '?s=shutdown&restart')); $tpl->msg('success', _t('Hostname gespeichert'), _t('Damit die &Auml;nderung wirksam wird, muss dein Raspberry Pi neu gestartet werden. <a href="%s">Jetzt neu starten.</a>', '?s=shutdown&restart'));
else else
$tpl->msg('error', _t('Fehler'), _t('Fehler beim &Auml;ndern des Hostname! Fehlercode: %s', $status)); $tpl->msg('error', _t('Fehler'), _t('Fehler beim &Auml;ndern des Hostname! Fehlercode: %s', $status));
} } else
else
$tpl->msg('error', _t('Fehler'), _t('Der Hostname ist ung&uuml;ltig! Er muss aus mindestens 1 bis 24 Zeichen bestehen und darf nur folgende Zeichen enthalten: A-Z a-z 0-9 -<br />Der Hostname darf nicht mit einem Bindestrich Anfangen oder Enden.')); $tpl->msg('error', _t('Fehler'), _t('Der Hostname ist ung&uuml;ltig! Er muss aus mindestens 1 bis 24 Zeichen bestehen und darf nur folgende Zeichen enthalten: A-Z a-z 0-9 -<br />Der Hostname darf nicht mit einem Bindestrich Anfangen oder Enden.'));
} }
} }
$tpl->assign('hostname', rpi_getHostname()); $tpl->assign('hostname', rpi_getHostname());
$tpl->draw('network_hostname'); $tpl->draw('network_hostname');
} } else {
else
{
$networkConnections = getAllNetworkConnections(); $networkConnections = getAllNetworkConnections();
$networkCounts = unserialize(htmlspecialchars_decode(getConfig('main:network.overflowCount', 'a:0:{}'))); $networkCounts = unserialize(htmlspecialchars_decode(getConfig('main:network.overflowCount', 'a:0:{}')));
$counter = 0; $counter = 0;
foreach ($networkConnections as $network) foreach ($networkConnections as $network) {
{
$countSent = 0; $countSent = 0;
$countReceive = 0; $countReceive = 0;
if (isset($networkCounts[$network['interface']]['sent'])) if (isset($networkCounts[$network['interface']]['sent']))
$countSent = $networkCounts[$network['interface']]['sent']; $countSent = $networkCounts[$network['interface']]['sent'];
if (isset($networkCounts[$network['interface']]['receive'])) if (isset($networkCounts[$network['interface']]['receive']))
$countReceive = $networkCounts[$network['interface']]['receive']; $countReceive = $networkCounts[$network['interface']]['receive'];
$networkConnections[$counter]['sent'] = (4294967295 * $countSent) + $network['sent']; $networkConnections[$counter]['sent'] = (4294967295 * $countSent) + $network['sent'];
$networkConnections[$counter]['receive'] = (4294967295 * $countReceive) + $network['receive']; $networkConnections[$counter]['receive'] = (4294967295 * $countReceive) + $network['receive'];
$counter += 1; $counter += 1;
} }
$tpl->assign('network_connections', $networkConnections); $tpl->assign('network_connections', $networkConnections);
$tpl->assign('hostname', rpi_getHostname()); $tpl->assign('hostname', rpi_getHostname());
$tpl->assign('wlan', scanAccessPoints($networkConnections, (isset($_GET['refresh_wlan'])) ? true : false)); $tpl->assign('wlan', scanAccessPoints($networkConnections, (isset($_GET['refresh_wlan'])) ? true : false));
$tpl->draw('network'); $tpl->draw('network');
} }
?>

View File

@@ -38,7 +38,7 @@ $tpl->assign('configHelpFilesFolders', $config['url']['help'].'?s=view&amp;i=1'.
$tpl->assign('configHelpCron', $config['url']['help'].'?s=view&amp;i=10'.getURLLangParam()); $tpl->assign('configHelpCron', $config['url']['help'].'?s=view&amp;i=10'.getURLLangParam());
$tpl->assign('cronEntry', $cronEntry); $tpl->assign('cronEntry', $cronEntry);
$tpl->assign('cronMatch', $cronMatch); $tpl->assign('cronMatch', $cronMatch);
$tpl->assign('cronPHPCLI', ($cronPHPCLI = (trim(exec('dpkg -s php7.0-cli | grep Status: ')) != '' || trim(exec('dpkg -s php7.3-cli | grep Status: ')) != '') ? true : false)); $tpl->assign('cronPHPCLI', ($cronPHPCLI = (trim(exec('dpkg -s php-cli | grep Status: ')) != '' || trim(exec('dpkg -s php7.3-cli | grep Status: ')) != '') ? true : false));
$tpl->assign('cronLastExecution', formatTime(getConfig('cron:execution.cron', 0))); $tpl->assign('cronLastExecution', formatTime(getConfig('cron:execution.cron', 0)));
$tpl->assign('cronLastExecutionBool', ($cronLastExecutionBool = (getConfig('cron:execution.cron', 0) > time()-150) ? true : false)); $tpl->assign('cronLastExecutionBool', ($cronLastExecutionBool = (getConfig('cron:execution.cron', 0) > time()-150) ? true : false));
$tpl->assign('cronLastExecutionLog', formatTime($lastExecutionLog[0])); $tpl->assign('cronLastExecutionLog', formatTime($lastExecutionLog[0]));

View File

@@ -393,60 +393,53 @@ function getImageFromSignal($signal)
function getAllNetworkConnections() function getAllNetworkConnections()
{ {
$shell_string = ''; $networkInterfaces = json_decode(shell_exec('ip -j addr'));
$output = array();
exec('/sbin/ifconfig | grep -E -o "^[[:alnum:][:punct:]]*" | grep -E -v "(lo)" | sed "s/:$//"', $networkInterfaces); foreach ($networkInterfaces as $interface) {
if ($interface->ifname != 'lo') {
$wirelessOption = [];
foreach ($networkInterfaces as $interface) foreach ($interface->addr_info as $addr) {
$shell_string .= '/sbin/ifconfig ' . $interface . (($networkInterfaces[count($networkInterfaces) - 1] != $interface) ? ' && echo "-#-" && ' : ''); if ($addr->family == "inet")
$ipAddress = $addr->local;
$streamInterfaces = explode('-#-', shell_exec($shell_string));
foreach ($streamInterfaces as $index => $streamInterface) {
$wirelessOption = '';
$interface = $networkInterfaces[$index];
preg_match('#inet(?:[ ]+(?:addr\:)?)([\w\.]+)#', $streamInterface, $ipAddress);
preg_match('#(?:HWaddr|ether) ([\w\:]+)#', $streamInterface, $macAddress);
preg_match('#RX(?:.*?)bytes[:| ]([\d]+)#', $streamInterface, $bytesReceived);
preg_match('#TX(?:.*?)bytes[:| ]([\d]+)#', $streamInterface, $bytesSent);
preg_match('#RX(?:.*?)packets[:| ]([\d]+)#', $streamInterface, $packetsReceived);
preg_match('#TX(?:.*?)packets[:| ]([\d]+)#', $streamInterface, $packetsSent);
$output0 = $interface;
if (substr($output0, 0, 4) == 'wlan') {
$streamWirelessInterface = shell_exec('/sbin/iwconfig ' . $output0);
if (0 == substr_count($streamWirelessInterface, 'Not-Associated')) {
$posConfig_start = @strpos($streamWirelessInterface, 'ESSID:"', 0) + 7;
$posConfig_end = @strpos($streamWirelessInterface, '"', $posConfig_start);
$wirelessOption['ssid'] = trim(substr($streamWirelessInterface, $posConfig_start, ($posConfig_end - $posConfig_start)));
$posConfig_start = @strpos($streamWirelessInterface, 'Access Point:', 0) + 13;
$posConfig_end = @strpos($streamWirelessInterface, 'Bit Rate', $posConfig_start);
$wirelessOption['mac'] = trim(substr($streamWirelessInterface, $posConfig_start, ($posConfig_end - $posConfig_start)));
$posConfig_start = @strpos($streamWirelessInterface, 'Signal level=', 0) + 13;
if (($posConfig_end = @strpos($streamWirelessInterface, '/100', $posConfig_start)) === false) {
$posConfig_end = @strpos($streamWirelessInterface, ' dBm', $posConfig_start);
$signal = trim(substr($streamWirelessInterface, $posConfig_start, ($posConfig_end - $posConfig_start)));
if ($signal <= -100)
$wirelessOption['signal'] = 0;
elseif ($signal >= -50)
$wirelessOption['signal'] = 100;
else
$wirelessOption['signal'] = 2 * ($signal + 100);
} else
$wirelessOption['signal'] = trim(substr($streamWirelessInterface, $posConfig_start, ($posConfig_end - $posConfig_start)));
} }
} $macAddress = $interface->address;
$bytesReceived = 0;
$bytesSent = 0;
$packetsReceived = 0;
$packetsSent = 0;
$output[] = array('interface' => $interface, 'mac' => strtoupper($macAddress[1]), 'ip' => (isset($ipAddress[1])) ? $ipAddress[1] : NULL, 'sent' => $bytesSent[1], 'receive' => $bytesReceived[1], 'option' => $wirelessOption, 'packets' => array('sent' => $packetsSent[1], 'received' => $packetsReceived[1])); if (substr($interface->ifname, 0, 4) == 'wlan') {
$streamWirelessInterface = shell_exec('/sbin/iwconfig ' . $interface->ifname);
if (0 == substr_count($streamWirelessInterface, 'Not-Associated')) {
$posConfig_start = @strpos($streamWirelessInterface, 'ESSID:"', 0) + 7;
$posConfig_end = @strpos($streamWirelessInterface, '"', $posConfig_start);
$wirelessOption['ssid'] = trim(substr($streamWirelessInterface, $posConfig_start, ($posConfig_end - $posConfig_start)));
$posConfig_start = @strpos($streamWirelessInterface, 'Access Point:', 0) + 13;
$posConfig_end = @strpos($streamWirelessInterface, 'Bit Rate', $posConfig_start);
$wirelessOption['mac'] = trim(substr($streamWirelessInterface, $posConfig_start, ($posConfig_end - $posConfig_start)));
$posConfig_start = @strpos($streamWirelessInterface, 'Signal level=', 0) + 13;
if (($posConfig_end = @strpos($streamWirelessInterface, '/100', $posConfig_start)) === false) {
$posConfig_end = @strpos($streamWirelessInterface, ' dBm', $posConfig_start);
$signal = trim(substr($streamWirelessInterface, $posConfig_start, ($posConfig_end - $posConfig_start)));
if ($signal <= -100)
$wirelessOption['signal'] = 0;
elseif ($signal >= -50)
$wirelessOption['signal'] = 100;
else
$wirelessOption['signal'] = 2 * ($signal + 100);
} else
$wirelessOption['signal'] = trim(substr($streamWirelessInterface, $posConfig_start, ($posConfig_end - $posConfig_start)));
}
}
$output[] = array('interface' => $interface->ifname, 'mac' => strtoupper($macAddress), 'ip' => (isset($ipAddress)) ? $ipAddress : NULL, 'sent' => $bytesSent, 'receive' => $bytesReceived, 'option' => $wirelessOption, 'packets' => array('sent' => $packetsSent, 'received' => $packetsReceived));
}
} }
return $output; return $output;