Skip to content

Instantly share code, notes, and snippets.

@redneck-f25
Created August 16, 2019 09:19
Show Gist options
  • Save redneck-f25/51ba79d0d3ef8a43a2eacffb672fd9d2 to your computer and use it in GitHub Desktop.
Save redneck-f25/51ba79d0d3ef8a43a2eacffb672fd9d2 to your computer and use it in GitHub Desktop.
#!/bin/bash
{
cat <<'EOF'
01.example.com
02.example.com alias.example.com
# comment
03.example.com\
alias.example.com\
alias2.example.com\
04.example.com\
alias.example.com
05.example.com \
alias1.example.com \
alias2.example.com \
alias3.example.com \
# comment \
alias4.example.com \
alias5.example.com \
alias6.example.com
06.example.com \
# comment
07.example.com
EOF
} | {
DOMAINS_TXT=/dev/stdin
# Different sed version for different os types...
_sed() {
if [[ "${OSTYPE}" = "Linux" || "${OSTYPE:0:5}" = "MINGW" ]]; then
sed -r "${@}"
else
sed -E "${@}"
fi
}
reset_configvars() { :; }
# Generate certificates for all domains found in domains.txt. Check if existing certificate are about to expire
ORIGIFS="${IFS}"
IFS=$'\n'
line=''
for part in $(<"${DOMAINS_TXT}" tr -d '\r' | awk '{print tolower($0)}' | _sed -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*$//g' -e 's/[[:space:]]+/ /g' -e 's/([^ ])>/\1 >/g' -e 's/> />/g' -e 's/[[:space:]]+\\/\\/'); do
if [[ "${part: -1}" == '\' ]]; then
[[ "${part:0:1}" != '#' ]] && line="${line} ${part:0:-1}"
continue
elif [[ "${part:0:1}" != '#' ]]; then
line="${line} ${part}"
fi
[[ -z "${line//[[:space:]]/}" ]] && continue
line="${line:1}"
reset_configvars
IFS="${ORIGIFS}"
alias="$(grep -Eo '>[^ ]+' <<< "${line}" || true)"
line="$(_sed -e 's/>[^ ]+[ ]*//g' <<< "${line}")"
aliascount="$(grep -Eo '>' <<< "${alias}" | awk 'END {print NR}' || true )"
[ ${aliascount} -gt 1 ] && _exiterr "Only one alias per line is allowed in domains.txt!"
domain="$(printf '%s\n' "${line}" | cut -d' ' -f1)"
morenames="$(printf '%s\n' "${line}" | cut -s -d' ' -f2-)"
[ ${aliascount} -lt 1 ] && alias="${domain}" || alias="${alias#>}"
line=''
export alias
[[ -t 1 ]] && domain_display="\x1b[33;1m${domain}\x1b[0m" || domain_display="{domain}"
if [[ -z "${morenames}" ]];then
echo -e "Processing ${domain_display}"
else
echo -e "Processing ${domain_display} with alternative names: ${morenames}"
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment