update-home-ip.sh
· 745 B · Bash
Eredeti
#!/usr/bin/env bash
# in /etc/nginx/sites-available/search.lema.org
# satisfy any;
# include /etc/nginx/home_allow.conf;
# auth_basic "Restricted";
# auth_basic_user_file /etc/nginx/.htpasswd;
CNAME="casinha.lema.org"
INCLUDE_FILE="/etc/nginx/home_allow.conf"
CURRENT_IP=$(dig +short "$CNAME" | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | head -n 1)
if [[ -z "$CURRENT_IP" ]]; then
echo "Could not resolve $CNAME"
exit 1
fi
NEW_CONTENT="allow $CURRENT_IP;\ndeny all;"
# Only reload if IP changed
if ! grep -q "allow $CURRENT_IP;" "$INCLUDE_FILE"; then
echo -e "$NEW_CONTENT" > "$INCLUDE_FILE"
nginx -t && nginx -s reload
echo "Updated Nginx allow list with IP $CURRENT_IP"
else
echo "IP unchanged, nothing to do."
fi
| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # in /etc/nginx/sites-available/search.lema.org |
| 4 | # satisfy any; |
| 5 | # include /etc/nginx/home_allow.conf; |
| 6 | # auth_basic "Restricted"; |
| 7 | # auth_basic_user_file /etc/nginx/.htpasswd; |
| 8 | |
| 9 | |
| 10 | |
| 11 | CNAME="casinha.lema.org" |
| 12 | INCLUDE_FILE="/etc/nginx/home_allow.conf" |
| 13 | CURRENT_IP=$(dig +short "$CNAME" | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | head -n 1) |
| 14 | |
| 15 | if [[ -z "$CURRENT_IP" ]]; then |
| 16 | echo "Could not resolve $CNAME" |
| 17 | exit 1 |
| 18 | fi |
| 19 | |
| 20 | NEW_CONTENT="allow $CURRENT_IP;\ndeny all;" |
| 21 | |
| 22 | # Only reload if IP changed |
| 23 | if ! grep -q "allow $CURRENT_IP;" "$INCLUDE_FILE"; then |
| 24 | echo -e "$NEW_CONTENT" > "$INCLUDE_FILE" |
| 25 | nginx -t && nginx -s reload |
| 26 | echo "Updated Nginx allow list with IP $CURRENT_IP" |
| 27 | else |
| 28 | echo "IP unchanged, nothing to do." |
| 29 | fi |
| 30 |