Created
January 26, 2021 20:31
-
-
Save ANAT01/3d910a09d8cf3c8c526d1d71c859917e to your computer and use it in GitHub Desktop.
Script for automaticly update DNS records in Beget hosting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script for automaticly update DNS records in Beget hosting througth Beget.API | |
# Documentation for Beget.API https://beget.ru/api | |
#User Settings | |
BEGET_LOGIN='username' | |
BEGET_PASS='passowrd' | |
DOMAINNAME='example.com' | |
#Empty value for errors | |
ERROR='' | |
MESSAGE='' | |
#Colors | |
RED='\033[0;31m' | |
GRN='\033[0;32m' | |
YEL='\033[1;33m' | |
NC='\033[0m' # No Color | |
failreport() | |
{ | |
clear | |
printf "=== ${RED}FAIL!!!${NC}===\n" | |
printf "${YEL}Ошибка:${NC} $ERROR\n" | |
printf "${YEL}Значение:${NC}\n$MESSAGE\n" | |
} | |
HOST_IP="$(wget -O - -q icanhazip.com)" | |
#HOST_IP='1.1.1.1' | |
if [[ $HOST_IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]];then | |
printf "IP for current machine: ${GRN}$HOST_IP${NC}\n"; | |
GETDNS=$(\ | |
curl -q "https://api.beget.ru/api/dns/getData" \ | |
-d login=$BEGET_LOGIN \ | |
-d passwd=$BEGET_PASS \ | |
-d input_format=json \ | |
-d output_format=json \ | |
--data-urlencode input_data='{"fqdn":"'$DOMAINNAME'"}' \ | |
2>/dev/null | |
) | |
#Check result for GETDNS request | |
if [[ `echo $GETDNS | jshon -e status` == '"success"' ]];then | |
#Check current DNS record | |
DNSIPRECORD=$( | |
echo $GETDNS |\ | |
jshon -e answer -e result -e records -e A -e 0 -e value |\ | |
sed "s/^\([\"']\)\(.*\)\1\$/\2/g" | |
) | |
printf "Read DNS: ${GRN}$DNSIPRECORD${NC}\n" | |
#Check equal local and remote DNS values | |
if [[ $DNSIPRECORD != $HOST_IP ]];then | |
SETDNS=$(curl "https://api.beget.ru/api/dns/changeRecords" \ | |
-d "login=$BEGET_LOGIN" \ | |
-d "passwd=$BEGET_PASS" \ | |
-d "input_format=json" \ | |
-d "output_format=json" \ | |
--data-urlencode 'input_data= | |
{ | |
"fqdn":"'$DOMAINNAME'", | |
"records": | |
{ | |
"A": | |
[ | |
{ | |
"priority":10, | |
"value":"'$HOST_IP'" | |
} | |
], | |
"MX": | |
[ | |
{ | |
"priority":10, | |
"value":"mx1.beget.ru" | |
}, | |
{ | |
"priority":20, | |
"value":"mx2.beget.ru" | |
} | |
] | |
} | |
}'\ | |
2>/dev/null | |
) | |
#Check status of request to record | |
if [[ `echo $GETDNS | jshon -e status` == '"success"' ]];then | |
printf "${GRN}Success!\n" | |
printf "${GRN}DNS 'A' record for $DOMAINNAME was updated to $HOST_IP${NC}\n" | |
else | |
ERROR='Не записать новый IP адрес' | |
MESSAGE="GETDNS = ${RED}$SETDNS${NC}" | |
failreport | |
fi | |
else | |
printf "${GRN}All rights. DNS records true${NC}\n" | |
fi | |
else | |
ERROR='Не удалось выполнить запрос Beget.API' | |
MESSAGE="GETDNS = ${RED}$GETDNS${NC}" | |
failreport | |
fi | |
else | |
ERROR='Не определент IP адрес данного компьютера' | |
MESSAGE="HOST_IP = ${RED}$HOST_IP${NC}" | |
failreport | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment