Linux Shell 获取域名的ip地址
标签:
linuxshelldig域名ipgethostbyname |
有时候已知域名,需要得到其所对应的ip地址,php中的gethostbyname()函数可以很轻松的完成这个功能,但是其局限性在于只能返回一个ip地址,有很多时候一个域名是对应多个ip的,比如一些大网站(百度、新浪等),(注:php中的gethostbynamel()函数可以返回一个域名的ip列表,以数组形式)本文的方法基于linux中的dig命令(具体用法可百度之),可以得到域名对应的多个ip(如果有的话)。代码如下:
http://s16/mw690/001KAiaGgy6KRpED6tFaf&690Shell 获取域名的ip地址" TITLE="Linux Shell 获取域名的ip地址" />
#author: ZHANGCHENG
#2014/07/30
#!/bin/bash
function gethostip(){
result=`dig +short $1 | awk 'NR==1{print $0}'`
url=$1
#echo $result
num=`echo $result | grep -E
"^(([0-2]*[0-9]+[0-9]+)\.([0-2]*[0-9]+[0-9]+)\.([0-2]*[0-9]+[0-9]+)\.([0-2]*[0-9]+[0-9]+))$"
| wc -l`
#echo $num
if [ $num -eq 0 ];then
#echo "This is CNAME"
gethostip $result
else
dig +short $url
fi
}
gethostip $1
文件名为gethostip.sh
运行:./gethostip.sh www.baidu.com

加载中…