Skip to content

Instantly share code, notes, and snippets.

@nick87720z
Last active June 28, 2021 11:23
Show Gist options
  • Save nick87720z/5b405351c34b1cb6c0998484599fb182 to your computer and use it in GitHub Desktop.
Save nick87720z/5b405351c34b1cb6c0998484599fb182 to your computer and use it in GitHub Desktop.
#!/bin/bash
TIMEOUT=${TIMEOUT:-10} # seconds
COLOR=${COLOR:-1} # 0 or 1
LOG_LEVEL=${LOG_LEVEL:-1} # 0..2
newline() { printf '\n'; }
line_over() { printf "${cl_back}"'\r%s' "$1"; }
cl_back="$(tput el1)"
if [ "${COLOR}" -eq 1 ]; then
col_red=$(tput setaf 1)
col_yellow=$(tput setaf 3)
sgr0=$(tput sgr0)
fi
re_file='[^:]*'
re_url_pre='[^#]*["([:space:]]'
re_host='[:@A-Za-z0-9~._-]*'
re_host_tail='[/")[:space:]].*'
printf "Total ebuilds ..."
ebuilds="$( find . -name '*.ebuild' )"
line_over "Total ebuilds: $( wc -l <<< "${ebuilds}" )"
newline
printf "Affected packages ..."
lines_affected="$( LANG=C xargs grep -E -e "^($re_url_pre)?http://$re_host($re_host_tail)?$" <<< "${ebuilds}" )"
line_over "Affected packages: $( { LANG=C grep -o -e "^$re_file" | uniq | wc -l ;} <<< "${lines_affected}" )"
newline
printf "Hosts to check ..."
hosts=$( { sed -E -n "{ s|^$re_file(:$re_url_pre)http://($re_host)($re_host_tail)?|"'\2\n\1|; tL; d; :L; P;D; }' | LANG=C grep -Fv '${' | sort -u ;} <<< "${lines_affected}" )
hosts_num=$( wc -l <<< "${hosts}" )
line_over "Hosts to check: ${hosts_num}"
newline
host_w=0
while read host; do
width=${#host}
host_w=$(( width > host_w ? width : host_w ))
done <<< "${hosts}"
printf "\nProcessing...\n"
{
while read host; do
printf '%s\n' ${host}
error=$( curl --connect-timeout ${TIMEOUT} -SsI "https://${host}" 2>&1 >/dev/null )
code=$?
printf '%s %s\n' "${code}" "${error}" | head -n1
done <<< "${hosts}"
} | {
COUNTER=0
while read host; do
COUNTER=$(( COUNTER + 1 ))
DAYS=$(( SECONDS / (24 * 3600) ))
HOURS=$(( SECONDS % (24 * 3600) / 3600 ))
MINS=$(( SECONDS % 3600 / 60 ))
SECS=$(( SECONDS % 60 ))
line_over "$( printf '%3i days %02i:%02i:%02i %'${#hosts_num}'i / %i: %-'${host_w}'s ' ${DAYS} ${HOURS} ${MINS} ${SECS} ${COUNTER} ${hosts_num} ${host} )"
read code error
case ${code} in
0 ) [ "${LOG_LEVEL}" -ge 2 ] && printf '\n%s' "${col_yellow}"
( LANG=C grep -F -e "http://$host" | LANG=C grep -o -e "^$re_file" \
|{ [ "${LOG_LEVEL}" -ge 2 ] && tee "/dev/stderr" || cat; } \
|xargs sed -E -i "s|http(://$host($re_host_tail)?)|${_s}https${_e}\\1|g;" ) <<< "${lines_affected}" 2>&1
;;
* ) [ "${LOG_LEVEL}" -ge 1 ] && printf '%s\n' "${col_red}${error}" ;;
esac
printf "${sgr0}"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment