目的
Linux下通过shell终端查询某域名的IP地址、通过IP地址查询绑定的域名。并
整理返回结果,创建python工具。
环境
linux + 命令行
工具
1. ping
2. host
3. dig
4. nslookup
工具一:PING --- 简单粗暴
使用ping命令发送一次请求,使用ICMP协议直接与目标通信。只要目标站点存在DNS公网解析,均可以找到域名对应的IP地址。
Ping -c 1 <domain name>
# 例1:ping 存在的域名
[email protected]:~# ping -c 1 baidu.com
PING baidu.com (111.13.101.208) 56(84) bytes of data.
64 bytes from baidu.com (111.13.101.208): icmp_seq=1 ttl=128 time=13.0 ms
--- baidu.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 13.053/13.053/13.053/0.000 ms
# 例二:ping映射到禁用ICMP协议主机上的域名
[email protected]:~# ping imooc.com
PING imooc.com (117.121.101.40) 56(84) bytes of data.
--- imooc.com ping statistics ---
432 packets transmitted, 0 received, 100% packet loss, time 441337ms
# 例二:ping不存在的域名
[email protected]:~# ping ajsdlfjasldfj.com
ping: ajsdlfjasldfj.com: Name or service not known
工具二:host
host是一个简单、单目标、可指定DNS服务器的域名查询工具。默认使用/etc/resolv.conf文件中的DNS服务器查询指定域名的A、AAAA、MX记录。
Usage: host [-aCdilrTvVw] [-c class] [-N ndots] [-t type] [-W time] [-R number]
[-m flag] hostname [server]
# 常用参数:
-a 指定查询所有类型(A、AAAA、MX、SOA等)的记录
-t <type> 设置查询记录的类型
-W <number> 设置查询超时时间
-s 设置遇到SERVFAIL响应时,停止查询
# 示例一 (未指定超时时间,等待6秒后返回结果)
[email protected]:~# host ziroom.com
ziroom.com has address 119.254.76.107
ziroom.com has address 119.254.76.108
ziroom.com has address 119.254.76.106
ziroom.com has address 119.254.83.229
ziroom.com has address 119.254.83.228
Host ziroom.com not found: 2(SERVFAIL)
;; connection timed out; no servers could be reached
# 示例二 (指定遇到SERVFAIL后停止继续查询)
[email protected]:~# host -s ziroom.com
ziroom.com has address 119.254.76.107
Host ziroom.com not found: 2(SERVFAIL)
;; connection timed out; no servers could be reached
# 示例三 (设置超时时间,大概3秒返回结果)
[email protected]:~# host -W 1 ziroom.com
ziroom.com has address 119.254.76.107
Host ziroom.com not found: 2(SERVFAIL)
;; connection timed out; no servers could be reached
# 通过上面三个实验的结果,如果要用在python中,应该使用 host -W 1 <domain name>
工具三:dig
dig是一款灵活、易用、功能强大、支持指定DNS服务器及批量查询的域名查询工具。dig向DNS服务器提交查询并整理展示查询结果。dig默认使用/etc/resolv.conf文件查询指定域名的A、NS记录。
# 快速命令
dig ziroom.com +noadditional +noadflag +nocomments
# 参数解释
# ziroom.com 设置查询的域名
# +noadditional 设置不显示附加结果
# +noadflag 设置不显示验证信息
# +nocomments 设置不显示注释信息
# 示例
[email protected]:~# dig ziroom.com +noadditional +noadflag +nocomments
; <<>> DiG 9.11.2-5-Debian <<>> ziroom.com +noadditional +noadflag +nocomments
;; global options: +cmd
;ziroom.com. IN A
ziroom.com. 5 IN A 119.254.76.107
ziroom.com. 5 IN A 119.254.83.228
ziroom.com. 5 IN A 119.254.76.106
ziroom.com. 5 IN A 119.254.83.229
ziroom.com. 5 IN A 119.254.76.108
ziroom.com. 5 IN NS dns10.hichina.com.
ziroom.com. 5 IN NS dns9.hichina.com.
;; Query time: 26 msec
;; SERVER: 192.168.158.2#53(192.168.158.2)
;; WHEN: Mon Apr 16 06:06:26 EDT 2018
;; MSG SIZE rcvd: 422
工具四:nslookup
一款功能丰富的域名查询工具。直接交互模式、非交互模式,默认查询A记录。
# 快速命令
nslookup -qt A ziroom.com
# 示例一
[email protected]:~# nslookup -qt ziroom.com
*** Invalid option: qt
Server: 192.168.158.2
Address: 192.168.158.2#53
Non-authoritative answer:
Name: ziroom.com
Address: 119.254.76.107
** server can‘t find ziroom.com: SERVFAIL
工具对比
ping | host | dig | nslookup | |
---|---|---|---|---|
速度 | 慢 | 一般 | 快 | 最快 |
准确率 | 准确、单一 | 准确、丰富 | 准确、单一 | 不一定准确、丰富 |
python代码
class WebSite(object):
def GetIpBySite(self, site):
cmd_dig = "dig {0} +noadditional +noadflag +nocomments +nodnssec".format(site)
cmd_host = "host -W 1 -s {0}".format(site)
try:
ans = self.RunCmdByOs(cmd_dig)
if ans:
ans = ans.split(‘\n‘)[4]
ip = ans.split(‘\t‘)
return (ip[5])
except:
pass
def RunCmdByOs(self, cmd):
try:
f = os.popen(cmd)
ans = f.read()
f.close()
return ans
except:
pass
ws = WebSite()
ip = ws.GetIpBySite("ziroom.com")
print(“ziroom.com的IP地址为:”ip)
# ziroom.com的IP地址为:119.254.76.107
总结
为了更好的在工具中输出域名对应的IP地址,应该对命令执行结果的字符串进行分析,找到合适的正则模式,匹配完整的IP地址。
原文地址:http://blog.51cto.com/executer/2104088
时间: 2024-11-02 22:20:45