rework vhost-get.sh, new search via macro option and standardized output

This commit is contained in:
Matthew Saunders Brown 2025-02-04 09:48:29 -08:00
parent 7e4a8eafb3
commit 590fd23f40

View File

@ -13,12 +13,18 @@ help()
thisfilename=$(basename -- "$0") thisfilename=$(basename -- "$0")
echo "Get VirtualHost info." echo "Get VirtualHost info."
echo "" echo ""
echo "usage: $thisfilename [-d <domain>] [-u <username>] [-o <mode>] [-c] [-h]" echo "usage: $thisfilename [-d <domain>] [-u <username>] [-m <macro>] [-o <option>] [-c] [-h]"
echo "" echo ""
echo " -h Print this help." echo " -h Print this help."
echo " -d <domain> Domain name to get info for. By default all domains are returned." echo " -d <domain> Domain name to get info for. By default all domains are returned."
echo " -u <username> Only return domains owned by specified username." echo " -u <username> Only return domains owned by specified username."
echo " -o <mode> Type of query. 'virtualhosts' (default), 'redirects', 'aliases'." echo " -m <macro> Abbreviated name of Apache macro to query."
echo " e.g. VHost, Redirect, VHostAlias, VHostSubdomain, VMail, Mailman3."
echo " Optional, defaults to VHost (all regular VirtualHosts)."
echo " Leave off the HTTP/HTTPS part, both are included in results."
echo " -o <option> Alias or Redirect URL to query."
echo " For Aliases & Redirects '-d <domain>' is the alias/redirect domain,"
echo " and '-o <option>' is the existing VirtualHost to alias/redirect to."
echo " -c CVS - Output in cvs format, instead of tabbed table." echo " -c CVS - Output in cvs format, instead of tabbed table."
} }
@ -27,69 +33,20 @@ vhost:getoptions "$@"
# create newline var # create newline var
NL=$'\n' NL=$'\n'
# check for option (mode or type of query) # check for macro
if [[ -z $option ]]; then if [[ -z $macro ]]; then
option='virtualhosts' macro='VHost'
fi fi
if [[ $option = 'virtualhosts' ]]; then if [[ $macro = 'VHost' ]]; then
if [[ -n $domain ]]; then if [[ -n $domain ]]; then
if [[ -d /srv/www/$domain ]]; then if [[ -f /etc/apache2/sites-available/$domain.conf ]]; then
virtualhostArray=($domain) # get single VHostHTTP(S) config for specified domain
else readarray -t configs < <(head -n 1 /etc/apache2/sites-available/$domain.conf|grep --extended-regexp "^Use VHostHTTP([S]?) ")
echo "ERROR: $domain not found" configcount=${#configs[@]}
exit 1 if [ "$configcount" -eq 0 ]; then
fi echo "ERROR: $domain is not configured as a VHost"
else
vhost::set-virtualhostArray
fi
output="virtualhost username config php status"
for v in "${virtualhostArray[@]}"
do
owner=$(stat -c '%U' /srv/www/$v)
fpm=$(head -n 1 /etc/apache2/sites-available/$v.conf |grep ^Use|cut -d ' ' -f 5)
if [[ -f /etc/apache2/sites-available/$v.conf ]]; then
macro="Custom"
if head -n 1 /etc/apache2/sites-available/$v.conf |grep --quiet ^Use; then
macro=$(head -n 1 /etc/apache2/sites-available/$v.conf |grep ^Use|cut -d ' ' -f 2)
fi
else
macro="None"
fi
if [[ -f /etc/apache2/sites-enabled/$v.conf ]]; then
status="Enabled"
else
status="Disabled"
fi
if [[ -n $username ]]; then
if [[ $username = $owner ]]; then
output="$output${NL}$v $owner $macro $fpm $status"
fi
else
output="$output${NL}$v $owner $macro $fpm $status"
fi
done
if [[ $output != "virtualhost username config php status" ]]; then
if [[ $cvs ]]; then
echo "$output" | tr " " ","
else
echo "$output" | column -t
fi
fi
elif [[ $option = 'redirects' ]]; then
# working dir for redirect configs
cd /etc/apache2/sites-available
if [[ -n $domain ]]; then
if [[ -f $domain.conf ]]; then
if head -n 1 $domain.conf |grep --quiet '^Use Redirect'; then
vhostConfigs=("$domain.conf")
else
echo "ERROR: $domain is not configured as a Redirect."
exit 1 exit 1
fi fi
else else
@ -97,23 +54,31 @@ elif [[ $option = 'redirects' ]]; then
exit 1 exit 1
fi fi
else else
vhostConfigs=(`ls -1`) # get array of all VHostHTTP(S) configs
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use VHostHTTP([S]?) ")
fi fi
output="virtualhost config redirect status" output="domain username macro php status"
for c in "${vhostConfigs[@]}" for config in "${configs[@]}"
do do
if head -n 1 /etc/apache2/sites-available/$c |grep --quiet '^Use Redirect'; then IFS=' ' read -r -a parts <<< "$config"
domain=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use Redirect'|cut -d ' ' -f 3) macro=${parts[1]}
macro=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use Redirect'|cut -d ' ' -f 2) domain=${parts[2]}
redirect=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use Redirect'|cut -d ' ' -f 4) owner=${parts[3]}
if [[ -f /etc/apache2/sites-enabled/$c ]]; then fpm=${parts[4]}
if [[ -f /etc/apache2/sites-enabled/$domain.conf ]]; then
status="Enabled" status="Enabled"
else else
status="Disabled" status="Disabled"
fi fi
output="$output${NL}$domain $macro $redirect $status" # filter by username, if it was specified
if [[ -n $username ]]; then
if [[ $username = $owner ]]; then
output="$output${NL}$domain $owner $macro $fpm $status"
fi
else
output="$output${NL}$domain $owner $macro $fpm $status"
fi fi
done done
@ -123,16 +88,61 @@ elif [[ $option = 'redirects' ]]; then
echo "$output" | column -t echo "$output" | column -t
fi fi
elif [[ $option = 'aliases' ]]; then elif [[ $macro = 'Redirect' ]]; then
# working dir for aliases configs
cd /etc/apache2/sites-available
if [[ -n $domain ]]; then if [[ -n $domain ]]; then
if [[ -f $domain.conf ]]; then if [[ -f /etc/apache2/sites-available/$domain.conf ]]; then
if head -n 1 $domain.conf |grep --quiet '^Use VHostAlias'; then # get single RedirectHTTP(S) config for specified domain
vhostConfigs=("$domain.conf") readarray -t configs < <(head -n 1 /etc/apache2/sites-available/$domain.conf|grep --extended-regexp "^Use RedirectHTTP([S]?) $domain")
configcount=${#configs[@]}
if [ "$configcount" -eq 0 ]; then
echo "ERROR: $domain is not configured as a Redirect"
exit 1
fi
else else
echo "ERROR: $domain is not configured on this server."
exit 1
fi
else
if [[ -n $option ]]; then
# get array of RedirectHTTP(S) configs for specified Redirect URL
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use RedirectHTTP([S]?) .* $option$")
else
# get array of all RedirectHTTP(S) configs
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use RedirectHTTP([S]?) ")
fi
fi
output="domain macro redirect status"
for config in "${configs[@]}"
do
IFS=' ' read -r -a parts <<< "$config"
macro=${parts[1]}
domain=${parts[2]}
redirect=${parts[3]}
if [[ -f /etc/apache2/sites-enabled/$domain.conf ]]; then
status="Enabled"
else
status="Disabled"
fi
output="$output${NL}$domain $macro $redirect $status"
done
if [[ $cvs ]]; then
echo "$output" | tr " " ","
else
echo "$output" | column -t
fi
elif [[ $macro = 'VHostAlias' ]]; then
if [[ -n $domain ]]; then
if [[ -f /etc/apache2/sites-available/$domain.conf ]]; then
# get single VHostAliasHTTP(S) config for specified domain
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/$domain.conf|grep --extended-regexp "^Use VHostAliasHTTP([S]?) ")
configcount=${#configs[@]}
if [ "$configcount" -eq 0 ]; then
echo "ERROR: $domain is not configured as a Alias" echo "ERROR: $domain is not configured as a Alias"
exit 1 exit 1
fi fi
@ -141,24 +151,37 @@ elif [[ $option = 'aliases' ]]; then
exit 1 exit 1
fi fi
else else
vhostConfigs=(`ls -1`) if [[ -n $option ]]; then
# get array of VHostAliasHTTP(S) configs for specified aliased domain
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use VHostAliasHTTP([S]?) $option ")
else
# get array of all VHostAliasHTTP(S) configs
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use VHostAliasHTTP([S]?) ")
fi
fi fi
output="virtualhost username macro alias status" output="domain username macro php alias status"
for c in "${vhostConfigs[@]}" for config in "${configs[@]}"
do do
if head -n 1 /etc/apache2/sites-available/$c |grep --quiet '^Use VHostAlias'; then IFS=' ' read -r -a parts <<< "$config"
macro=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use VHostAlias'|cut -d ' ' -f 2) macro=${parts[1]}
alias=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use VHostAlias'|cut -d ' ' -f 3) domain=${parts[2]}
owner=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use VHostAlias'|cut -d ' ' -f 4) owner=${parts[3]}
virtualhost=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use VHostAlias'|cut -d ' ' -f 5) fpm=${parts[4]}
if [[ -f /etc/apache2/sites-enabled/$c ]]; then alias=${parts[5]}
if [[ -f /etc/apache2/sites-enabled/$domain.conf ]]; then
status="Enabled" status="Enabled"
else else
status="Disabled" status="Disabled"
fi fi
output="$output${NL}$alias $owner $macro $virtualhost $status" # filter by username, if it was specified
if [[ -n $username ]]; then
if [[ $username = $owner ]]; then
output="$output${NL}$alias $owner $macro $fpm $domain $status"
fi
else
output="$output${NL}$alias $owner $macro $fpm $domain $status"
fi fi
done done
@ -168,7 +191,148 @@ elif [[ $option = 'aliases' ]]; then
echo "$output" | column -t echo "$output" | column -t
fi fi
elif [[ $macro = 'VHostSubdomain' ]]; then
if [[ -n $domain ]]; then
if [[ -f /etc/apache2/sites-available/$domain.conf ]]; then
# get single VHostSubdomainHTTP(S) config for specified domain
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/$domain.conf|grep --extended-regexp "^Use VHostSubdomainHTTP([S]?) ")
configcount=${#configs[@]}
if [ "$configcount" -eq 0 ]; then
echo "ERROR: $domain is not configured as a VHostSubdomain"
exit 1
fi
else
echo "ERROR: $domain is not configured on this server."
exit 1
fi
else
# get array of all VHostSubdomainHTTP(S) configs
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use VHostSubdomainHTTP([S]?) ")
fi
output="domain username macro php status"
for config in "${configs[@]}"
do
IFS=' ' read -r -a parts <<< "$config"
macro=${parts[1]}
domain=${parts[5]}.${parts[2]}
owner=${parts[3]}
fpm=${parts[4]}
if [[ -f /etc/apache2/sites-enabled/$domain.conf ]]; then
status="Enabled"
else
status="Disabled"
fi
# filter by username, if it was specified
if [[ -n $username ]]; then
if [[ $username = $owner ]]; then
output="$output${NL}$domain $owner $macro $fpm $status"
fi
else
output="$output${NL}$domain $owner $macro $fpm $status"
fi
done
if [[ $cvs ]]; then
echo "$output" | tr " " ","
else
echo "$output" | column -t
fi
elif [[ $macro = 'VMail' ]]; then
if [[ -n $domain ]]; then
if [[ -f /etc/apache2/sites-available/$domain.conf ]]; then
# get single VMailHTTP(S) config for specified domain
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/$domain.conf|grep --extended-regexp "^Use VMailHTTP([S]?) ")
configcount=${#configs[@]}
if [ "$configcount" -eq 0 ]; then
echo "ERROR: $domain is not configured for VMailHTTP"
exit 1
fi
else
echo "ERROR: $domain is not configured on this server."
exit 1
fi
else
if [[ -n $option ]]; then
# get array of VMailHTTP(S) configs for specified aliased domain
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use VMailHTTP([S]?) $option ")
else
# get array of all VMailHTTP(S) configs
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use VMailHTTP([S]?) ")
fi
fi
output="domain macro status"
for config in "${configs[@]}"
do
IFS=' ' read -r -a parts <<< "$config"
macro=${parts[1]}
domain=${parts[2]}
if [[ -f /etc/apache2/sites-enabled/$domain.conf ]]; then
status="Enabled"
else
status="Disabled"
fi
output="$output${NL}$domain $macro $status"
done
if [[ $cvs ]]; then
echo "$output" | tr " " ","
else
echo "$output" | column -t
fi
elif [[ $macro = 'Mailman3' ]]; then
if [[ -n $domain ]]; then
if [[ -f /etc/apache2/sites-available/$domain.conf ]]; then
# get single Mailman3HTTP(S) config for specified domain
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/$domain.conf|grep --extended-regexp "^Use Mailman3HTTP([S]?) ")
configcount=${#configs[@]}
if [ "$configcount" -eq 0 ]; then
echo "ERROR: $domain is not configured for Mailman3"
exit 1
fi
else
echo "ERROR: $domain is not configured on this server."
exit 1
fi
else
if [[ -n $option ]]; then
# get array of Mailman3HTTP(S) configs for specified aliased domain
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use Mailman3HTTP([S]?) $option ")
else
# get array of all VMailHTTP(S) configs
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use Mailman3HTTP([S]?) ")
fi
fi
output="domain macro status"
for config in "${configs[@]}"
do
IFS=' ' read -r -a parts <<< "$config"
macro=${parts[1]}
domain=${parts[2]}
if [[ -f /etc/apache2/sites-enabled/$domain.conf ]]; then
status="Enabled"
else
status="Disabled"
fi
output="$output${NL}$domain $macro $status"
done
if [[ $cvs ]]; then
echo "$output" | tr " " ","
else
echo "$output" | column -t
fi
else else
echo "ERROR: Invalid mode '$option'" echo "ERROR: Invalid macro '$macro'"
exit 1 exit 1
fi fi