我正在尝试监视 OPENWRT 路由器上的日志,并在 PPPTD 服务写入日志时发送电子邮件:
Mon Nov 29 09:10:31 2021 daemon.info pptpd[5832]: CTRL: Client x.x.x.x control connection started
我正在使用这个脚本:
#!/bin/ash
logread -f | awk ' /control connection started/ { print "From: R01 Router <orignating [email protected]>"
print "To: [email protected]"
print "Mime-Version: 1.0"
print "Content-Type: text/plain"
print "Subject: Incoming PPTP connection from" $10
print "Content-Transfer-Encoding: 7bit"
print "Incoming PPTP connection from " $10 } ' | /usr/sbin/sendmail -t &
但由于电子邮件并不总是发送,因此得到的结果不一致。有人可以指出我正确的方向吗?
答案1
解决了。
这不起作用的原因是logread -f
因为持续监控日志,sendmail
缓冲区永远不会被关闭,因此在进程终止之前邮件永远不会被发送。
#!/bin/ash
logread -f | awk ' /control connection started/ {
print "From: R01 Router <[email protected]>" > "awkout.txt"
print "To: "[email protected]" >> "awkout.txt"
print "Mime-Version: 1.0" >> "awkout.txt"
print "Content-Type: text/plain" >> "awkout.txt"
print "Subject: Incoming PPTP connection from " $10 >> "awkout.txt"
print "Content-Transfer-Encoding: 7bit" >> "awkout.txt"
print "Incoming PPTP connection from " $10 >> "awkout.txt"
system("ssmtp [email protected] < awkout.txt");
} ' &
每次 awk 匹配时,此代码都会发送一封电子邮件。希望这可以帮助任何试图在 OpenWrt 下监控日志读取的人