Linux下經過shell終端查詢某域名的IP地址、經過IP地址查詢綁定的域名。並 整理返回結果,建立python工具。
linux + 命令行
1. ping 2. host 3. dig 4. nslookup
使用ping命令發送一次請求,使用ICMP協議直接與目標通訊。只要目標站點存在DNS公網解析,都可以找到域名對應的IP地址。python
Ping -c 1 <domain name> # 例1:ping 存在的域名 root@kali:~# 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協議主機上的域名 root@kali:~# 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不存在的域名 root@kali:~# ping ajsdlfjasldfj.com ping: ajsdlfjasldfj.com: Name or service not known
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秒後返回結果) root@kali:~# 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後中止繼續查詢) root@kali:~# 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秒返回結果) root@kali:~# 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 ziroom.com +noadditional +noadflag +nocomments # 參數解釋 # ziroom.com 設置查詢的域名 # +noadditional 設置不顯示附加結果 # +noadflag 設置不顯示驗證信息 # +nocomments 設置不顯示註釋信息 # 示例 root@kali:~# 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 -qt A ziroom.com # 示例一 root@kali:~# 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 | |
---|---|---|---|---|
速度 | 慢 | 通常 | 快 | 最快 |
準確率 | 準確、單一 | 準確、豐富 | 準確、單一 | 不必定準確、豐富 |
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