获取所有服务器IP地址


Get all server IP addresses

如何获取脚本运行服务器的所有IPv4和IPv6外部地址?

我已经设法编写了一个使用CLI-ifconfig和一些正则表达式的代码来获取它们,但我认为还有更好的方法。

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);
}