server-scripts/mail_on_ssh/mail_on_ssh.sh

56 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
#################################################################
# Check availability of software
#################################################################
function availability {
if [[ ! $(command -v $1) ]]; then
echo Error: '$1' is not available but required. Please install it!
exit 1
fi
}
availability mail
#################################################################
# Get arguments
#################################################################
missingArg()
{
echo "Error: Please define the configuration to be used!"
echo " Usage: $0 -c <configuration_file>"
exit 1
}
while getopts "c:" opt
do
case "$opt" in
c ) CONFIG_FILE="$OPTARG" ;;
? ) missingArg ;;
esac
done
# Check if config was empty
if [ -z "$CONFIG_FILE" ]
then
missingArg
fi
# Check if config file exists
if [ ! -f $CONFIG_FILE ]
then
echo "$CONFIG_FILE does not exist!"
exit 1
fi
source $CONFIG_FILE
#################################################################
# Send email
#################################################################
if [ "$PAM_TYPE" != "close_session" ]; then
host="`hostname`"
subject="SSH Login: $PAM_USER from $PAM_RHOST on $host"
message=$(env)
echo "$message" | mail -s "$subject" "$RECEPIENT"
fi