我正在使用 csv 文件对文件第一列中的每个域进行 nslookup。我正在做的是检查我的域列表中的域是否与我的服务器匹配。域的 MX 记录是否指向我的服务器?域的名称服务器是否与我的名称服务器匹配?
现在我有一个设置类型的工作。我正在为我的服务器 IP/主机/等搜索标准输出。如果它们匹配或不匹配,我会写一些东西。
但是,如果 MX 记录指向其自身的子域,则不足以确定这是否是我的 IP 之一。这就是我现在正在做的事情:
例如 nslookup:
$ nslookup -type=MX mydomain.com
Server: 192.168.1.1
Address: 192.168.1.1#53
Non-authoritative answer:
mydomain.com mail exchanger = 10 mail.mydomain.com.
Authoritative answers can be found from:
mail.mydomain.com internet address = 1.2.3.4 # << I seem to think this isn't always present.
当前子域解决方法:
elif 'mail.' + row[0] in stdout:
host2ip = socket.gethostbyname('mail.' + row[0])
newdata = [host2ip]
writer.writerow(row + newdata)
但是,如果域使用 mx = mail2.mydomain.com 或任何其他子域/A-Record 上述失败怎么办。
更好的是使用输出(mail.domain.com 或 mail2.mydomain.com 或其他)并将其写入行(或之前我将 socket.gethostbyname( value ))
我没有设法找到我想做的另一种方式。在线搜索大多指向使用整个标准输出来追加。我想在标准输出中搜索“邮件交换器 =”### 并使用处理 mx 优先级的文本,或者 mail.mydomain.com
整个蟒蛇
#!/usr/bin/python
#
import datetime
import csv
import os, time
import socket
from stat import * # ST_SIZE etc
from subprocess import Popen, PIPE, STDOUT
# Set Date
now = datetime.datetime.now()
today = now.strftime("%m-%d-%Y")
# Files
original = "dns_list.csv"
results = "results/dns_results_" + today + ".csv" #dns_results_04-14-2012.csv
tempfile = results + ".tmp"
# Commands
mxscan = "nslookup -type=MX"
nsscan = "nslookup -type=NS"
ascan = "nslookup -type=A"
digserver = "8.8.8.8"
SP = " "
incsv = open(original, 'rb')
try:
reader = csv.reader(incsv)
outcsv = open(tempfile, 'wb')
try:
writer = csv.writer(outcsv)
for row in reader:
p = Popen(mxscan + SP + row[0] + SP + digserver, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdout, empty = p.communicate()
print 'Command: %s\nOutput: %s\n' % (mxscan + SP + row[0] + SP + digserver , stdout)
if not stdout or 'find Zone: NXDOMAIN' in stdout: # 'Zone' is column header
newdata = ['mail exchange']
writer.writerow(row + newdata)
elif 'psmtp.com' in stdout:
newdata = ['Postini']
writer.writerow(row + newdata)
elif 'mail.' + row[0] in stdout:
host2ip = socket.gethostbyname('mail.' + row[0])
newdata = [host2ip]
writer.writerow(row + newdata)
else:
newdata = ['External Email']
writer.writerow(row + newdata)
finally:
outcsv.close()
finally:
incsv.close()
original = tempfile
incsv = open(original, 'rb')
try:
reader = csv.reader(incsv)
outcsv = open(results, 'wb')
try:
writer = csv.writer(outcsv)
for row in reader:
p = Popen(nsscan + SP + row[0], shell=True, stdin=PIPE,stdout=PIPE, stderr=PIPE)
stdout, empty = p.communicate()
print 'Command: %s\nOutput: %s\n' % (mxscan + SP + row[0],stdout)
if not stdout or 'find Zone: NXDOMAIN' in stdout:
newdata = ['A records', 'Action']
writer.writerow(row + newdata)
elif 'nameserver = dauth1.mydomain.com' in stdout or 'nameserver = dauth2.mydomain.com' in stdout:
newdata = ['dauth1/2']
writer.writerow(row + newdata)
elif 'nameserver = ns1.mydomain.com' in stdout or 'nameserver = ns2.mydomain.com' in stdout:
newdata = ['ns1/2']
writer.writerow(row + newdata)
else:
newdata = ['External DNS', 'Delete/Charge']
writer.writerow(row + newdata)
finally:
outcsv.close()
finally:
incsv.close()
print "Writing changes to new file...."
time.sleep(1)
os.remove(tempfile)
print "Complete! Your new file is located at /root/mxscan/" + results
有什么想法吗?