demo-sync/setip.sh

92 lines
3.1 KiB
Bash
Raw Normal View History

2017-07-05 01:24:52 +00:00
#!/bin/bash
2017-07-05 00:34:40 +00:00
# simple basic random
function getKey () {
echo $(tr -dc 'A-HJ-NP-Za-km-z2-9' < /dev/urandom | dd bs=128 count=1 status=none)
}
# Variables
TRUE=1
VDMUSER="vdm"
VDMHOME="/home/vdm"
2017-07-18 18:38:48 +00:00
VDMSCRIPT="https://raw.githubusercontent.com/vdm-io/dynamic-ip/master/setip.sh"
2017-07-05 00:34:40 +00:00
VDMIPSERVER="https://www.vdm.io/setip"
# Require script to be run via sudo, but not as root
2017-07-05 01:28:04 +00:00
#if [[ $EUID -ne 0 ]]; then
#
# echo "Script must be run with root privilages!"
# exit 1
#fi
2017-07-05 00:34:40 +00:00
# Set cronjob without removing existing
if [ -f $VDMHOME/setip.cron ]; then
2017-07-05 00:34:40 +00:00
echo "Crontab already configured for updates...Skipping"
else
echo -n "Adding crontab entry for continued updates..."
2017-07-05 22:47:08 +00:00
# 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/setip.cron
2017-07-05 22:47:08 +00:00
else
echo "$currentCron" > $VDMHOME/setip.cron
2017-07-05 22:47:08 +00:00
fi
2017-07-05 00:34:40 +00:00
# check if the MAILTO is already set
if [[ $currentCron != *"MAILTO"* ]]; then
echo "MAILTO=\"\"" >> $VDMHOME/setip.cron
echo "" >> $VDMHOME/setip.cron
2017-07-05 00:34:40 +00:00
fi
# check if the @reboot curl -s $VDMSCRIPT | sudo bash is already set
if [[ $currentCron != *"@reboot curl -s $VDMSCRIPT | sudo bash"* ]]; then
echo "@reboot curl -s $VDMSCRIPT | sudo bash" >> $VDMHOME/setip.cron
2017-07-05 00:34:40 +00:00
fi
# check if the @reboot curl -s $VDMSCRIPT | sudo bash is already set
if [[ $currentCron != *"*/5 * * * * curl -s $VDMSCRIPT | sudo bash"* ]]; then
echo "*/5 * * * * curl -s $VDMSCRIPT | sudo bash" >> $VDMHOME/setip.cron
2017-07-05 00:34:40 +00:00
fi
# set the user cron
crontab -u $VDMUSER $VDMHOME/setip.cron
2017-07-05 00:34:40 +00:00
echo "Done"
fi
# Set update key
if [ -f $VDMHOME/setip.key ]; then
2017-07-05 00:34:40 +00:00
echo "Update key already set!"
else
echo -n "Setting the update key..."
echo $(getKey) > $VDMHOME/setip.key
2017-07-05 00:34:40 +00:00
echo "Done"
fi
# Get update key
serverKey=$(<"$VDMHOME/setip.key")
2017-07-05 00:34:40 +00:00
# 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: $serverKey" --silent $VDMIPSERVER)
2017-07-05 00:34:40 +00:00
if [[ "$accessToke" != "$TRUE" ]]; then
2017-07-05 01:24:52 +00:00
read -s -p "Please enter your VDM access key: " vdmAccessKey
echo ""
2017-07-05 00:34:40 +00:00
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: $serverKey" --silent $VDMIPSERVER)
2017-07-05 00:34:40 +00:00
if [[ "$resultAccess" != "$TRUE" ]]; then
echo " >> YOUR VDM ACCESS KEY IS INCORRECT! <<"
exit 1
fi
echo "Done"
else
echo "Access granted to the VDM system."
fi
# 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: $serverKey" -H "VDM-IP: $IPNOW" --silent $VDMIPSERVER)
2017-07-05 00:34:40 +00:00
if [[ "$resultUpdate" != "$TRUE" ]]; then
echo " >> YOUR SERVER KEY IS INCORRECT! <<"
2017-07-05 00:34:40 +00:00
exit 1
fi
echo "Done"