How can I get all IPv4 and IPv6 external addresses of the server the script is running on?
I have managed to put together a code that uses CLI ifconfig and some regular expressions to get them but I thought there could be a better way.
How can I get all IPv4 and IPv6 external addresses of the server the script is running on?
I have managed to put together a code that uses CLI ifconfig and some regular expressions to get them but I thought there could be a better way.
function get_public_ips($eth = 'eth0') {
$ipv4 = array_filter(explode(';', preg_replace('/\s/', ';', shell_exec("ip -o -4 addr list " . $eth . " | awk '{print $4}' | cut -d/ -f1"))), 'trim');
$ipv6 = array_filter(explode(';', preg_replace(Array('/([a-z0-9:]+).*/', '/\s/'), Array('$1', ';'), shell_exec("ifconfig " . $eth . " | grep -Ev ':Link' | awk '/inet6/{print $3}'"))), 'trim');
sort($ipv4);
sort($ipv6);
return Array('ipv4' => $ipv4, 'ipv6' => $ipv6);
}