raspap-webgui-mirror/includes/internetRoute.php

69 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2020-06-25 13:54:56 +00:00
<?php
/*
* Fetches details of the kernel routing table
*
* @param boolean $checkAccesss Perform connectivity test
* @return string
*/
function getRouteInfo($checkAccess)
{
$rInfo = array();
// get all default routes
exec('ip route list | sed -rn "s/default via (\b([0-9]{1,3}\.){3}[0-9]{1,3}).*dev (\w*)(.*((\b([0-9]{1,3}\.){3}[0-9]{1,3})))?/\3 \5 \1/p"', $routes);
2021-03-28 14:35:19 +00:00
$devpat = array("tun", "ppp"); // routing in case of VPN and PPP connection are different
foreach ($devpat as $pat) {
exec('ip route list | grep -oP "'.$pat.'[0-9]" | sort -u', $devs);
}
if (!empty($devs)) {
2021-03-28 14:35:19 +00:00
foreach ($devs as $dev) {
unset($gateway);
unset($ipadd);
exec('ip route list | sed -rn "s/^.*via (([0-9]{1,3}\.){3}[0-9]{1,3}) dev "' . $dev . '".*$/\1/p" | head -n 1', $gateway);
if (empty($gateway)) {
exec('ip route list | sed -rn "s/(([0-9]{1,3}\.){3}[0-9]{1,3}).*dev.*"' . $dev . '".*scope link src.*/\1/p"', $gateway);
}
exec('ifconfig -a | grep -i ' . $dev . ' -A 1 | grep -oP "(?<=inet )([0-9]{1,3}\.){3}[0-9]{1,3}"', $ipadd);
if (!empty($gateway) && !empty($ipadd)) {
$routes[]="$dev $ipadd[0] $gateway[0]";
}
}
}
if (!empty($routes)) {
foreach ($routes as $i => $route) {
$prop = explode(' ', $route);
$rInfo[$i]["interface"] = $prop[0];
$rInfo[$i]["ip-address"] = $prop[1];
$rInfo[$i]["gateway"] = $prop[2];
// resolve the name of the gateway (if possible)
unset($host);
exec('host ' . $prop[2] . ' | sed -rn "s/.*domain name pointer (.*)\./\1/p" | head -n 1', $host);
$rInfo[$i]["gw-name"] = empty($host) ? "*" : $host[0];
if (isset($checkAccess) && $checkAccess) {
// check internet connectivity w/ and w/o DNS resolution
unset($okip);
exec('ping -W1 -c 1 -I ' . $prop[0] . ' ' . RASPI_ACCESS_CHECK_IP . ' | sed -rn "s/.*icmp_seq=1.*time=.*/OK/p"', $okip);
$rInfo[$i]["access-ip"] = empty($okip) ? false : true;
unset($okdns);
exec('ping -W1 -c 1 -I ' . $prop[0] . ' ' . RASPI_ACCESS_CHECK_DNS . ' | sed -rn "s/.*icmp_seq=1.*time=.*/OK/p"', $okdns);
$rInfo[$i]["access-dns"] = empty($okdns) ? false : true;
}
2020-06-25 13:54:56 +00:00
}
} else {
$rInfo = array("error" => "No route to the internet found");
2020-06-28 15:58:42 +00:00
}
return $rInfo;
2020-06-28 15:58:42 +00:00
}
2022-04-21 08:52:09 +00:00
/*
* Fetches raw output of ip route
*
* @return string
*/
function getRouteInfoRaw()
{
exec('ip route list', $routes);
return $routes;
}