From f98efee4918905ea4603a53f09caedae5394c583 Mon Sep 17 00:00:00 2001 From: Andrew Stowell Date: Wed, 27 Sep 2023 15:16:00 -0400 Subject: [PATCH] more robust public ip look up. --- 4-public-ip.sh | 44 ++++++++++++++++++++++++++++++++++++++++---- 6-public-ip.sh | 43 +++++++++++++++++++++++++++++++++++++++++-- all-crons.sh | 32 ++++++++++++++++---------------- 3 files changed, 97 insertions(+), 22 deletions(-) diff --git a/4-public-ip.sh b/4-public-ip.sh index 1538e1e..fe13280 100755 --- a/4-public-ip.sh +++ b/4-public-ip.sh @@ -1,8 +1,44 @@ #!/bin/bash -# works consistently if you specify the -4 to force IPv4 -dig -4 +short A myip.opendns.com @resolver1.opendns.com +# there are a few different ways to resolve public-ip, +# keep trying until we get a result -#curl --fail --silent --show-error https://ipinfo.io/ip -#echo "" +# dig +if [[ -n $(command -v "dig") ]]; then + # via opendns + address=$(dig "@resolver1.opendns.com" -4 "myip.opendns.com" "A" "+short") + if [[ "$?" -ne "0" ]]; then + # dig was unsuccessful + echo "$address" 1>&2 + address="" + fi +fi + +# curl +if [[ -z "$address" ]] && [[ -n $(command -v "curl") ]]; then + # via ipinfo.io + address=$(curl -4 --fail --silent --show-error "https://ipinfo.io/ip") + + if [[ -z "$address" ]]; then + # via google + address=$(curl -4 --fail --silent --show-error "https://domains.google.com/checkip") + fi +fi + +# wget +if [[ -z "$address" ]] && [[ -n $(command -v "wget") ]]; then + # via ipinfo.io + address=$(wget -4 --quiet --output-document - "https://ipinfo.io/ip") + + if [[ -z "$address" ]]; then + # via google + address=$(wget -4 --quiet --output-document - "https://domains.google.com/checkip") + fi +fi + +if [[ -n "$address" ]]; then + echo "$address" +else + echo 'Tried everything, unable to resolve IP address.' 1>&2 +fi diff --git a/6-public-ip.sh b/6-public-ip.sh index 9bd7b32..84070ed 100755 --- a/6-public-ip.sh +++ b/6-public-ip.sh @@ -1,5 +1,44 @@ #!/bin/bash -# works consistently if you specify the -6 to force IPv6 -dig -6 +short AAAA myip.opendns.com @resolver1.opendns.com +# there are a few different ways to resolve public-ip, +# keep trying until we get a result + +# dig +if [[ -n $(command -v "dig") ]]; then + # via opendns + address=$(dig "@resolver1.opendns.com" -6 "myip.opendns.com" "AAAA" "+short") + if [[ "$?" -ne "0" ]]; then + # dig was unsuccessful + echo "$address" 1>&2 + address="" + fi +fi + +# curl +if [[ -z "$address" ]] && [[ -n $(command -v "curl") ]]; then + # via ipinfo.io + address=$(curl -6 --fail --silent --show-error "https://v6.ipinfo.io/ip") + + if [[ -z "$address" ]]; then + # via google + address=$(curl -6 --fail --silent --show-error "https://domains.google.com/checkip") + fi +fi + +# wget +if [[ -z "$address" ]] && [[ -n $(command -v "wget") ]]; then + # via ipinfo.io + address=$(wget -6 --quiet --output-document - "https://v6.ipinfo.io/ip") + + if [[ -z "$address" ]]; then + # via google + address=$(wget -6 --quiet --output-document - "https://domains.google.com/checkip") + fi +fi + +if [[ -n "$address" ]]; then + echo "$address" +else + echo 'Tried everything, unable to resolve IP address.' 1>&2 +fi diff --git a/all-crons.sh b/all-crons.sh index 2600d3d..a8aff02 100755 --- a/all-crons.sh +++ b/all-crons.sh @@ -1,24 +1,24 @@ #!/bin/bash -# this only works if your crontab uses three spaces for each section, -# like the following spacing example: -#m h dom mon dow command +# quickly generate a script file which will contain all crontab tasks. +# optionally takes filename to use, defaults to 'crontasks.sh'. +crontasks="$1" -# quickly generate a script file which will run each crontab task -# optionally takes filename to use, otherwise defaults 'crontasks.sh' -FILENAME="$1" - -if [ -z "$FILENAME" ]; then - FILENAME='crontasks.sh' +if [[ -z "$crontasks" ]]; then + crontasks='crontasks.sh' fi -echo "#!/bin/bash" > "$FILENAME" -echo "" >> "$FILENAME" +# prefix file we are generating +echo "#!/bin/bash" > "$crontasks" +echo "" >> "$crontasks" -# list crontab, remove comments and empty lines, -# expect cron time formatting to be the first 20 characters, -# so drop the 21st character onward to standard out. -crontab -l | grep -Ev '^(#|$)' | cut -c 21- >> "$FILENAME" +# list crontab, remove comments, convert tabs to space, squeeze the spaces, +# then take the sixth field "command" and add it to the file. +# could have issues if a task has a "#" in the middle. +crontab -l | grep -o '^[^#]*' | tr "\t" ' ' | tr -s ' ' | cut -s -d ' ' -f 6- >> "$crontasks" -chmod +x "$FILENAME" +echo "" >> "$crontasks" + +# make the file executable, so we are good to go +chmod +x "$crontasks"