「sendmail」は、「logread」出力によってトリガーされた通知を確実に送信しません。

「sendmail」は、「logread」出力によってトリガーされた通知を確実に送信しません。

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でログの読み取りを監視したいすべての人に役立つことを願っています。

おすすめ記事