获取NS和MX Web信息python

时间:2016-02-17 23:40:31

标签: python python-2.7

我试图通过以下代码获取有关两个域输入的信息:

#! usr/bin/python
domainlist=[]
#Ask for domain 1
domain = raw_input("------------------------------------------\nIntroduce el primer domino de la pagina web:\n")
domainlist.append(domain)
#Ask for domain 2
domain = raw_input("------------------------------------------\nIntroduce el segundo domino de la pagina web:\n")
domainlist.append(domain)
print "-------------------"

#Declare Variable
#Find dns
def fundns():
    import dns.resolver
    idns = dns.resolver.query(i,'NS')
    print "Los DNS de %s" % domain + " son:"
    for server in idns:
        print server 
#Find mx
def funmx():        
    import dns.resolver
    mx = dns.resolver.query(i,'MX')
    print "Los MX de %s" % domain + " son:"
    for rdata in mx:
        print "Host", rdata.exchange
for i in domainlist:
    fundns()
    print "----------------------------------------"
    funmx()

但我收到了这个错误:

  

追踪(最近一次调用最后一次):文件" ejercicio5.py",第29行,          funmx()文件" ejercicio5.py",第22行,在funmx中

我可以在打印件中看到所有信息,除非上一个mx注册表,我不知道出了什么问题......

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您需要将域名实际传递给您的函数

import dns.resolver

domainlist = ['google.com', 'yahoo.com']

#Declare Variable
#Find dns
def fundns(domain):

    idns = dns.resolver.query(domain,'NS')
    print "Los DNS de %s" % domain + " son:"
    for server in idns:
        print server 
#Find mx
def funmx(domain):        

    mx = dns.resolver.query(domain,'MX')
    print "Los MX de %s" % domain + " son:"
    for rdata in mx:
        print "Host", rdata.exchange

for i in domainlist:
    fundns(i)
    print "----------------------------------------"
    funmx(i)

以前你试图使用全局变量,或者看起来如此。

输出:

Los DNS de google.com son:
ns3.google.com.
ns2.google.com.
ns4.google.com.
ns1.google.com.
----------------------------------------
Los MX de google.com son:
Host alt3.aspmx.l.google.com.
Host alt2.aspmx.l.google.com.
Host alt4.aspmx.l.google.com.
Host alt1.aspmx.l.google.com.
Host aspmx.l.google.com.
Los DNS de yahoo.com son:
ns6.yahoo.com.
ns2.yahoo.com.
ns5.yahoo.com.
ns3.yahoo.com.
ns1.yahoo.com.
ns4.yahoo.com.
----------------------------------------
Los MX de yahoo.com son:
Host mta5.am0.yahoodns.net.
Host mta7.am0.yahoodns.net.
Host mta6.am0.yahoodns.net.