用 IP 地址替换部分 URL [英] Replace part of URL with IP adress

查看:77
本文介绍了用 IP 地址替换部分 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是下面的事情.我有一个网址,比如 http://www.google.com/one/two/three一个>

What I want to do is something like below. I have a URL, say http://www.google.com/one/two/three

我需要提取主域名www.google.com",将其提供给 nslookup(因为 nslookup/dig 似乎不适用于完整 URL),然后将 URL 替换为已解析的 IP 地址.例如

I need to extract the main domain name "www.google.com", to feed it to nslookup (As nslookup/dig does not seem to work with full URL) and then replace the URL with resolved IP address.e.g.

$ echo "http://www.google.com/one/two/three" | sed "s/<pattern>//g" 
$ www.google.com

问题是http://"可能并不总是存在.然后

The problem is that "http://" may not always be there. And then

$ echo "http://www.google.com/one/two/three" | sed "s/<pattern>//g" 
$ http://11.22.33.44/one/two/three

谁能提供任何相关链接或相关示例?

Can anyone provide any related link or related examples ?

推荐答案

试试这个 sed 命令:

Try this sed command:

echo "http://www.google.com/one/two/three" | sed -r 's#(https?://)?([^/]+).*#\2#'

输出:

www.google.com

当您获取 IP 地址时:

And when you have fetched IP address:

$> 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:// 或在 URL 前没有任何 https?://.

This will work with http://, https:// or without any https?:// before the URL.

这篇关于用 IP 地址替换部分 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