简体   繁体   English

如何使用awk从特定中继中提取Postfix日志中的所有对话?

[英]How can I extract all conversations in a Postfix log from a particular relay using awk?

I am trying to extract the from address from the sending relay IP address in a postfix log file 我正在尝试从Postfix日志文件中的发送中继IP地址提取发件人地址

Any ideas??? 有任何想法吗???

Much appreciated for any help 非常感谢您的帮助

Ken

Nov 16 00:05:10 mailserver pfs/smtpd[4365]: 925D54E6D9B: client=client1[1.2.3.4]   
Nov 16 00:05:10 mailserver pfs/cleanup[4413]: 925D54E6D9B: message-id=<11414>    
Nov 16 00:05:10 mailserver pfs/qmgr[19118]: 925D54E6D9B: from=<11414@localhost>, size=40217, nrcpt=1 (queue active)    
Nov 16 00:05:10 mailserver pfs/smtp[4420]: 925D54E6D9B: to, relay=[1.3.5.7]:25, delay=0.02, delays=0.02/0/0/0, dsn=5.0.0, status=bounced (host [1.3.5.7] refused to talk to me: 550 Please remove this address from your list)   
Nov 16 00:05:10 mailserver pfs/bounce[4310]: 925D54E6D9B: sender non-delivery notification: 972E34E6D9F   
Nov 16 00:05:10 mailserver pfs/qmgr[19118]: 925D54E6D9B: removed

Hmm, if you just want to collect the from and relay fields with their display bling, you could use this: 嗯,如果你只是想收集,并用它们的显示金光闪闪继电器领域,你可以这样做:

/: from=/ { lastFrom = $7 }
/relay=/ { print lastFrom, $8 }

If you really want to extract the core addresses, it gets slightly more complex... 如果您真的要提取核心地址,它将变得稍微复杂一些。

/: from=/ { lastFrom = $7 }
/relay=/ {
  r = $8
  gsub(/from=</, "", lastFrom)
  gsub(/>,*/, "", lastFrom)
  gsub(/relay=\[/, "", r)
  gsub(/\].*/, "", r)
  print lastFrom, r
}

$ awk -f mail2.awk mail.dat
11414@localhost 1.3.5.7

As usual, these solutions work in both The One True Awk as well as gawk. 与往常一样,这些解决方案可在The One True Awk和gawk中使用。

$7 ~ /^from=,$/ {
    from[$6] = substr($7, 7, length($7) - 8)
} 
$8 ~ /^relay=\[/ { 
    if (substr($8, "[1.3.5.7]")) 
        print from[$6]
    delete from[$6]}
}

Each time a from-recording line is seen, this saves it in an associative array, indexed by the queue ID of the message. 每次看到一条来自记录的行,这会将其保存在一个关联数组中,该数组由消息的队列ID索引。 When a relay line is seen, if it's for the relay you're interested in the associated from line is printed. 当看到中继线时,如果它是用于中继的,则您对关联的from行感兴趣。 substr() is used just so you don't have to \\-escape all of the metacharacters - "[", "]", ".". 使用substr()只是这样,您不必\\-转义所有元字符-“ [”,“]”,“。”。 Whether it's a relay you're interested in or not, the from data is cleaned up so that the array doesn't grow without bounds. 无论您是否对中继感兴趣,都会清理from数据,以便数组不会无限制地增长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM