Skip to content

Instantly share code, notes, and snippets.

@beancurd1
Last active November 10, 2021 15:27
Show Gist options
  • Save beancurd1/a55e299dc125810cba3561fda01642a7 to your computer and use it in GitHub Desktop.
Save beancurd1/a55e299dc125810cba3561fda01642a7 to your computer and use it in GitHub Desktop.
Freemyip or DYNU Dynamic IP Updater Linux Bash Script, check external every 60 seconds (please change it to a LARGER value if you need to change to avoid blacklist). It runs as a auto-start job on my CoreELEC Amlogic S905 box
#!/bin/sh
# It calls an external python script sendmail.py to email the new IP out
# This should work on both bash and sh shells e.g. LibreELEC/CoreELEC
# DYNU Dynamic IP Updater
# https://wiki.libreelec.tv/useful_shell_scripts#dynu_dynamic_ip_updater
# Modified to update Freemyip DYNDNS
# Modify These for DYNU or other DynDNS services e.g. Freemyip, DuckDNS, etc.
DYNU_USERNAME="username"
DYNU_PASSWORD="password-hash"
DYNU_HOSTNAME="example.dynu.com"
REFRESH_RATE=600
# Leave set to 0.0.0.0, will be initialized with a Publich IP during first run
PUBLIC_IP=0.0.0.0
test_ip ()
{
# Comparing IP Address $1=Existing IP $2=IP Fetched Externally
dt=$(date +"%r,%Y%m%d")
if [ -n $2 ] ; then
if [ $1 != $2 ] ; then
echo "New IP detected, Updating DynDNS & Existing IP $1 to New IP $2"
update_ip $2 $1
PUBLIC_IP=$2
else
echo "$1 - $2 IPs are the same, no action required"
fi
fi
}
update_ip ()
{
LOCAL_IP=$(ip addr show wlan0 | awk '/inet/ {print $2}' | sed 's#/.*##')
# DYNU_WGET="https://api.dynu.com/nic/update?hostname=$DYNU_HOSTNAME&myip=$1&username=$DYNU_USERNAME&password=$DYNU_PASSWORD"
FREEMYIP_WGET="https://freemyip.com/update?token=YOUR_TOKEN&domain=YOUR_SUBDOMAIN.freemyip.com&verbose=yes"
DYNU_UPDATE=`wget $FREEMYIP_WGET -O - -q ; echo`
dt=$(date +"%m-%d-%Y, %r")
echo "$DYNU_UPDATE NewIP=$1 OldIP=$2 LocalIP=$LOCAL_IP DateTime=$dt"
# send an email notification with new and old IPs
/storage/freemyip/sendmail.py $1 $2
(>&2 echo "$DYNU_UPDATE")
}
get_pubip ()
{
# Loop and Fetch a public IP from one of these servers
# break the loop only if IP returned from the servers is a true IP
URLS="ipecho.net/plain
checkip.dyndns.org
ifconfig.co
myexternalip.com/raw
canhazip.com
ifconfig.me
api6.ipify.org
plain-text-ip.com
ident.me
api.ipify.org"
while true; do
url=`echo ${URLS} | cut -d' ' -f$((1 + RANDOM % 10))`
PUBLIC_IP_TEST=`curl -s $url | sed 's#.*Address: \(.*\)</b.*#\1#'`
echo $PUBLIC_IP - $PUBLIC_IP_TEST, $url
if echo "$PUBLIC_IP_TEST" | grep "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$"; then
# Initialise $PUBLIC_IP when it first runs
if [ "$PUBLIC_IP" = "0.0.0.0" ]; then
PUBLIC_IP=$PUBLIC_IP_TEST
fi
break
else
dt=$(date +"%r,%Y%m%d")
echo "IP '$PUBLIC_IP_TEST' fetched from '$url' at $dt doesn't look like an IP address. Current IP '$PUBLIC_IP'"
fi
sleep 15
done
}
while true; do
get_pubip
test_ip $PUBLIC_IP $PUBLIC_IP_TEST
sleep $REFRESH_RATE
done
@iriki
Copy link

iriki commented Nov 10, 2021

Hello and thank you for the script. How can I hash the password? Is it MD5?

EDIT found it: https://www.dynu.com/NetworkTools/Hash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment