我想要做的事情如下所示。我有一个网址,比如说
http://www.google.com/one/two/three
我需要提取主域名"www.google.com",将其提供给nslookup (因为nslookup/dig似乎不能处理完整的URL),然后用解析的IP地址替换URL。
$ echo "http://www.google.com/one/two/three" | sed "s///g"
$ www.google.com问题是"http://“可能并不总是存在的。然后
$ echo "http://www.google.com/one/two/three" | sed "s///g"
$ http://11.22.33.44/one/two/three谁能提供任何相关的链接或相关的例子?
发布于 2013-08-07 02:09:08
尝试这个sed命令:
echo "http://www.google.com/one/two/three" | sed -r 's#(https?://)?([^/]+).*#\2#'输出:
www.google.com获取IP地址后:
$> IP="11.22.33.44"
$> echo "https://www.google.com/one/two/three" | sed -r "s#(https?://)?([^/]+)(.*)#\1$IP\3#"
https://11.22.33.44/one/two/three这将适用于http://,https://或者没有任何https?://在URL之前。
发布于 2013-08-07 03:18:44
由于URL可能会变得杂乱无章(“http://user:pass@www.example.com:8080/one/two/three")在一般情况下,我建议使用带有URI解析库的语言。
url="http://stackoverflow.com/questions/18087200/replace-part-of-url-with-ip-adress"
newurl=$(perl -MURI -le '
$uri = URI->new(shift);
$cmd = "nslookup " . $uri->host;
$out = qx($cmd);
if ($out =~ /Name:.*?\KAddress:\s+([\d.]+)/s) {
$ip = $1;
$uri->host($ip);
print $uri
} else {warn "cannot find IP address in nslookup output"}
' "$url")
echo "$newurl"输出
http://198.252.206.16/questions/18087200/replace-part-of-url-with-ip-adresshttps://stackoverflow.com/questions/18087200
复制相似问题