From 25f362dc1937ae3c4f80e3bfa082d77816cfafe2 Mon Sep 17 00:00:00 2001 From: D9ping Date: Fri, 19 Oct 2018 23:36:40 +0200 Subject: [PATCH 1/3] Use ip and iw instead of ipconfig and iwconfig. #152 #249 Signed-off-by: D9ping --- includes/dashboard.php | 229 +++++++++++++++++++++++++++++------------ 1 file changed, 163 insertions(+), 66 deletions(-) diff --git a/includes/dashboard.php b/includes/dashboard.php index e39c246c..1d8887f9 100755 --- a/includes/dashboard.php +++ b/includes/dashboard.php @@ -1,74 +1,140 @@ addMessage(_('Interface name invalid.'), 'danger'); + $status->showMessages(); + return; } - preg_match_all( '/[0-9.]+\/([0-3][0-9])/i',$strWlan0,$result ) || $result[1] = 'No Subnet Mask Found'; - $strNetMask = ''; - foreach($result[1] as $netmask) { - $strNetMask .= long2ip(-1 << (32 -(int)$netmask))." "; - } - preg_match( '/RX packets:(\d+)/',$strWlan0,$result ) || $result[1] = 'No Data'; - $strRxPackets = $result[1]; - preg_match( '/TX packets:(\d+)/',$strWlan0,$result ) || $result[1] = 'No Data'; - $strTxPackets = $result[1]; - preg_match( '/RX bytes:(\d+ \(\d+.\d+ [K|M|G]iB\))/i',$strWlan0,$result ) || $result[1] = 'No Data'; - $strRxBytes = $result[1]; - preg_match( '/TX Bytes:(\d+ \(\d+.\d+ [K|M|G]iB\))/i',$strWlan0,$result ) || $result[1] = 'No Data'; - $strTxBytes = $result[1]; - preg_match( '/ESSID:\"([a-zA-Z0-9\s].+)\"/i',$strWlan0,$result ) || $result[1] = 'Not connected'; - $strSSID = str_replace( '"','',$result[1] ); - preg_match( '/Access Point: ([0-9a-f:]+)/i',$strWlan0,$result ) || $result[1] = ''; - $strBSSID = $result[1]; - preg_match( '/Bit Rate=([0-9\.]+ Mb\/s)/i',$strWlan0,$result ) || $result[1] = ''; - $strBitrate = $result[1]; - preg_match( '/Tx-Power=([0-9]+ dBm)/i',$strWlan0,$result ) || $result[1] = ''; - $strTxPower = $result[1]; - preg_match( '/Link Quality=([0-9]+)/i',$strWlan0,$result ) || $result[1] = ''; - $strLinkQuality = $result[1]; - preg_match( '/Signal level=(-?[0-9]+ dBm)/i',$strWlan0,$result ) || $result[1] = ''; - $strSignalLevel = $result[1]; - preg_match('/Frequency:(\d+.\d+ GHz)/i',$strWlan0,$result) || $result[1] = ''; - $strFrequency = $result[1]; - if(strpos( $strWlan0, "UP" ) !== false) { - $status->addMessage('Interface is up', 'success'); - $wlan0up = true; - } else { - $status->addMessage('Interface is down', 'warning'); + if (!function_exists('exec')) { + $status->addMessage(_('Required exec function is disabled. Check if exec is not added to php disable_functions.'), 'danger'); + $status->showMessages(); + return; } + exec('ip a show '.RASPI_WIFI_CLIENT_INTERFACE, $stdoutIp); + $stdoutIpAllLinesGlued = implode(" ", $stdoutIp); + $stdoutIpWRepeatedSpaces = preg_replace('/\s\s+/', ' ', $stdoutIpAllLinesGlued); + + preg_match('/link\/ether ([0-9a-f:]+)/i', $stdoutIpWRepeatedSpaces, $matchesMacAddr ) || $matchesMacAddr[1] = 'No MAC Address Found'; + $macAddr = $matchesMacAddr[1]; + + preg_match('/inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(([0-3][0-9]))/i', $stdoutIpWRepeatedSpaces, $matchesIpv4AddrAndSubnet) || $matchesIpv4Addr[1] = 'No IP Address Found'; + $ipv4Addr = $matchesIpv4AddrAndSubnet[1]; + $strNetMask = long2ip(-1 << (32 -(int)$matchesIpv4AddrAndSubnet[2])); + // TODO multiple ipv4 addresses + + preg_match('/inet6 ([a-f0-9:]+)/i', $stdoutIpWRepeatedSpaces, $matchesIpv4Addr ) || $matchesIpv6Addr[1] = 'No IPv6 Address Found'; + $ipv6Addr = $matchesIpv6Addr[1]; + // TODO multiple ipv6 addresses + + preg_match('/state (UP|DOWN)/i', $stdoutIpWRepeatedSpaces, $matchesState ) || $matchesState[1] = 'unknown'; + $deviceState = $matchesState[1]; + + // Because of table layout used in the ip output we get the interface statistics directly from + // the system. One advantage of this is that it could work when interface is disable. + exec('cat /sys/class/net/'.RASPI_WIFI_CLIENT_INTERFACE.'/statistics/rx_packets ', $stdoutCatRxPackets); + $strRxPackets = 'No data'; + if (ctype_digit($stdoutCatRxPackets[0])) { + $strRxPackets = $stdoutCatRxPackets[0]; + } + + exec('cat /sys/class/net/'.RASPI_WIFI_CLIENT_INTERFACE.'/statistics/tx_packets ', $stdoutCatTxPackets); + $strTxPackets = 'No data'; + if (ctype_digit($stdoutCatTxPackets[0])) { + $strTxPackets = $stdoutCatTxPackets[0]; + } + + exec('cat /sys/class/net/'.RASPI_WIFI_CLIENT_INTERFACE.'/statistics/rx_bytes ', $stdoutCatRxBytes); + $strRxBytes = 'No data'; + if (ctype_digit($stdoutCatRxBytes[0])) { + $strRxBytes = $stdoutCatRxBytes[0]; + $strRxBytes .= getHumanReadableDatasize($strRxBytes); + } + + exec('cat /sys/class/net/'.RASPI_WIFI_CLIENT_INTERFACE.'/statistics/tx_bytes ', $stdoutCatTxBytes); + $strTxBytes = 'No data'; + if (ctype_digit($stdoutCatTxBytes[0])) { + $strTxBytes = $stdoutCatTxBytes[0]; + $strTxBytes .= getHumanReadableDatasize($strTxBytes); + } + + define('SSIDMAXLEN', 32); + // Warning iw comes with: "Do NOT screenscrape this tool, we don't consider its output stable." + exec('iw dev '.RASPI_WIFI_CLIENT_INTERFACE.' link ', $stdoutIw); + $stdoutIwAllLinesGlued = implode(' ', $stdoutIw); + $stdoutIwWRepSpaces = preg_replace('/\s\s+/', ' ', $stdoutIwAllLinesGlued); + + preg_match('/Connected to (([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2}))/', $stdoutIwWRepSpaces, $matchesBSSID) || $matchesBSSID[1] = ''; + $connectedBSSID = $matchesBSSID[1]; + + $wlanHasLink = $deviceState; + if (!preg_match('/SSID: ([^ ]{1,'.SSIDMAXLEN.'})/', $stdoutIwWRepSpaces, $matchesSSID)) { + $wlanHasLink = false; + $matchesSSID[1] = 'Not connected'; + } + + $connectedSSID = $matchesSSID[1]; + + preg_match('/freq: (\d+)/i', $stdoutIwWRepSpaces, $matchesFrequency) || $matchesFrequency[1] = ''; + $frequency = $matchesFrequency[1].' MHz'; + + preg_match('/signal: (-?[0-9]+ dBm)/i', $stdoutIwWRepSpaces, $matchesSignal) || $matchesSignal[1] = ''; + $signalLevel = $matchesSignal[1]; + + preg_match('/tx bitrate: ([0-9\.]+ [KMGT]?Bit\/s)/', $stdoutIwWRepSpaces, $matchesBitrate) || $matchesBitrate[1] = ''; + $bitrate = $matchesBitrate[1]; + + // txpower is now displayed on iw dev(..) info command, not on link command. + exec('iw dev '.RASPI_WIFI_CLIENT_INTERFACE.' info ', $stdoutIwInfo); + $stdoutIwInfoAllLinesGlued = implode(' ', $stdoutIwInfo); + $stdoutIpInfoWRepSpaces = preg_replace('/\s\s+/', ' ', $stdoutIwInfoAllLinesGlued); + + preg_match('/txpower ([0-9\.]+ dBm)/i', $stdoutIpInfoWRepSpaces, $matchesTxPower ) || $matchesTxPower[1] = ''; + $strTxPower = $matchesTxPower[1]; + + // iw does not have the "Link Quality". This is a is an aggregate value, + // and depends on the driver and hardware. + // Display link quality as signal quality for now. + $strLinkQuality = 0; + if ($signalLevel > -100 && $wlanHasLink) { + if ($signalLevel >= 0) { + $strLinkQuality = 100; + } else { + $strLinkQuality = 100 + $signalLevel; + } + } + + $wlan0up = false; + $classMsgDevicestatus = 'warning'; + if ($deviceState === 'UP') { + $wlan0up = true; + $classMsgDevicestatus = 'success'; + } + + $status->addMessage(sprintf(_('Interface is %s.'), strtolower($deviceState)), $classMsgDevicestatus); + if( isset($_POST['ifdown_wlan0']) ) { - exec( 'ifconfig ' . RASPI_WIFI_CLIENT_INTERFACE . ' | grep -i running | wc -l',$test ); + // Pressed stop button + exec( 'ifconfig '.RASPI_WIFI_CLIENT_INTERFACE.' | grep -i running | wc -l', $test ); if($test[0] == 1) { - exec( 'sudo ip link set ' . RASPI_WIFI_CLIENT_INTERFACE . ' down',$return ); + exec( 'sudo ip link set '.RASPI_WIFI_CLIENT_INTERFACE.' down' ); } else { echo 'Interface already down'; } } elseif( isset($_POST['ifup_wlan0']) ) { - exec( 'ifconfig ' . RASPI_WIFI_CLIENT_INTERFACE . ' | grep -i running | wc -l',$test ); + // Pressed start button + exec('ifconfig '.RASPI_WIFI_CLIENT_INTERFACE.' | grep -i running | wc -l', $test); if($test[0] == 0) { - exec( 'sudo ip link set ' . RASPI_WIFI_CLIENT_INTERFACE . ' up',$return ); - exec( 'sudo ip -s a f label ' . RASPI_WIFI_CLIENT_INTERFACE,$return); + exec('sudo ip link set ' . RASPI_WIFI_CLIENT_INTERFACE . ' up'); + exec('sudo ip -s a f label ' . RASPI_WIFI_CLIENT_INTERFACE); } else { echo 'Interface already up'; } @@ -86,15 +152,15 @@ function DisplayDashboard(){


