update-home-ip.sh
· 1.4 KiB · Bash
Sin formato
#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# This will either allow automically with allowed IP or ask for auth otherwise
# 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;
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# call it every x minutes to stay up to day
# sudo crontab -e
# ------------------------------------------------------------------------------
# */5 * * * * /etc/nginx/update-home-ip.sh > /var/log/home_ip_update.log 2>&1
# ------------------------------------------------------------------------------
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 | # ------------------------------------------------------------------------------ |
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 | |
21 | CNAME="casinha.lema.org" |
22 | INCLUDE_FILE="/etc/nginx/home_allow.conf" |
23 | CURRENT_IP=$(dig +short "$CNAME" | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | head -n 1) |
24 | |
25 | if [[ -z "$CURRENT_IP" ]]; then |
26 | echo "Could not resolve $CNAME" |
27 | exit 1 |
28 | fi |
29 | |
30 | NEW_CONTENT="allow $CURRENT_IP;\ndeny all;" |
31 | |
32 | # Only reload if IP changed |
33 | if ! 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" |
37 | else |
38 | echo "IP unchanged, nothing to do." |
39 | fi |
40 |