Revision aa5acb42
Added by Ermal LUÇI almost 11 years ago
etc/inc/interfaces.inc | ||
---|---|---|
4443 | 4443 |
|
4444 | 4444 |
/* Guess the physical interface by providing a IP address */ |
4445 | 4445 |
function guess_interface_from_ip($ipaddress) { |
4446 |
if(! is_ipaddr($ipaddress)) { |
|
4447 |
return false; |
|
4448 |
} |
|
4449 |
if(is_ipaddrv4($ipaddress)) { |
|
4450 |
/* create a route table we can search */ |
|
4451 |
exec("/usr/bin/netstat -rnWf inet", $output, $ret); |
|
4452 |
foreach($output as $line) { |
|
4453 |
if(preg_match("/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\/[0-9]+[ ]+link[#]/", $line)) { |
|
4454 |
$fields = preg_split("/[ ]+/", $line); |
|
4455 |
if(ip_in_subnet($ipaddress, $fields[0])) { |
|
4456 |
return $fields[5]; |
|
4457 |
} |
|
4458 |
} |
|
4459 |
} |
|
4460 |
} |
|
4461 |
/* FIXME: This works from cursory testing, regexp might need fine tuning */ |
|
4462 |
if(is_ipaddrv6($ipaddress)) { |
|
4463 |
/* create a route table we can search */ |
|
4464 |
exec("/usr/bin/netstat -rnWf inet6", $output, $ret); |
|
4465 |
foreach($output as $line) { |
|
4466 |
if(preg_match("/[0-9a-f]+[:]+[0-9a-f]+[:]+[\/][0-9]+/", $line)) { |
|
4467 |
$fields = preg_split("/[ ]+/", $line); |
|
4468 |
if(ip_in_subnet($ipaddress, $fields[0])) { |
|
4469 |
return $fields[5]; |
|
4470 |
} |
|
4471 |
} |
|
4472 |
} |
|
4473 |
} |
|
4474 |
$ret = exec_command("/sbin/route -n get {$ipaddress} | /usr/bin/awk '/interface/ { print \$2; };'"); |
|
4475 |
if(empty($ret)) { |
|
4446 |
|
|
4447 |
$family = ''; |
|
4448 |
if(is_ipaddrv4($ipaddress)) |
|
4449 |
$family = 'inet'; |
|
4450 |
if (empty($family) && is_ipaddrv6($ipaddress)) |
|
4451 |
$family = 'inet6'; |
|
4452 |
|
|
4453 |
if (empty($family)) |
|
4476 | 4454 |
return false; |
4477 |
} |
|
4478 |
return $ret; |
|
4455 |
|
|
4456 |
/* create a route table we can search */ |
|
4457 |
$output = ''; |
|
4458 |
$_gb = exec("/sbin/route -n get -{$family} " . escapeshellarg($ipaddress) . " | /usr/bin/awk '/interface/ { print \$2; };'", $output); |
|
4459 |
$output[0] = trim($output[0], " \n"); |
|
4460 |
if (!empty($output[0])) |
|
4461 |
return $output[0]; |
|
4462 |
|
|
4463 |
return false; |
|
4479 | 4464 |
} |
4480 | 4465 |
|
4481 | 4466 |
/* |
Also available in: Unified diff
Use route command directly rather than trying to make a route search on php thorugh netstat. It Fixes #4000