Skip to content

Instantly share code, notes, and snippets.

@dvalfrid
Last active January 20, 2021 15:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dvalfrid/33a775b16bc333675ea9 to your computer and use it in GitHub Desktop.
Save dvalfrid/33a775b16bc333675ea9 to your computer and use it in GitHub Desktop.
Bash script for updating DNS server at joker.com
#!/bin/bash
# Fetching IP adress from joker.com
# Format: <html><head><title>Current IP Check</title></head><body>Current IP Address: 88.80.167.132</body></html>
function fetchIP {
local _returnParam=$1
local _response=$(curl --silent https://svc.joker.com/nic/checkip)
local _exit_code=$?
if [ $_exit_code -ne 0 ]; then
(
logger "Faild to fetch new IP adress"
)
fi
eval $_returnParam="'$_response'"
}
# Extract IP adress from response
function extractIP {
local _returnParam=$1 # <html><head><title>Current IP Check</title></head><body>Current IP Address: 88.80.167.132</body></html>
local _current_ip=$(echo $2 | sed -n -e 's/.*IP Address: \([0-9\.]*\).*/\1/p')
local _exit_code=$?
if [ $_exit_code -ne 0 ]; then
logger "[WARNING] Faild to match IP number"
fi
eval $_returnParam="$_current_ip"
}
# Fetching old IP from file
function oldIP {
local _returnParam=$1
local _file=$2
if [ ! -f "$_file" ]; then # Check if file exists
touch "$_file" # Create file
fi
eval $_returnParam="`cat $_file`"
}
# Update DNS Server
function updateDNS {
local _current_ip=$1
local _updateResponse=$(curl --write-out %{http_code} --silent --output /dev/null $3)
local _update_exit_code=$?
if [ $_update_exit_code -ne 0 ]; then # Curl could not make the call
(
logger "[Warning] Failed to update DNS provider, exit code: $_update_exit_code"
)
else
(
if [ $_updateResponse -ne 200 ]; then # Response call was not 200
(
logger "[Warning] Faild to update DNS provider, response code: $_updateResponse"
)
else
(
echo $_current_ip > $2 # Save current IP to file
logger "[Info] Old IP: $old_ip, New IP: $_current_ip"
)
fi
)
fi
}
# Main program
logger "[Info] DNS update started"
file="/usr/local/var/log/myip.file3" # Local storage for IP number
fetchIP response # Fetching the IP
extractIP currnet_ip "$response" # Extracting IP from response
oldIP old_ip "$file" # Read old IP adress from file
if [ "$currnet_ip" == "$old_ip" ]; then # Check if old and current IP are same
(
logger "[Info] Same IP adress: $old_ip"
)
else # They are not the same
(
updateDNS $currnet_ip $file "https://svc.joker.com/nic/update?username=<username>&password=<passowrd>&hostname=<domain>&myip=$currnet_ip&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"
)
fi
logger "[Info] DNS update finished"
@dvalfrid
Copy link
Author

First version for DNS updating script

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