如何获取使用 wget 请求的域的 IP?

How to get IP of a domain requested with wget?

我正在寻找一种方法来获取使用 wget 命令请求的域的 IP 地址,当命令失败时。

我无法使用ping命令获取IP,因为在wget命令终止后地址可能会改变。

我想在 shell 脚本中执行此操作。

wget失败时,它以非零退出状态终止,错误被写入标准错误描述符(2)。

所以你可以检查退出代码($?变量),并解析写入标准错误的字符串:

url='http://whosebug.com/users/edit/1646322'

output=$( wget "$url" 2>&1 )
if [[ $? -ne 0 ]]; then
  printf '%s' "$output" | \
    perl -ne '/^Connecting to .*\|([^\|]+)\|/ and print '
fi

示例输出

151.101.129.69