Son aktivite 1747064984

Script to update home IP in nginx conf

update-home-ip.sh Ham
1#!/usr/bin/env bash
2
3# ------------------------------------------------------------------------------
4# This will either allow automically with allowed IP or ask for auth otherwise
5# in /etc/nginx/sites-available/search.lema.org
6# ------------------------------------------------------------------------------
7# satisfy any;
8# include /etc/nginx/home_allow.conf;
9# auth_basic "Restricted";
10# auth_basic_user_file /etc/nginx/.htpasswd;
11# ------------------------------------------------------------------------------
12
13# ------------------------------------------------------------------------------
14# call it every x minutes to stay up to day
15# sudo crontab -e
16# ------------------------------------------------------------------------------
17# */5 * * * * /etc/nginx/update-home-ip.sh > /var/log/home_ip_update.log 2>&1
18# ------------------------------------------------------------------------------
19
20
21CNAME="casinha.lema.org"
22INCLUDE_FILE="/etc/nginx/home_allow.conf"
23CURRENT_IP=$(dig +short "$CNAME" | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | head -n 1)
24
25if [[ -z "$CURRENT_IP" ]]; then
26 echo "Could not resolve $CNAME"
27 exit 1
28fi
29
30NEW_CONTENT="allow $CURRENT_IP;\ndeny all;"
31
32# Only reload if IP changed
33if ! grep -q "allow $CURRENT_IP;" "$INCLUDE_FILE"; then
34 echo -e "$NEW_CONTENT" > "$INCLUDE_FILE"
35 nginx -t && nginx -s reload
36 echo "Updated Nginx allow list with IP $CURRENT_IP"
37else
38 echo "IP unchanged, nothing to do."
39fi
40