Add configuration file, add script output file for script 'get_addresslist_for_domain.sh'.
This commit is contained in:
parent
354b2f3d7b
commit
235f1cf1f7
40
conf/get_addresslist_for_domain.conf.sample
Normal file
40
conf/get_addresslist_for_domain.conf.sample
Normal file
@ -0,0 +1,40 @@
|
||||
# ----------------------------------------------------
|
||||
# ---
|
||||
# - Parameter Settings for script 'get_addresslist_for_domain..sh'.
|
||||
# ---
|
||||
# ----------------------------------------------------
|
||||
|
||||
|
||||
# - db_type
|
||||
# -
|
||||
# - Type of Postfix Database
|
||||
# -
|
||||
# - Possible values are 'pgsql' (PostgeSQL) or 'mysql' (MySQL)
|
||||
# -
|
||||
# - Defaults to: db_type="pgsql"
|
||||
# -
|
||||
#db_type="pgsql"
|
||||
|
||||
# - db_name
|
||||
# -
|
||||
# - Database name for the postfix database
|
||||
# -
|
||||
# - Defaults to: db_name="postfix"
|
||||
# -
|
||||
#db_name="postfix"
|
||||
|
||||
# - mysql_credential_args (root access to MySQL Database)
|
||||
# -
|
||||
# - Only used if db_type=mysql
|
||||
# -
|
||||
# - Example
|
||||
# - mysql_credential_args="--login-path=local"
|
||||
# - mysql_credential_args="--defaults-file=/etc/mysql/debian.cnf" (Debian default)
|
||||
# - mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
|
||||
# -
|
||||
# - Defaults to:
|
||||
# - '/etc/mysql/debian.cnf' if MySQL is installed from debian package system
|
||||
# - '/usr/local/mysql/sys-maint.cnf' otherwise
|
||||
# -
|
||||
#mysql_credential_args=""
|
||||
|
@ -1,5 +1,29 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
|
||||
script_dir="$(dirname $(realpath $0))"
|
||||
conf_dir="${script_dir}/conf"
|
||||
conf_file="${conf_dir}/get_addresslist_for_domain.conf"
|
||||
|
||||
_date="$(date +%Y-%m-%d)"
|
||||
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Setting Defaults
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
DEFAULT_db_type="pgsql"
|
||||
DEFAULT_db_name="postfix"
|
||||
|
||||
|
||||
#---------------------------------------
|
||||
#-----------------------------
|
||||
# Base Function(s)
|
||||
#-----------------------------
|
||||
#---------------------------------------
|
||||
|
||||
function usage() {
|
||||
|
||||
echo
|
||||
@ -7,18 +31,66 @@ function usage() {
|
||||
if [ -n "$1" ];then
|
||||
echo -e "Error: $1\n"
|
||||
fi
|
||||
echo -e "\nPrints a summary of mailboxes and forward addresse for the given domain.\n"
|
||||
echo -e "\nPrints a summary of mailboxes and forward addresses for the given domain.\n"
|
||||
echo -e "\tusage: `basename $0` <email domain>\n"
|
||||
exit 1
|
||||
clean_up 1
|
||||
}
|
||||
|
||||
|
||||
trim() {
|
||||
local var="$*"
|
||||
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
|
||||
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
|
||||
echo -n "$var"
|
||||
}
|
||||
clean_up() {
|
||||
|
||||
# Perform program exit housekeeping
|
||||
exit $1
|
||||
}
|
||||
echononl(){
|
||||
echo X\\c > /tmp/shprompt$$
|
||||
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
|
||||
echo "$*\\c" 1>&2
|
||||
else
|
||||
echo -e -n "$*" 1>&2
|
||||
fi
|
||||
rm /tmp/shprompt$$
|
||||
}
|
||||
|
||||
warn (){
|
||||
echo ""
|
||||
echo -e " [ \033[33m\033[1mWarning\033[m ]: $*"
|
||||
echo ""
|
||||
}
|
||||
|
||||
error (){
|
||||
echo ""
|
||||
echo -e " [ \033[31m\033[1mError\033[m ]: $*"
|
||||
echo ""
|
||||
}
|
||||
|
||||
fatal(){
|
||||
echo ""
|
||||
echo -e "[ \033[31m\033[1mFehler\033[m ]: $*"
|
||||
echo -e "\n Script was interupted!\n"
|
||||
echo
|
||||
clean_up 1
|
||||
}
|
||||
echo_ok() {
|
||||
echo -e "\033[75G[ \033[32mok\033[m ]"
|
||||
}
|
||||
echo_failed() {
|
||||
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
|
||||
}
|
||||
echo_skipped() {
|
||||
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
|
||||
}
|
||||
|
||||
trap clean_up SIGHUP SIGINT SIGTERM
|
||||
|
||||
clear
|
||||
echo ""
|
||||
echo -e "\033[32mRunning script \033[1m"$(basename $0)"\033[m .."
|
||||
|
||||
|
||||
|
||||
|
||||
@ -28,37 +100,57 @@ address="$1"
|
||||
|
||||
domain=`echo $address | cut -d'@' -f2`
|
||||
|
||||
mysql=false
|
||||
# - mysql_credential_args
|
||||
# -
|
||||
# - MySQL / MariaDB credentials
|
||||
# -
|
||||
# - Giving password on command line is insecure an sind mysql 5.5
|
||||
# - you will get a warning doing so.
|
||||
# -
|
||||
# - Reading username/password fro file ist also possible, using MySQL/MariaDB
|
||||
# - commandline parameter '--defaults-file'.
|
||||
# -
|
||||
# - Since Mysql Version 5.6, you can read username/password from
|
||||
# - encrypted file.
|
||||
# -
|
||||
# - Create (encrypted) option file:
|
||||
# - $ mysql_config_editor set --login-path=local --socket=/tmp/mysql.sock --user=root --password
|
||||
# - $ Password:
|
||||
# -
|
||||
# - Use of option file:
|
||||
# - $ mysql --login-path=local ...
|
||||
# -
|
||||
# - Example
|
||||
# - mysql_credential_args="--login-path=local"
|
||||
# - mysql_credential_args="--defaults-file=/etc/mysql/debian.cnf" (Debian default)
|
||||
# - mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
|
||||
# -
|
||||
mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
|
||||
mysql_db=postfix
|
||||
out_file="${script_dir}/log/adresslist-${domain}-${_date}.list"
|
||||
> "$out_file"
|
||||
|
||||
echo ""
|
||||
echo ""
|
||||
echononl " Loading Configuration values from $(basename ${conf_file}).."
|
||||
if [[ ! -f "$conf_file" ]]; then
|
||||
echo_skipped
|
||||
else
|
||||
source "${conf_file}" > /dev/null 2>&1
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo_ok
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
fi
|
||||
|
||||
[[ -n "$db_type" ]] || db_type="$DEFAULT_db_type"
|
||||
|
||||
if [[ "$db_type" != "pgsql" ]] && [[ "$db_type" != "mysql" ]]; then
|
||||
fatal "Unknown Database Type '$db_type' for Password Database (Parameter db_type)"
|
||||
fi
|
||||
|
||||
[[ -n "$db_name" ]] || db_name="$DEFAULT_db_name"
|
||||
|
||||
|
||||
if [[ "$db_type" = "mysql" ]]; then
|
||||
if [[ -z "$mysql_credential_args" ]]; then
|
||||
if [[ -f "/etc/mysql/debian.cnf" ]]; then
|
||||
mysql_credential_args="--defaults-file=/etc/mysql/debian.cnf"
|
||||
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
|
||||
mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
|
||||
else
|
||||
fatal "No credentials for access to MySQL is given!"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echononl " Create output directory '$(dirname "$out_file")'.."
|
||||
if [[ ! -d "$(dirname "$out_file")" ]] ; then
|
||||
mkdir "$(dirname "$out_file")"
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo_ok
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
else
|
||||
echo_skipped
|
||||
fi
|
||||
|
||||
#echo "address...: $address"
|
||||
#echo "domain....: $domain"
|
||||
|
||||
declare -A address_arr
|
||||
declare -a orders
|
||||
@ -69,17 +161,17 @@ declare -a mbox_alias_arr
|
||||
|
||||
|
||||
echo ""
|
||||
if $mysql ; then
|
||||
_addresses=$(mysql $mysql_credential_args $mysql_db -N -s -e "select address from alias where domain = '$domain' ORDER BY address")
|
||||
if [[ "$db_type" = "mysql" ]]; then
|
||||
_addresses=$(mysql $mysql_credential_args "$db_name" -N -s -e "select address from alias where domain = '$domain' ORDER BY address")
|
||||
for _address in $_addresses ; do
|
||||
address_arr[$_address]=$(trim $(mysql $mysql_credential_args $mysql_db -N -s -e "select goto from alias where address = '$_address'"))
|
||||
address_arr[$_address]=$(trim $(mysql $mysql_credential_args "$db_name" -N -s -e "select goto from alias where address = '$_address'"))
|
||||
orders+=("$(trim $_address)")
|
||||
done
|
||||
else
|
||||
|
||||
_addresses=$(su - postgres -c"psql postfix -t -q -c\"select address from alias where domain = '$domain' ORDER BY address\"")
|
||||
_addresses=$(su - postgres -c"psql "$db_name" -t -q -c\"select address from alias where domain = '$domain' ORDER BY address\"")
|
||||
for _address in $_addresses ; do
|
||||
address_arr[$_address]=$(trim $(su - postgres -c"psql postfix -t -q -c\"select goto from alias where address = '$_address'\""))
|
||||
address_arr[$_address]=$(trim $(su - postgres -c"psql "$db_name" -t -q -c\"select goto from alias where address = '$_address'\""))
|
||||
orders+=("$(trim $_address)")
|
||||
done
|
||||
fi
|
||||
@ -114,47 +206,65 @@ for i in ${!_tmp_mbox_arr[@]} ; do
|
||||
fi
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "--------------------"
|
||||
echo "- Zusammenfassung E-Mail Adressen der Domain \"$domain\""
|
||||
echo "--------------------"
|
||||
echo "" >> "$out_file"
|
||||
echo "--------------------" >> "$out_file"
|
||||
echo "- Zusammenfassung E-Mail Adressen der Domain \"$domain\"" >> "$out_file"
|
||||
echo "--------------------" >> "$out_file"
|
||||
|
||||
echo -e "\n\n\033[32m\033[1mZusammenfassung E-Mail Adressen der Domain \"$domain\":\033[m\n"
|
||||
|
||||
|
||||
echo "" >> "$out_file"
|
||||
echo "E-Mail Adressen: Postfach ohne Weiterleitungen:" >> "$out_file"
|
||||
echo "-----------------------------------------------" >> "$out_file"
|
||||
echo "" >> "$out_file"
|
||||
|
||||
|
||||
echo ""
|
||||
echo "E-Mail Adressen: Postfach ohne Weiterleitungen:"
|
||||
echo "-----------------------------------------------"
|
||||
echo -e "\n\033[1mE-Mail Adressen: Postfach ohne Weiterleitungen\033[m\n"
|
||||
|
||||
for i in ${!mbox_arr[@]} ; do
|
||||
echo "${mbox_arr[$i]}"
|
||||
echo " ${mbox_arr[$i]}"
|
||||
echo " ${mbox_arr[$i]}" >> "$out_file"
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "E-Mail Adressen: Postfach mit Weiterleitungen:"
|
||||
echo "----------------------------------------------"
|
||||
echo "" >> "$out_file"
|
||||
echo "" >> "$out_file"
|
||||
echo "E-Mail Adressen: Postfach mit Weiterleitungen:" >> "$out_file"
|
||||
echo "----------------------------------------------" >> "$out_file"
|
||||
echo "" >> "$out_file"
|
||||
|
||||
echo -e "\n\n\033[1mE-Mail Adressen: Postfach mit Weiterleitungen:\033[m\n"
|
||||
|
||||
for i in ${!mbox_alias_arr[@]} ; do
|
||||
echo -e "${mbox_alias_arr[$i]}\n --> ${address_arr[${mbox_alias_arr[$i]}]}"
|
||||
echo
|
||||
echo -e " ${mbox_alias_arr[$i]}\n --> ${address_arr[${mbox_alias_arr[$i]}]}"
|
||||
echo -e " ${mbox_alias_arr[$i]}\n --> ${address_arr[${mbox_alias_arr[$i]}]}" >> "$out_file"
|
||||
echo "" >> "$out_file"
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "E-Mail Adressen: Nur Weiterleitungen:"
|
||||
echo "-------------------------------------"
|
||||
echo "" >> "$out_file"
|
||||
echo "" >> "$out_file"
|
||||
echo "E-Mail Adressen: Nur Weiterleitungen:" >> "$out_file"
|
||||
echo "-------------------------------------" >> "$out_file"
|
||||
echo "" >> "$out_file"
|
||||
|
||||
echo -e "\n\n\033[1mE-Mail Adressen: Nur Weiterleitungen:\033[m\n"
|
||||
|
||||
for i in ${!alias_arr[@]} ; do
|
||||
[[ ${alias_arr[$i]} =~ ^abuse@ ]] && continue
|
||||
[[ ${alias_arr[$i]} =~ ^postmaster@ ]] && continue
|
||||
echo -en "${alias_arr[$i]}\n -->"
|
||||
echo -en " ${alias_arr[$i]}\n -->"
|
||||
echo -en " ${alias_arr[$i]}\n -->" >> "$out_file"
|
||||
|
||||
IFS=',' read -a _addr_list <<<"${address_arr[${alias_arr[$i]}]}"
|
||||
for j in ${!_addr_list[@]} ; do
|
||||
echo -n " ${_addr_list[$j]}"
|
||||
echo -n " ${_addr_list[$j]}" >> "$out_file"
|
||||
done
|
||||
echo
|
||||
echo
|
||||
echo ""
|
||||
echo ""
|
||||
echo "" >> "$out_file"
|
||||
echo "" >> "$out_file"
|
||||
done
|
||||
|
||||
echo "" >> "$out_file"
|
||||
echo ""
|
||||
exit 0
|
||||
clean_up 0
|
||||
|
Loading…
Reference in New Issue
Block a user