Last active
October 27, 2024 08:56
-
-
Save inceabdullah/d78ecbaf81ae77306846aaa79b84f34e to your computer and use it in GitHub Desktop.
Static Gateway for Dynamic A Recorded Domain
This file contains hidden or 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
| * * * * * /root/.crontab-scripts/dynamic-a-recorded-domain-on-static-gateway.sh |
This file contains hidden or 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 | |
| PREV_A_RECORD_FILE="a_record-prev.txt" | |
| PREV_A_RECORD=$( cat ${PREV_A_RECORD_FILE} ) | |
| #-- | |
| echo "PREV_A_RECORD= ${PREV_A_RECORD}" | |
| sleep 1 | |
| DOMAIN=example.com | |
| DNS=172.16.100.1 | |
| GATEWAY=172.16.100.1 | |
| #-- | |
| cat <<eof | |
| DOMAIN= ${DOMAIN} | |
| DNS= ${DNS} | |
| GATEWAY= ${GATEWAY} | |
| eof | |
| sleep 1 | |
| A_RECORD=$( dig @${DNS} +short ${DOMAIN} ) | |
| if [ ! "$?" -eq "0" ]; then | |
| echo "DOMAIN resolve problem" | |
| exit 1 | |
| fi | |
| #-- | |
| echo "A_RECORD= ${A_RECORD}" | |
| sleep 1 | |
| $( echo ${A_RECORD} > ${PREV_A_RECORD_FILE} ) | |
| # Check if the command sudo exists | |
| command -v sudo | |
| if [ "$?" -eq "0" ]; then | |
| SUDO_PRECMD="sudo " | |
| else | |
| SUDO_PRECMD="" | |
| fi | |
| # Delete prev a record gateway | |
| if [ ! -z "${PREV_A_RECORD}" ]; then | |
| IP_R_D_CMD="${SUDO_PRECMD}ip r d ${PREV_A_RECORD}/32" | |
| #-- | |
| echo "IP_R_D_CMD= ${IP_R_D_CMD}" | |
| bash -c "${IP_R_D_CMD}" | |
| fi | |
| IP_R_R_CMD="${SUDO_PRECMD}ip r r ${A_RECORD}/32 via ${GATEWAY}" | |
| #-- | |
| echo "IP_R_R_CMD= ${IP_R_R_CMD}" | |
| bash -c "${IP_R_R_CMD}" | |
| #--DONE | |
| echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment