Created
March 21, 2017 14:24
-
-
Save geekteq/930ebccf43fbd600b7a3cf708ae615ba to your computer and use it in GitHub Desktop.
update dynamic IP in UFW ssh access
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| HOSTNAME=dynamic.domain.com | |
| LOGFILE=$HOME/ufw.log | |
| Current_IP=$(host $HOSTNAME | head -n1 | cut -f4 -d ' ') | |
| if [ ! -f $LOGFILE ]; then | |
| /usr/sbin/ufw allow from $Current_IP to any port 22 proto tcp | |
| echo $Current_IP > $LOGFILE | |
| else | |
| Old_IP=$(cat $LOGFILE) | |
| if [ "$Current_IP" = "$Old_IP" ] ; then | |
| echo IP address has not changed | |
| else | |
| /usr/sbin/ufw delete allow from $Old_IP to any port 22 proto tcp | |
| /usr/sbin/ufw allow from $Current_IP to any port 22 proto tcp | |
| echo $Current_IP > $LOGFILE | |
| echo iptables have been updated | |
| fi | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment