Python 从一个查询中获取结果并将其放入另一个查询中(搜索mx记录)
我有很多电子邮件日志文件要处理。我试图找到我们发送给的每个人,按mx服务器排序 这将返回MX服务器的列表:Python 从一个查询中获取结果并将其放入另一个查询中(搜索mx记录),python,linux,grep,mx-record,Python,Linux,Grep,Mx Record,我有很多电子邮件日志文件要处理。我试图找到我们发送给的每个人,按mx服务器排序 这将返回MX服务器的列表: grep 'mx' /my/log/file | cut -d , -f 11 | cut -d ' ' -f 1 | sort | uniq 出厂价格: mx3.hotmail.com mx2.hotmail.com mx1.hotmail.com mx4.hotmail.com hotmail.com hotmail.com.au 这将获取从该MX服务器发送到的域(在本例中为所有
grep 'mx' /my/log/file | cut -d , -f 11 | cut -d ' ' -f 1 | sort | uniq
出厂价格:
mx3.hotmail.com
mx2.hotmail.com
mx1.hotmail.com
mx4.hotmail.com
hotmail.com
hotmail.com.au
这将获取从该MX服务器发送到的域(在本例中为所有Hotmail):
出厂价格:
mx3.hotmail.com
mx2.hotmail.com
mx1.hotmail.com
mx4.hotmail.com
hotmail.com
hotmail.com.au
如何编写脚本,以便将一个查询的结果直接插入另一个查询?我把python作为一个标签,因为我对它很熟悉。
mx*.hotmail.com
应该与m.hotmail.com、mx.hotmail.com、mxx.hotmail.com等匹配。你可能想要mx.*\.hotmail\.com
要在另一个bash命令中使用一个bash命令中的字符串,可以使用$()
。例如,echo abc$(echo def)ghi
您也可以使用反引号,但反引号也不能嵌套。这就是我们最终要做的:
cat /my/log/file | cut -d "," -f 11,6 | cut -d '@' -f 2 | cut -d ' ' -f 1 | egrep '(([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}),(([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6})' | cut -d "," -f 2,1 | sort | uniq > /tmp/mxservers2.txt
结果如下所示:
hotmail.com,mx1.hotmail.com
d,2012-07-17 07:09:29+0000,2012-07-17 07:09:15+0000,,bounce@address.net,recipient@example.net,,relayed,2.0.0 (success),smtp;250 2.0.0 bK9F1j04M0vJLGl06K9VnA mail accepted for delivery,mx.example.net (0.0.0.0),,smtp,(127.0.0.1),smtp,sending IP,receiving IP,"ENHANCEDSTATUSCODES,8BITMIME,SIZE,STARTTLS",18704,sending.domain.com,message.streaming,,FALSE,=?utf-8?Q?Subject?= <sender@example2.net>
作为参考,日志条目如下所示:
hotmail.com,mx1.hotmail.com
d,2012-07-17 07:09:29+0000,2012-07-17 07:09:15+0000,,bounce@address.net,recipient@example.net,,relayed,2.0.0 (success),smtp;250 2.0.0 bK9F1j04M0vJLGl06K9VnA mail accepted for delivery,mx.example.net (0.0.0.0),,smtp,(127.0.0.1),smtp,sending IP,receiving IP,"ENHANCEDSTATUSCODES,8BITMIME,SIZE,STARTTLS",18704,sending.domain.com,message.streaming,,FALSE,=?utf-8?Q?Subject?= <sender@example2.net>
d,2012-07-17 07:09:29+000012-07-17 07:09:15+0000,,bounce@address.net,recipient@example.net,中继,2.0.0(成功),smtp;250 2.0.0 bK9F1j04M0vJLGl06K9VnA邮件接受投递,mx.example.net(0.0.0),smtp,(127.0.0.1),smtp,发送IP,接收IP,“增强状态码,8BITMIME,大小,STARTTLS”,18704,sending.domain.com,message.streaming,,FALSE,=?utf-8?Q?Subject?=
不完美,但完成了任务。显示示例日志条目。