我正在使用csv文件來nslookup我的文件的第一列中的每個域。 我正在做的是檢查我的域列表中的域是否與我的服務器匹配。 域的MX記錄是否指向我的服務器? 域的名稱服務器是否與我的域名服務器相匹配?python:搜索stdout文本作爲變量/字符串
現在我有一個安裝類的工作。我搜索標準輸出我的服務器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-記錄上述失敗。
最好是使用輸出(mail.domain.com或mail2.mydomain.com或其他)並將其寫入該行(或以前,我將socket.gethostbyname(值))
我沒有設法找到我想要做的另一種方式。在線搜索主要指向使用整個stdout附加。當我想搜索「郵件交換器=」 ###的標準輸出,並使用文本過程使用的MX優先級,或者,mail.mydomain.com
整個Python
#!/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
有什麼想法?
你是完全正確的,自從讀這本以來,我一直在重寫它。現在我只需要弄清楚如何提高dns.resolver.NXDOMAIN。嘗試了幾種不喜歡的方法。試驗和錯誤,我猜。再次感謝! – Tommy 2012-04-27 01:23:31