Linux shell script to remove 2 day old frozen emails from exim queue - linux

I want to put shell script under the cron job which will do following:
1) Removes 2 days old email messages which are in the Exim queue and are bounced/frozen messages, which won't be delivered. Script must not remove messages which are legitimate and are just waiting their time to be delivered.
2) Removes messages which are from invalid sender like null or <> and / or sent to invalid recipient nobody or <>
Thanks a lot.

exiqgrep is your friend! You can easily find queue items matching certain criteria and pipe the found message-id:s to a remove command. E.g:
exiqgrep -z -o 172800 -i | xargs -r exim -Mrm
which translates as follows: find queue items (exiqgrep) that are frozen (-z) and older than two days (-o 172800) and output their message id:s (-i) to xargs that runs only if getting any input (-r) telling exim to remove (-Mrm) the items with given message id:s.
Null (or <>) senders are certainly not invalid! Bounces and other (non-)delivery reports are typically sent from a null address in order to avoid infinite loops in case the bounce is not deliverable. However if you have lots of these on the queue and they stay there for long (e.g. if you are trying to bounce spam sent from fake addresses) you can certainly clean them up too. For example:
exiqgrep -o 86400 -f '<>' -i | xargs -r exim -Mrm
which finds queue items older than one day with a null sender and deletes them.
EDIT: you could also set the following option in your exim.conf to automatically remove frozen bounces after two days:
ignore_bounce_errors_after = 2d

Related

Skipping Sendmail's Queue

I've set up Sendmail so that all messages are delivered to /dev/null instead of being actually stored anywhere else. I'm trying to reduce the number of unecessary disk writes and since those messages are essentially removed I want to, if possible, skip writing them to mqueue. Is there any way to do that?
The closest I could think of is mounting a nullfs filesystem on the mqueue directory, but I'd like a "cleaner" approach using sendmail only. Is this possible?
Thanks!
Most likely you choose wrong way to solve your problem but anyway:
You can select discard mailer for all recipients in check_rcpt (Local_check_rcpt) rule set. It will act as equivalent of DISCARD in access table.
Add the following lines to sendmil.mc file, generate new sendmail.cf file and restart or HUP sendmail daemon.
LOCAL_RULESETS
SLocal_check_rcpt
# PUT TAB (\t) BEFORE $# !!!
R$* $#discard $: discard

Why do Period will be ignored at the beginning of a sentence in SendEmail on Linux

I want to use command line to send an email on Linux so I choose sendEmail (a lightweight, command line SMTP email client). However, I find Period (.) at the beginning of a sentence will be ignored and it really confused me.
-m MESSAGE message body
My command:
sendEmail -f sender#example.com -t receiver#example.com -u "Test mail" -s smtp.example.com -xu sender#example.com -xp sender_password -m ".Hello\n..Hello\nHello.world" -o tls=no
What I want to display is:
.Hello
..Hello
Hello.world
But the result is:
Hello
.Hello
Hello.world
Thanks a million.
This is a bug in the sendEmail client. In SMTP a line containing nothing else but a single period . is used to indicate the end of the message (DATA segment in SMTP). To avoid unintended transmission termination if a message contains a line with a single period, on all lines starting with a period an extra period has to be added before the message goes onto the wire; and removed upon receive. It's the task of a proper SMTP client to take care of this. It's clearly a faulty behavior of the client.
To get around the bug, add an extra period.
For details see RFC5321, sections 4.1.1.4 and 4.5.4.

Using flock to lock file for X time

I am a Linux novice and currently developing a security system using Raspberry Pi 3 and MotionEye. To get notifications via e-mail, I am attempting to create a custom shell script that will send an e-mail if there is motion, lock for X minutes, then send another e-mail if there is still motion. However, I am having some difficulties.
I created a simple Python script named "send_email.py" using SMTP that works perfectly fine for sending e-mails when I execute it via command line.
The shell script (named "flock_email.sh") is where I run into troubles in a few regards:
Whenever I run flock_email.sh, it completely overwrites send_email.py. I have tried to change file permission so it is only executable by the user, but it still overwrites.
The flock command/function does not work as I intended or at all. I have looked all over the internet and tried multiple different codes, but none have worked. I have attached my various flock_email.sh scripts I have tried.
Not necessarily a problem, but I am a bit confused on what my "shebang" line should be. For flock_email.sh I have it as "!#/bin/bash", which I believe makes the script it executable, at least according to this. Do I still need to change the permissions via the command "chmod +x flock_email.sh"? The path is /home/pi, which is essentially the main directory of my Pi.
The different solutions I have tried:
In flock_email.sh, I have tried to directly change the file permissions to read-only instead of using flock, having it sleep, then changing the permissions back to allow execution of the file.
Multiple flock_email.sh implementations, as attached.
To summarize:
I need to execute send_email.py before locking the file flock_email.sh.
Once locked, it needs to stay locked for X time.
Does anyone have any pointers or suggestions? I have spent well over 15 hours tinkering with this and feel like I have gotten nowhere!
send_email.py:
#!/usr/bin/env
import smtplib
def send_email():
content = "Message I want to send to specified e-mail."
sender = "e-mail account that will send message"
pword = "password of sender"
receiver = "e-mail account that will receive message"
mail = smtplib.SMTP("smtp.gmail.com",587)
mail.ehlo
mail.starttls()
mail.login(sender,pword)
mail.sendmail(sender,receiver,content)
mail.close()
send_email()
flock_email.sh (1):
#!/bin/bash
(
python /home/pi/send_email.py
flock -e 200
sleep [time in seconds]
)
flock_email.sh (2):
#!/bin/bash
(
python /home/pi/send_email.py
exec 3>/home/pi/send_email.py
flock -x 3
sleep [time in seconds]
exec 3>&-
)
flock_email.sh (3):
#!/bin/bash
python /home/pi/send_email.py
chmod 444 /home/pi/send_email.py # modify to read only for all
sleep [time in seconds]
chmod 755 /home/pi/send_email.py # modify to rwx for owner, r-x for others
The reason why man flock and all posts say to use > is because you're supposed to use a dedicated lock file, typically in /var/lock:
#!/bin/bash
exec 3> /var/lock/motionmail
flock -ne 3 || exit
python /home/pi/send_email.py
sleep 3600
This additionally fixes you sending your email regardless, before you ever check the lock, and aborts new emails instead of queueing them all up.
You choose the lock file name based on the scope you want your lock to have:
If you only want one email per hour, you can use something like /var/lock/motionmail because there's just one per system.
If you want one email for each user per hour, you can use $HOME/.motionmail.lock because there's just one per user.
You can use /home/pi/send_email.py if you want with <, but this implies that you want one email per hour not only for each user, programming language and script copy, but also every time you hit save and replace the file with your editor*
* Editors differ in whether they replace or overwrite a file

mail can't send messages: Process exited with a non-zero status

I wrote a bash script that sends out a mail, but after 50 e-mails it starts to say "mail can't send messages: Process exited with a non-zero status". Can anyone help solve my problem. The code I used is below if you want to take a look at it.
#!/bin/bash
#Declare variables area.
emailBody=email_body.txt; #you have to use without “ symbol for some reason
emailList=email_list_delimiter.txt;
#send mail command. using a read file loop.
while IFS= read -r emailTo; do
cat $emailBody |
mail -s "Hi, I'm looking for a position in IT Field." $emailTo |
echo “Success”;
done < <(grep . $emailList)
You are probably hitting a server-side limit on the number of messages you can send in a fixed time, or equivalently the number of connections allowed within a moving window of time.
If you can (the message is not "personalized") it is best to send one message to multiple recipients, rather than many messages, each to one recipient. Do that by perhaps putting your own e-mail address in the To field, and then Bccing the whole of the list of recipients in one go. You'll have to check your mail command for how to do that.

Calculating the difference between the first words(timestamp) using perl dynamically

I have a program that keeps on writing the icmp echo requests being received by a machine into a file.
I am using system ("tcpdump icmpecho[0] == 8 | tee abc.txt") to do that.
So this process keeps on going till I end the program manually.
Each line has the timestamp as its first word.
now i want to calculate the frequency of the echo requests I am receiving using a separate script so that if it reaches a certain threshold , I can print an alert.
I tried to use grep -Eo '^[^ ]+' file
to get the timestamps into an array, but I dont know what to do after getting them into an array. grep goes on in a while loop since the file it is reading from keeps on getting populated infinitely.(I'll not have an option of monitoring the differences and printing an alert if grep goes on like that right?)
All I am trying to do is to keep track of the frequency of icmp echo requests that are coming in on my machine and print an alert message whenever that frequency crosses a threshold. is there any alternative way?
All timestamps are saved in #arr
perl -ne '$f{$_}++ or push #arr, $_ for /(\d+:\d+)/ }{ print "$_ [$f{$_} times]\n" for #arr' file
constantly reading from log file,
perl -e 'open$T,pop;while(1){while(<$T>){ ++$f{$_}>10 and print "[$f{$_}]$_" for /(\d+:\d+)/ }sleep 1;seek $T,0,1}' file
I am using
tcpstat -i eth1 -f icmp[0] == 8
to get the request count. it gives me 3 more parameters but got to research a bit bout them!

Resources