Skip to content

Instantly share code, notes, and snippets.

@hvanmegen
Created February 12, 2020 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hvanmegen/2bc6a7905d0492cda88b919f58473cdb to your computer and use it in GitHub Desktop.
Save hvanmegen/2bc6a7905d0492cda88b919f58473cdb to your computer and use it in GitHub Desktop.
findmail shell script
#!/bin/bash
scriptname=`basename ${0}`
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
# Check if an argument was set
if [ -z "$1" ]; then
echo "${scriptname}: No search argument specified"
echo "Try '${scriptname} PATTERN' to search for a specific email."
exit 3
fi
# set up started variables
debug=false
found=false
declare -a list=("/var/log/mail.log" "/var/log/maillog")
# start by having found nothing (variable set near top)
for log in "${list[@]}"
do
if [[ -e ${log} && -r ${log} ]]; then
# we found something!
for id in `cat ${log} | grep $1 | cut -d " " -f 7 | cut -d ":" -f 1 | grep -v "\=" | grep -v "\." | sort | uniq`
do
if [ ${debug} == true ] ; then
echo "Found '${id}' referenced by looking for '${1}'..."
fi
if [[ ${#id} -gt 11 && ${#id} -lt 15 ]] ; then
found=true
cat ${log} | grep ${id} | grep "sm-mta"
fi
if [ ${debug} == true ] ; then
echo
fi
done
fi
done
# if we didn't find something, then
if [ ${found} != true ] ; then
echo "No readable logfile(s) found."
exit 2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment