Skip to content

Instantly share code, notes, and snippets.

@mskian
Last active May 21, 2023 11:55
Show Gist options
  • Save mskian/2afa263f0de9c0be72e0cc55ca92a6b9 to your computer and use it in GitHub Desktop.
Save mskian/2afa263f0de9c0be72e0cc55ca92a6b9 to your computer and use it in GitHub Desktop.
Pi-hole - A Simple Bash Script to Update the Home/ISP ip address in UFW Firewall to access port 53
VPSIP=XXX.XX.XX.X
PASSWORD='SERVERPASSWORD'
#!/bin/bash
# Setup Reference: https://blog.sbstp.ca/vps-pihole/
# Load ENV Variables
. .env
# GET IP
ip=$(curl -s https://api.ipify.org)
# File Name
ip_file="ip_data.txt"
# Load VPS IP from ENV
vps_ip=$VPSIP
# Verify File exist or not
if [ -f "$ip_file" ]; then
source $ip_file
echo "Get IP from data file: $data"
else
echo "$ip_file does not exist."
exit 1
fi
# Updating Firewall Rules
if [ $ip = $data ]; then
echo "IP Exist in UFW Rules - Readded for Verification"
sleep 2;
sshpass -p $PASSWORD ssh root@$vps_ip "ufw allow from $ip to any port 53 && ufw reload"
else
sshpass -p $PASSWORD ssh root@$vps_ip "ufw delete allow from $data to any port 53 && ufw reload"
sleep 2;
sshpass -p $PASSWORD ssh root@$vps_ip "ufw allow from $ip to any port 53 && ufw reload"
sleep 2;
echo "data=$ip" > $ip_file
fi
#!/bin/bash
# Setup Reference: https://blog.sbstp.ca/vps-pihole/
ip=$(curl -s https://api.ipify.org)
ip_file="ip_data.txt"
echo "data=$ip" > $ip_file
echo "Your Network IP is $ip"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment