Skip to content

Instantly share code, notes, and snippets.

@nicksnell
Created November 23, 2012 14:21
Show Gist options
  • Select an option

  • Save nicksnell/4135838 to your computer and use it in GitHub Desktop.

Select an option

Save nicksnell/4135838 to your computer and use it in GitHub Desktop.
UFW Updater (MySQL)
#!/bin/bash
target_hosts="dynhost.does-not-exist.com another-host.does-not-exist.com"
if [ -f "/root/mysql-allow-hosts-current" ]; then
mv /root/mysql-allow-hosts-current /root/mysql-allow-hosts-prev
fi
touch /root/mysql-allow-hosts-current
if [ -f "/root/mysql-allow-hosts-prev" ]; then
# Remove previously set firewall allows
for prev_ip in `cat /root/mysql-allow-hosts-prev`; do
ufw delete allow from $prev_ip to any port 3306 > /dev/null
done
fi
for target_host in $target_hosts; do
# Look up IP per host
# echo "Looking up IP for host:" $target_host
target_ip=`host $target_host | cut -d ' ' -f 4`
if [ $? -eq 0 ]; then
echo $target_ip >> /root/mysql-allow-hosts-current
ufw allow from $target_ip to any port 3306 > /dev/null
fi
done
@nicksnell
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment