python:将stdout作为变量/字符串搜索文本

时间:2012-04-24 21:00:49

标签: python string search variables stdout

我正在使用csv文件来查找我文件第一列中的每个域。 我正在做的是检查我的域列表中的域是否与我的服务器匹配。 域的MX记录是否指向我的服务器? 域名服务器是否与我的域名服务器匹配?

现在我有一个安装工作​​。我正在搜索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-Record,则上述操作失败。

更好的方法是使用输出(mail.domain.com或mail2.mydomain.com或其他)并将其写入行(或者像之前的那样&lt; ll socket.gethostbyname( value) ))

我没有设法找到我想做的另一种方式。在线搜索主要指向使用整个标准输出追加。在哪里我想搜索标准输出邮件交换器=&#39; ###并使用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

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这并没有回答你关于解析stdout的问题,但我认为这对你的问题有帮助。

查看dnspython模块。提供的first example可以帮助您稍微清理MX查询。

import dns.resolver

answers = dns.resolver.query('dnspython.org', 'MX')
for rdata in answers:
    print 'Host', rdata.exchange, 'has preference', rdata.preference

然后,您可以执行rdata.exchange的查找,并将其与属于您的主机名或IP地址进行比较。

answers = dns.resolver.query(rdata.exchange)
[a.address for a in answers]

更新: 不知道你为什么要自己提出一个NXDOMAIN异常,但这是一种处理它们的方法。

try:
    answers = dns.resolver.query('bogus.test.for.nxdomain')
except dns.resolver.NXDOMAIN:
    print "NXDOMAIN exception caught."
else:
    print "What?  Maybe my query is going to a 'helpful' server" \
          "that resolves non-existant dns queries to its own server."
    print "Add a filter for the ip address(s) we just got."
    print list(answers)