-

-

-


- +

+

+

+


-

-


+

+



-

+

@@ -102,12 +168,12 @@ function DisplayDashboard(){

-

-

-

-

+

+

+

+


-


+


- +
= $pib) { + $humanDatasize = ' ('.round($numbytes / $pib, $precision).' PB)'; + } elseif ($numbytes >= $tib) { + $humanDatasize = ' ('.round($numbytes / $tib, $precision).' TB)'; + } elseif ($numbytes >= $gib) { + $humanDatasize = ' ('.round($numbytes / $gib, $precision).' GB)'; + } elseif ($numbytes >= $mib) { + $humanDatasize = ' ('.round($numbytes / $mib, $precision).' MB)'; + } elseif ($numbytes >= $kib) { + $humanDatasize = ' ('.round($numbytes / $kib, $precision).' KB)'; + } + + return $humanDatasize; +} + From f81bae5f3517bf556413cc88026712af24b4ed1d Mon Sep 17 00:00:00 2001 From: D9ping Date: Sat, 20 Oct 2018 14:38:27 +0200 Subject: [PATCH 2/3] Don't use ifconfig for checking interface state. Made several strings translatable in dashboard. Signed-off-by: D9ping --- includes/dashboard.php | 61 +++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/includes/dashboard.php b/includes/dashboard.php index 1d8887f9..d94416c1 100755 --- a/includes/dashboard.php +++ b/includes/dashboard.php @@ -23,44 +23,44 @@ function DisplayDashboard(){ $stdoutIpAllLinesGlued = implode(" ", $stdoutIp); $stdoutIpWRepeatedSpaces = preg_replace('/\s\s+/', ' ', $stdoutIpAllLinesGlued); - preg_match('/link\/ether ([0-9a-f:]+)/i', $stdoutIpWRepeatedSpaces, $matchesMacAddr ) || $matchesMacAddr[1] = 'No MAC Address Found'; + preg_match('/link\/ether ([0-9a-f:]+)/i', $stdoutIpWRepeatedSpaces, $matchesMacAddr ) || $matchesMacAddr[1] = _('No MAC Address Found'); $macAddr = $matchesMacAddr[1]; - preg_match('/inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(([0-3][0-9]))/i', $stdoutIpWRepeatedSpaces, $matchesIpv4AddrAndSubnet) || $matchesIpv4Addr[1] = 'No IP Address Found'; + preg_match('/inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(([0-3][0-9]))/i', $stdoutIpWRepeatedSpaces, $matchesIpv4AddrAndSubnet) || $matchesIpv4Addr[1] = _('No IPv4 Address Found'); $ipv4Addr = $matchesIpv4AddrAndSubnet[1]; - $strNetMask = long2ip(-1 << (32 -(int)$matchesIpv4AddrAndSubnet[2])); + $ipv4Netmask = long2ip(-1 << (32 -(int)$matchesIpv4AddrAndSubnet[2])); // TODO multiple ipv4 addresses - preg_match('/inet6 ([a-f0-9:]+)/i', $stdoutIpWRepeatedSpaces, $matchesIpv4Addr ) || $matchesIpv6Addr[1] = 'No IPv6 Address Found'; + preg_match('/inet6 ([a-f0-9:]+)/i', $stdoutIpWRepeatedSpaces, $matchesIpv4Addr ) || $matchesIpv6Addr[1] = _('No IPv6 Address Found'); $ipv6Addr = $matchesIpv6Addr[1]; // TODO multiple ipv6 addresses preg_match('/state (UP|DOWN)/i', $stdoutIpWRepeatedSpaces, $matchesState ) || $matchesState[1] = 'unknown'; - $deviceState = $matchesState[1]; + $interfaceState = $matchesState[1]; // Because of table layout used in the ip output we get the interface statistics directly from // the system. One advantage of this is that it could work when interface is disable. exec('cat /sys/class/net/'.RASPI_WIFI_CLIENT_INTERFACE.'/statistics/rx_packets ', $stdoutCatRxPackets); - $strRxPackets = 'No data'; + $strRxPackets = _('No data'); if (ctype_digit($stdoutCatRxPackets[0])) { $strRxPackets = $stdoutCatRxPackets[0]; } exec('cat /sys/class/net/'.RASPI_WIFI_CLIENT_INTERFACE.'/statistics/tx_packets ', $stdoutCatTxPackets); - $strTxPackets = 'No data'; + $strTxPackets = _('No data'); if (ctype_digit($stdoutCatTxPackets[0])) { $strTxPackets = $stdoutCatTxPackets[0]; } exec('cat /sys/class/net/'.RASPI_WIFI_CLIENT_INTERFACE.'/statistics/rx_bytes ', $stdoutCatRxBytes); - $strRxBytes = 'No data'; + $strRxBytes = _('No data'); if (ctype_digit($stdoutCatRxBytes[0])) { $strRxBytes = $stdoutCatRxBytes[0]; $strRxBytes .= getHumanReadableDatasize($strRxBytes); } exec('cat /sys/class/net/'.RASPI_WIFI_CLIENT_INTERFACE.'/statistics/tx_bytes ', $stdoutCatTxBytes); - $strTxBytes = 'No data'; + $strTxBytes = _('No data'); if (ctype_digit($stdoutCatTxBytes[0])) { $strTxBytes = $stdoutCatTxBytes[0]; $strTxBytes .= getHumanReadableDatasize($strTxBytes); @@ -75,7 +75,11 @@ function DisplayDashboard(){ preg_match('/Connected to (([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2}))/', $stdoutIwWRepSpaces, $matchesBSSID) || $matchesBSSID[1] = ''; $connectedBSSID = $matchesBSSID[1]; - $wlanHasLink = $deviceState; + $wlanHasLink = false; + if ($interfaceState === 'UP') { + $wlanHasLink = true; + } + if (!preg_match('/SSID: ([^ ]{1,'.SSIDMAXLEN.'})/', $stdoutIwWRepSpaces, $matchesSSID)) { $wlanHasLink = false; $matchesSSID[1] = 'Not connected'; @@ -98,7 +102,7 @@ function DisplayDashboard(){ $stdoutIpInfoWRepSpaces = preg_replace('/\s\s+/', ' ', $stdoutIwInfoAllLinesGlued); preg_match('/txpower ([0-9\.]+ dBm)/i', $stdoutIpInfoWRepSpaces, $matchesTxPower ) || $matchesTxPower[1] = ''; - $strTxPower = $matchesTxPower[1]; + $txPower = $matchesTxPower[1]; // iw does not have the "Link Quality". This is a is an aggregate value, // and depends on the driver and hardware. @@ -114,30 +118,39 @@ function DisplayDashboard(){ $wlan0up = false; $classMsgDevicestatus = 'warning'; - if ($deviceState === 'UP') { + if ($interfaceState === 'UP') { $wlan0up = true; $classMsgDevicestatus = 'success'; } - $status->addMessage(sprintf(_('Interface is %s.'), strtolower($deviceState)), $classMsgDevicestatus); - if( isset($_POST['ifdown_wlan0']) ) { + if (isset($_POST['ifdown_wlan0'])) { // Pressed stop button - exec( 'ifconfig '.RASPI_WIFI_CLIENT_INTERFACE.' | grep -i running | wc -l', $test ); - if($test[0] == 1) { + if ($interfaceState === 'UP') { + $status->addMessage(sprintf(_('Interface is going %s.'), _('down')), 'warning'); exec( 'sudo ip link set '.RASPI_WIFI_CLIENT_INTERFACE.' down' ); + $wlan0up = false; + $status->addMessage(sprintf(_('Interface is now %s.'), _('down')), 'success'); + } elseif ($interfaceState === 'unknown') { + $status->addMessage(_('Interface state unknown.'), 'danger'); } else { - echo 'Interface already down'; + $status->addMessage(sprintf(_('Interface already %s.'), _('down')), 'warning'); } } elseif( isset($_POST['ifup_wlan0']) ) { // Pressed start button - exec('ifconfig '.RASPI_WIFI_CLIENT_INTERFACE.' | grep -i running | wc -l', $test); - if($test[0] == 0) { + if ($interfaceState === 'DOWN') { + $status->addMessage(sprintf(_('Interface is going %s.'), _('up')), 'warning'); exec('sudo ip link set ' . RASPI_WIFI_CLIENT_INTERFACE . ' up'); exec('sudo ip -s a f label ' . RASPI_WIFI_CLIENT_INTERFACE); + $wlan0up = true; + $status->addMessage(sprintf(_('Interface is now %s.'), _('up')), 'success'); + } elseif ($interfaceState === 'unknown') { + $status->addMessage(_('Interface state unknown.'), 'danger'); } else { - echo 'Interface already up'; + $status->addMessage(sprintf(_('Interface already %s.'), _('up')), 'warning'); } + } else { + $status->addMessage(sprintf(_('Interface is %s.'), strtolower($interfaceState)), $classMsgDevicestatus); } ?>
@@ -153,7 +166,7 @@ function DisplayDashboard(){



-

+




@@ -172,7 +185,7 @@ function DisplayDashboard(){



-

+



@@ -191,9 +204,9 @@ function DisplayDashboard(){
'; + echo ''; } else { - echo ''; + echo ''; } ?> " onclick="document.location.reload(true)" /> From 6b05989498e93fc3ed63b827b31ae1cd123736ad Mon Sep 17 00:00:00 2001 From: D9ping Date: Tue, 23 Oct 2018 11:57:55 +0200 Subject: [PATCH 3/3] Showing multiple IPv4/IPv6 addresses. Signed-off-by: D9ping --- includes/dashboard.php | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/includes/dashboard.php b/includes/dashboard.php index d94416c1..2469f017 100755 --- a/includes/dashboard.php +++ b/includes/dashboard.php @@ -26,14 +26,36 @@ function DisplayDashboard(){ preg_match('/link\/ether ([0-9a-f:]+)/i', $stdoutIpWRepeatedSpaces, $matchesMacAddr ) || $matchesMacAddr[1] = _('No MAC Address Found'); $macAddr = $matchesMacAddr[1]; - preg_match('/inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(([0-3][0-9]))/i', $stdoutIpWRepeatedSpaces, $matchesIpv4AddrAndSubnet) || $matchesIpv4Addr[1] = _('No IPv4 Address Found'); - $ipv4Addr = $matchesIpv4AddrAndSubnet[1]; - $ipv4Netmask = long2ip(-1 << (32 -(int)$matchesIpv4AddrAndSubnet[2])); - // TODO multiple ipv4 addresses + $ipv4Addrs = ''; + $ipv4Netmasks = ''; + if (!preg_match_all('/inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/([0-3][0-9])/i', $stdoutIpWRepeatedSpaces, $matchesIpv4AddrAndSubnet)) { + $ipv4Addrs = _('No IPv4 Address Found'); + } else { + $numMatchesIpv4AddrAndSubnet = count($matchesIpv4AddrAndSubnet); + for ($i = 1; $i < $numMatchesIpv4AddrAndSubnet; $i += 2) { + if ($i > 2) { + $ipv4Netmasks .= ' '; + $ipv4Addrs .= ' '; + } - preg_match('/inet6 ([a-f0-9:]+)/i', $stdoutIpWRepeatedSpaces, $matchesIpv4Addr ) || $matchesIpv6Addr[1] = _('No IPv6 Address Found'); - $ipv6Addr = $matchesIpv6Addr[1]; - // TODO multiple ipv6 addresses + $ipv4Addrs .= $matchesIpv4AddrAndSubnet[$i][0]; + $ipv4Netmasks .= long2ip(-1 << (32 -(int)$matchesIpv4AddrAndSubnet[$i+1][0])); + } + } + + $ipv6Addrs = ''; + if (!preg_match_all('/inet6 ([a-f0-9:]+)/i', $stdoutIpWRepeatedSpaces, $matchesIpv6Addr)) { + $ipv6Addrs = _('No IPv6 Address Found'); + } else { + $numMatchesIpv6Addr = count($matchesIpv6Addr); + for ($i = 1; $i < $numMatchesIpv6Addr; ++$i) { + if ($i > 1) { + $ipv6Addrs .= ' '; + } + + $ipv6Addrs .= $matchesIpv6Addr[$i]; + } + } preg_match('/state (UP|DOWN)/i', $stdoutIpWRepeatedSpaces, $matchesState ) || $matchesState[1] = 'unknown'; $interfaceState = $matchesState[1]; @@ -165,9 +187,9 @@ function DisplayDashboard(){


-

-

-

+

+

+