Skip to content

Instantly share code, notes, and snippets.

@n0kovo
Last active February 27, 2023 06:46
Show Gist options
  • Save n0kovo/678cb1dca47249c9d4260e0d0947d309 to your computer and use it in GitHub Desktop.
Save n0kovo/678cb1dca47249c9d4260e0d0947d309 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Description: Resolve domains from stdin and print the IPs to stdout (with progress bar)
# Example: cat domains.txt | ./resolve.sh | sort | uniq -c | sort -nr
# Progress bar printing to stderr
function progress_bar {
progress=$(( (${1} * 100 / ${2} * 100) / 100 ))
done=$(( (${progress} * 6) / 10 ))
left=$((60 - $done))
# Build progressbar string lengths
fill=$(printf "%${done}s")
empty=$(printf "%${left}s")
}
# Read domains from stdin lines and resolve them
domains=$(cat -)
total=$(echo "$domains" | wc -l)
i=1
printf "$domains" | while read domain; do
# Resolve the domain
ip=$(dig +short "$domain" | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
# Increment the counter and print the progress bar
i=$((i + 1))
# Print if dig returned an IP
[[ $ip ]] &&
# To stderr
printf "\r\033[K" >&2 &&
echo -e "\e[32m$ip\e[0m" >&2 &&
# To stdout if not a tty or a pipe
[[ ! -t 1 ]] &&
[[ ! -p /dev/stdout ]] &&
echo "$ip"
# Print progress bar to stderr
printf "\rProgress : [${fill// /█}${empty// /▒}] ${progress}%%" >&2
progress_bar ${i} ${total}
done
# Clear the progress bar
printf "\r\033[K" >&2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment