我经常遇到这样的问题:我有一个 URL,并且想要 ping 该地址的域。
是否有一个选项允许 ping 仅使用作为参数给出的 URL 的域部分?
答案1
你可以在 bash 中完成所有这些转换,参数扩展:
pingd() (
domain=$1
# allow 'http' or 'www' to match any upper- or lower-case versions
# (bash version 4.4-alpha and later)
shopt -s nocasematch
# strip any leading http(s)
domain=${domain/#http:\/\/}
domain=${domain/#https:\/\/}
# strip any leading user:pass@
domain=${domain/#*:*@/}
# strip the trailing slash onwards
domain=${domain/%\/*/}
# strip any trailing port
domain=${domain/%:*/}
# strip any remaining leading www
domain=${domain/#www./}
ping "$domain"
)
示例(用echo ping
代替ping
来演示):
$ pingd 'http://user:[email protected]:80/some/path/url?data&and#hash'
ping example.com
$ pingd 'http://www.stackexchange.com'
ping stackexchange.com
$ pingd 'http://www.stackexchange.com/help'
ping stackexchange.com
$ pingd 'https://www.stackexchange.com'
ping stackexchange.com
$ pingd 'https://unix.stackexchange.com'
ping unix.stackexchange.com
$ pingd 'https://unix.stackexchange.com/'
ping unix.stackexchange.com
$ pingd 'https://unix.stackexchange.com/questions/428989/allow-full-urls-starting-with-http-https-or-www-in-ping'
ping unix.stackexchange.com
$ pingd 'www.example.com'
ping example.com
答案2
获取不带“http://”的域部分的快速解决方案是在 Firefox 中设置browser.urlbar.trimURLs
为,这样您就可以更轻松地仅复制 URL 的域部分。false
about:config
pingd
但是您可以将这样的功能添加到您的~/.bashrc
:
function pingd {
ping $(echo "$1" | sed -e "s/[^/]*\/\/\([^@]*@\)\?\([^:/]*\).*/\2/" | sed "s/^www\.//")
}
适用于:
pingd "http://user:[email protected]:80/some/path/url?data&and#hash"
PING 示例.com
...
如果 url 不包含&
也不!
包含引号也不行