1
0
mirror of https://github.com/vdm-io/dynamic-ip.git synced 2024-12-12 19:07:46 +00:00

Added tweaked echo function, completed the DNS update in getIP

This commit is contained in:
Llewellyn van der Merwe 2017-07-25 09:47:59 +01:00
parent e4782ee2df
commit f8e5e3161e
No known key found for this signature in database
GPG Key ID: CAD7B16D27AF28C5
2 changed files with 106 additions and 29 deletions

View File

@ -38,6 +38,9 @@ VDMIPSERVER="https://www.vdm.io/$ACTION"
############## ########## ############## ##########
############################################################## ##############################################################
function main () { function main () {
## set time for this run
echoTweak "$ACTION on $Datetimenow"
echo "started"
# get this server IP # get this server IP
HOSTIP="$(dig +short myip.opendns.com @resolver1.opendns.com)" HOSTIP="$(dig +short myip.opendns.com @resolver1.opendns.com)"
## make sure cron is set ## make sure cron is set
@ -57,6 +60,7 @@ function main () {
############## DEFAULTS ########## ############## DEFAULTS ##########
############## ########## ############## ##########
############################################################## ##############################################################
Datetimenow=$(TZ=":ZULU" date +"%m/%d/%Y @ %R (UTC)" )
VDMUSER=$(whoami) VDMUSER=$(whoami)
VDMHOME=~/ VDMHOME=~/
VDMSCRIPT="${REPOURL}$ACTION.sh" VDMSCRIPT="${REPOURL}$ACTION.sh"
@ -72,12 +76,37 @@ THEIPS=''
############## ########## ############## ##########
############################################################## ##############################################################
# little repeater
function repeat () {
head -c $1 < /dev/zero | tr '\0' $2
}
# little echo tweak
function echoTweak () {
echoMessage="$1"
mainlen="$2"
characters="$3"
if [ $# -lt 2 ]
then
mainlen=60
fi
if [ $# -lt 3 ]
then
characters='\056'
fi
chrlen="${#echoMessage}"
increaseBy=$((mainlen-chrlen))
tweaked=$(repeat "$increaseBy" "$characters")
echo -n "$echoMessage$tweaked"
}
# Set cronjob without removing existing # Set cronjob without removing existing
function setCron () { function setCron () {
if [ -f $VDMHOME/$ACTION.cron ]; then if [ -f $VDMHOME/$ACTION.cron ]; then
echo "Crontab already configured for updates...Skipping" echoTweak "Crontab already configured for updates..."
echo "Skipping"
else else
echo -n "Adding crontab entry for continued updates..." echoTweak "Adding crontab entry for continued updates..."
# check if user crontab is set # check if user crontab is set
currentCron=$(crontab -u $VDMUSER -l 2>/dev/null) currentCron=$(crontab -u $VDMUSER -l 2>/dev/null)
if [[ -z "${currentCron// }" ]]; then if [[ -z "${currentCron// }" ]]; then
@ -113,9 +142,10 @@ function getKey () {
function getLocalKey () { function getLocalKey () {
# Set update key # Set update key
if [ -f $VDMHOME/$ACTION.key ]; then if [ -f $VDMHOME/$ACTION.key ]; then
echo "Update key already set!" echoTweak "Update key already set!"
echo "continue"
else else
echo -n "Setting the update key..." echoTweak "Setting the update key..."
echo $(getKey) > $VDMHOME/$ACTION.key echo $(getKey) > $VDMHOME/$ACTION.key
echo "Done" echo "Done"
fi fi
@ -131,25 +161,26 @@ function setAccessToken () {
if [[ "$accessToke" != "$TRUE" ]]; then if [[ "$accessToke" != "$TRUE" ]]; then
read -s -p "Please enter your VDM access key: " vdmAccessKey read -s -p "Please enter your VDM access key: " vdmAccessKey
echo "" echo ""
echo -n "One moment while we set your access to the VDM system..." echoTweak "One moment while we set your access to the VDM system..."
resultAccess=$(curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36" -H "VDM-TRUST: $vdmAccessKey" -H "VDM-KEY: $VDMSERVERKEY" -H "VDM-HOST-IP: $HOSTIP" --silent $VDMIPSERVER) resultAccess=$(curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36" -H "VDM-TRUST: $vdmAccessKey" -H "VDM-KEY: $VDMSERVERKEY" -H "VDM-HOST-IP: $HOSTIP" --silent $VDMIPSERVER)
if [[ "$resultAccess" != "$TRUE" ]]; then if [[ "$resultAccess" != "$TRUE" ]]; then
echo " >> YOUR VDM ACCESS KEY IS INCORRECT! << $resultAccess" echo "YOUR VDM ACCESS KEY IS INCORRECT! >> $resultAccess"
exit 1 exit 1
fi fi
echo "Done" echo "Done"
else else
echo "Access granted to the VDM system." echoTweak "Access granted to the VDM system."
echo "Done"
fi fi
} }
function getIPs () { function getIPs () {
# store the IP in the HOSTNAME file # store the IP in the HOSTNAME file
echo -n "Getting the Dynamic IPs..." echoTweak "Getting the Dynamic IPs..."
THEIPS=$(curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36" -H "VDM-KEY: $VDMSERVERKEY" -H "VDM-HOST-IP: $HOSTIP" -H "VDM-GET: 1" --silent $VDMIPSERVER) THEIPS=$(curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36" -H "VDM-KEY: $VDMSERVERKEY" -H "VDM-HOST-IP: $HOSTIP" -H "VDM-GET: 1" --silent $VDMIPSERVER)
# the IPs # the IPs
if [[ "$THEIPS" == "$FALSE" || ${#THEIPS} -lt 15 ]]; then if [[ "$THEIPS" == "$FALSE" || ${#THEIPS} -lt 15 ]]; then
echo " >> No IPs FOUND! << " echo "No IPs FOUND! "
exit 1 exit 1
fi fi
echo "Done" echo "Done"
@ -162,42 +193,58 @@ function setDNS () {
row=( $rr ) row=( $rr )
if [[ ${#row[@]} == 3 ]]; then if [[ ${#row[@]} == 3 ]]; then
# first check IP # first check IP
echo -n "Checking the Dynamic IP..." echoTweak "Checking the Dynamic IP..."
if [[ "${row[2]}" =~ ^([0-9]{1,3})[.]([0-9]{1,3})[.]([0-9]{1,3})[.]([0-9]{1,3})$ ]] if [[ "${row[2]}" =~ ^([0-9]{1,3})[.]([0-9]{1,3})[.]([0-9]{1,3})[.]([0-9]{1,3})$ ]]
then then
for (( i=1; i<${#BASH_REMATCH[@]}; ++i )) for (( i=1; i<${#BASH_REMATCH[@]}; ++i ))
do do
if (( ${BASH_REMATCH[$i]} > 255 )); then if (( ${BASH_REMATCH[$i]} > 255 )); then
echo " >> bad IP << " echo "bad IP"
continue continue
fi fi
done done
else else
echo " >> bad IP << " echo "bad IP"
continue continue
fi fi
echo "Done" echo "Done"
echo -n "Checking local DNS file..." echoTweak "Checking local DNS file..."
# check if the DNS file is found (CENTOS) # check if the DNS file is found (CENTOS)
DNSfile=0 FILENAME=0
if [ -f "/var/named/${row[1]}.${row[0]}.db" ] if [ -f "/var/named/${row[1]}.${row[0]}.db" ]
then then
DNSfile=2 FILENAME="/var/named/${row[1]}.${row[0]}.db"
RELOADNAME="${row[1]}.${row[0]}"
elif [ -f "/var/named/${row[0]}.db" ] elif [ -f "/var/named/${row[0]}.db" ]
then then
DNSfile=1 FILENAME="/var/named/${row[0]}.db"
RELOADNAME="${row[0]}"
fi fi
# confirm that it was found # confirm that it was found
if [[ "$DNSfile" == 0 ]] if [[ "$FILENAME" == 0 ]]
then then
echo " >> not found << " echo "not found"
continue continue
fi fi
echo "Done" echo "Done"
# now add the IP A record if needed # now add the IP A record if needed
echo -n "Update DNS now.." echoTweak "Update DNS now.."
if grep -Fq "${row[2]}" "$FILENAME"
echo "done" then
# IP already set
echo "IP (${row[2]}) already set"
else
# code if not found
echoTweak "${row[1]}" 16 '\040' >> "$FILENAME"
echoTweak "1" 8 '\040' >> "$FILENAME"
echo "IN A ${row[2]}" >> "$FILENAME"
# Only reload the rndc if found
command -v rndc >/dev/null 2>&1 || {
rndc reload "$RELOADNAME" IN external
rndc reload "$RELOADNAME" IN internal
}
echo "Done"
fi
fi fi
done done
} }

View File

@ -38,6 +38,9 @@ VDMIPSERVER="https://www.vdm.io/$ACTION"
############## ########## ############## ##########
############################################################## ##############################################################
function main () { function main () {
## set time for this run
echoTweak "$ACTION on $Datetimenow"
echo "started"
## make sure cron is set ## make sure cron is set
setCron setCron
## get the local server key ## get the local server key
@ -53,6 +56,7 @@ function main () {
############## DEFAULTS ########## ############## DEFAULTS ##########
############## ########## ############## ##########
############################################################## ##############################################################
Datetimenow=$(TZ=":ZULU" date +"%m/%d/%Y @ %R (UTC)" )
VDMUSER=$(whoami) VDMUSER=$(whoami)
VDMHOME=~/ VDMHOME=~/
VDMSCRIPT="${REPOURL}$ACTION.sh" VDMSCRIPT="${REPOURL}$ACTION.sh"
@ -65,12 +69,37 @@ TRUE=1
############## ########## ############## ##########
############################################################## ##############################################################
# little repeater
function repeat () {
head -c $1 < /dev/zero | tr '\0' $2
}
# little echo tweak
function echoTweak () {
echoMessage="$1"
mainlen="$2"
characters="$3"
if [ $# -lt 2 ]
then
mainlen=60
fi
if [ $# -lt 3 ]
then
characters='\056'
fi
chrlen="${#echoMessage}"
increaseBy=$((mainlen-chrlen))
tweaked=$(repeat "$increaseBy" "$characters")
echo -n "$echoMessage$tweaked"
}
# Set cronjob without removing existing # Set cronjob without removing existing
function setCron () { function setCron () {
if [ -f $VDMHOME/$ACTION.cron ]; then if [ -f $VDMHOME/$ACTION.cron ]; then
echo "Crontab already configured for updates...Skipping" echoTweak "Crontab already configured for updates..."
echo "Skipping"
else else
echo -n "Adding crontab entry for continued updates..." echoTweak "Adding crontab entry for continued updates..."
# check if user crontab is set # check if user crontab is set
currentCron=$(crontab -u $VDMUSER -l 2>/dev/null) currentCron=$(crontab -u $VDMUSER -l 2>/dev/null)
if [[ -z "${currentCron// }" ]]; then if [[ -z "${currentCron// }" ]]; then
@ -106,9 +135,10 @@ function getKey () {
function getLocalKey () { function getLocalKey () {
# Set update key # Set update key
if [ -f $VDMHOME/$ACTION.key ]; then if [ -f $VDMHOME/$ACTION.key ]; then
echo "Update key already set!" echoTweak "Update key already set!"
echo "continue"
else else
echo -n "Setting the update key..." echoTweak "Setting the update key..."
echo $(getKey) > $VDMHOME/$ACTION.key echo $(getKey) > $VDMHOME/$ACTION.key
echo "Done" echo "Done"
fi fi
@ -124,10 +154,10 @@ function setAccessToken () {
if [[ "$accessToke" != "$TRUE" ]]; then if [[ "$accessToke" != "$TRUE" ]]; then
read -s -p "Please enter your VDM access key: " vdmAccessKey read -s -p "Please enter your VDM access key: " vdmAccessKey
echo "" echo ""
echo -n "One moment while we set your access to the VDM system..." echoTweak "One moment while we set your access to the VDM system..."
resultAccess=$(curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36" -H "VDM-TRUST: $vdmAccessKey" -H "VDM-KEY: $VDMSERVERKEY" --silent $VDMIPSERVER) resultAccess=$(curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36" -H "VDM-TRUST: $vdmAccessKey" -H "VDM-KEY: $VDMSERVERKEY" --silent $VDMIPSERVER)
if [[ "$resultAccess" != "$TRUE" ]]; then if [[ "$resultAccess" != "$TRUE" ]]; then
echo " >> YOUR VDM ACCESS KEY IS INCORRECT! << $resultAccess" echo "YOUR VDM ACCESS KEY IS INCORRECT! >> $resultAccess"
exit 1 exit 1
fi fi
echo "Done" echo "Done"
@ -140,10 +170,10 @@ function setIP () {
# get this server IP # get this server IP
IPNOW="$(dig +short myip.opendns.com @resolver1.opendns.com)" IPNOW="$(dig +short myip.opendns.com @resolver1.opendns.com)"
# store the IP in the HOSTNAME file # store the IP in the HOSTNAME file
echo -n "Setting/Update the Dynamic IP..." echoTweak "Setting/Update the Dynamic IP..."
resultUpdate=$(curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36" -H "VDM-KEY: $VDMSERVERKEY" -H "VDM-IP: $IPNOW" --silent $VDMIPSERVER) resultUpdate=$(curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36" -H "VDM-KEY: $VDMSERVERKEY" -H "VDM-IP: $IPNOW" --silent $VDMIPSERVER)
if [[ "$resultUpdate" != "$TRUE" ]]; then if [[ "$resultUpdate" != "$TRUE" ]]; then
echo " >> YOUR SERVER KEY IS INCORRECT! << $resultUpdate" echo "YOUR SERVER KEY IS INCORRECT! >> $resultUpdate"
exit 1 exit 1
fi fi
echo "Done" echo "Done"