1
0
mirror of https://github.com/vdm-io/dynamic-ip.git synced 2024-10-31 18:52:38 +00:00
dynamic-ip/setip.sh

158 lines
6.6 KiB
Bash
Raw Normal View History

2017-07-05 01:24:52 +00:00
#!/bin/bash
#/--------------------------------------------------------------------------------------------------------| www.vdm.io |------/
# __ __ _ _____ _ _ __ __ _ _ _
# \ \ / / | | | __ \ | | | | | \/ | | | | | | |
# \ \ / /_ _ ___| |_ | | | | _____ _____| | ___ _ __ _ __ ___ ___ _ __ | |_ | \ / | ___| |_| |__ ___ __| |
# \ \/ / _` / __| __| | | | |/ _ \ \ / / _ \ |/ _ \| '_ \| '_ ` _ \ / _ \ '_ \| __| | |\/| |/ _ \ __| '_ \ / _ \ / _` |
# \ / (_| \__ \ |_ | |__| | __/\ V / __/ | (_) | |_) | | | | | | __/ | | | |_ | | | | __/ |_| | | | (_) | (_| |
# \/ \__,_|___/\__| |_____/ \___| \_/ \___|_|\___/| .__/|_| |_| |_|\___|_| |_|\__| |_| |_|\___|\__|_| |_|\___/ \__,_|
# | |
# |_|
#/-------------------------------------------------------------------------------------------------------------------------------/
#
# @version 1.0.0
# @build 21st July, 2017
# @package Dynamic IP
# @author Llewellyn van der Merwe <https://github.com/Llewellynvdm>
# @copyright Copyright (C) 2015. All Rights Reserved
# @license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
#
#/-----------------------------------------------------------------------------------------------------------------------------/
2017-07-05 00:34:40 +00:00
############################ GLOBAL ##########################
ACTION="setip"
######### DUE TO NOT BEING ABLE TO INCLUDE DYNAMIC ###########
#################### UPDATE TO YOUR NEEDS ####################
##############################################################
############## ##########
############## CONFIG ##########
############## ##########
##############################################################
2017-07-21 23:25:05 +00:00
REPOURL="https://raw.githubusercontent.com/vdm-io/dynamic-ip/master/"
VDMIPSERVER="https://www.vdm.io/$ACTION"
##############################################################
############## ##########
############## MAIN ##########
############## ##########
##############################################################
function main () {
## make sure cron is set
setCron
## get the local server key
getLocalKey
## check access (set if not ready)
setAccessToken
## update IP
setIP
2017-07-05 00:34:40 +00:00
}
##############################################################
############## ##########
############## DEFAULTS ##########
############## ##########
##############################################################
VDMUSER=$(whoami)
VDMHOME=~/
VDMSCRIPT="${REPOURL}$ACTION.sh"
VDMSERVERKEY=''
2017-07-05 00:34:40 +00:00
TRUE=1
##############################################################
############## ##########
############## FUNCTIONS ##########
############## ##########
##############################################################
2017-07-05 00:34:40 +00:00
# Set cronjob without removing existing
function setCron () {
if [ -f $VDMHOME/$ACTION.cron ]; then
echo "Crontab already configured for updates...Skipping"
else
echo -n "Adding crontab entry for continued updates..."
# check if user crontab is set
currentCron=$(crontab -u $VDMUSER -l 2>/dev/null)
if [[ -z "${currentCron// }" ]]; then
currentCron="# VDM crontab settings"
echo "$currentCron" > $VDMHOME/$ACTION.cron
else
echo "$currentCron" > $VDMHOME/$ACTION.cron
fi
# check if the MAILTO is already set
if [[ $currentCron != *"MAILTO"* ]]; then
echo "MAILTO=\"\"" >> $VDMHOME/$ACTION.cron
echo "" >> $VDMHOME/$ACTION.cron
fi
# check if the @reboot curl -s $VDMSCRIPT | sudo bash is already set
2017-07-21 23:12:17 +00:00
if [[ $currentCron != *"@reboot curl -s $VDMSCRIPT | bash"* ]]; then
echo "@reboot curl -s $VDMSCRIPT | bash" >> $VDMHOME/$ACTION.cron
fi
# check if the @reboot curl -s $VDMSCRIPT | sudo bash is already set
2017-07-21 23:12:17 +00:00
if [[ $currentCron != *"*/5 * * * * curl -s $VDMSCRIPT | bash"* ]]; then
echo "*/5 * * * * curl -s $VDMSCRIPT | bash" >> $VDMHOME/$ACTION.cron
fi
# set the user cron
crontab -u $VDMUSER $VDMHOME/$ACTION.cron
echo "Done"
2017-07-05 00:34:40 +00:00
fi
}
function getKey () {
# simple basic random
echo $(tr -dc 'A-HJ-NP-Za-km-z2-9' < /dev/urandom | dd bs=128 count=1 status=none)
}
function getLocalKey () {
# Set update key
if [ -f $VDMHOME/$ACTION.key ]; then
echo "Update key already set!"
else
echo -n "Setting the update key..."
echo $(getKey) > $VDMHOME/$ACTION.key
echo "Done"
2017-07-05 00:34:40 +00:00
fi
# Get update key
VDMSERVERKEY=$(<"$VDMHOME/$ACTION.key")
}
2017-07-05 00:34:40 +00:00
function setAccessToken () {
# check if vdm access was set
accessToke=$(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" --silent $VDMIPSERVER)
2017-07-05 00:34:40 +00:00
if [[ "$accessToke" != "$TRUE" ]]; then
read -s -p "Please enter your VDM access key: " vdmAccessKey
echo ""
echo -n "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)
if [[ "$resultAccess" != "$TRUE" ]]; then
echo " >> YOUR VDM ACCESS KEY IS INCORRECT! << $resultAccess"
exit 1
fi
echo "Done"
else
echo "Access granted to the VDM system."
fi
}
2017-07-05 00:34:40 +00:00
function setIP () {
# get this server IP
IPNOW="$(dig +short myip.opendns.com @resolver1.opendns.com)"
# store the IP in the HOSTNAME file
echo -n "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)
if [[ "$resultUpdate" != "$TRUE" ]]; then
echo " >> YOUR SERVER KEY IS INCORRECT! << $resultUpdate"
2017-07-05 00:34:40 +00:00
exit 1
fi
echo "Done"
}
2017-07-05 00:34:40 +00:00
##############################################################
############## ##########
############## MAIN ##########
############## ##########
##############################################################
main