Skip to content

Instantly share code, notes, and snippets.

@yurinnick
Created November 3, 2017 04:57
Show Gist options
  • Save yurinnick/35af5724cb8d927439f54989e1e134d3 to your computer and use it in GitHub Desktop.
Save yurinnick/35af5724cb8d927439f54989e1e134d3 to your computer and use it in GitHub Desktop.
Automatically update VPN gateway IP
#!/bin/bash
vpn_host="netherlands-udp.ivacy.net"
log_file="/var/log/vpn_ip_update.log"
log() {
echo ${1} | tee ${log_file}
}
new_ip=$(host ${vpn_host} | awk '/has address/ { print $4 }')
current_rule=$(ufw status numbered | awk '/53\/udp/ { print $2 }' | cut -c 1)
current_ip=$(ufw status numbered | awk '/53\/udp/ { print $3 }')
log "VPN: ${vpn_host} has address ${new_ip}"
log "UFW: ${vpn_host} has address ${current_ip}"
if [ ${current_ip} == ${new_ip} ]; then
log "IPs match, no restart necessary"
else
log "Updating last IP with current"
ufw --force delete ${current_rule}
ufw allow out to ${new_ip} port 53 proto udp
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment