vmail-stack/bin/vmail-domains-get.sh

82 lines
2.1 KiB
Bash
Raw Normal View History

2021-02-10 16:16:23 -08:00
#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-stack
# MIT License Copyright (c) 2021 Matthew Saunders Brown
# load config
2021-03-19 11:58:35 -07:00
source /usr/local/etc/vmail.conf || echo "ERROR: Either you do not have vmail user permissions or the config file is missing." && exit
2021-02-10 16:16:23 -08:00
help()
{
thisfilename=$(basename -- "$0")
echo "$thisfilename"
echo "Get domain data from vmail database."
echo ""
echo "usage: $thisfilename [domain] [OPTIONS]"
echo ""
echo " -c Output in cvs format."
echo " -h Print this help."
echo " -s Be more silent - use tabs instead of tables for output, do not display column headers."
echo " -w Wildcard search when searching for specific domain."
echo ""
echo " Domain is optional. If not specified all domains will be queried."
echo " By default domain search is for an exact matchs."
echo " Specify -w to turn them in to wildcard search. e.g:"
echo " $thisfilename stack -w # search for all domains that contain 'stack'."
exit
}
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-e"
dbquery="SELECT * from vm_domains"
# check for and set domain
if [ -n "$1" ]; then
if [ $1 == "-h" ]; then
help
elif [[ ! $1 =~ ^- ]] ; then
domain=$1
shift
# query="SELECT * from vm_domains WHERE domain LIKE '%$domain%';"
fi
fi
# set any options that were passed
while getopts "chsw" opt; do
case "${opt}" in
c )
cvs="| sed 's/\t/,/g'"
;;
h )
help
exit;;
s )
dbcmdopts="-s -N $dbcmdopts"
;;
w )
wildcardsearch=true
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
exit;;
esac
done
# build query
if [ -n "$domain" ]; then
if [ -n "$wildcardsearch" ]; then
dbquery="$dbquery WHERE domain LIKE '%$domain%'"
else
if vmail::validate_domain $domain; then
dbquery="$dbquery WHERE domain='$domain'"
else
echo "ERROR: Invalid domain name: $domain"
exit 1
fi
fi
fi
# execute mysql query
eval $dbcmd $dbcmdopts "\"$dbquery;\"" $cvs