如何获取wget请求的域名的IP?

时间:2016-11-14 04:58:51

标签: shell ip wget

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

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

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

1 个答案:

答案 0 :(得分:1)

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

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

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

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

示例输出

151.101.129.69