diff --git a/.gitignore b/.gitignore index 1e86000..a68ce0c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ ip-trigger-storage venv/ -__pychache__/ +__pycache__/ *.pyc diff --git a/README.md b/README.md index ce73d24..4e12098 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Docker, docker-compose. # Setup -Clone the repo `git clone git@github.com:vekerdyb/ip-trigger.py` +Clone the repo `git clone git@github.com:vekerdyb/ip-trigger.git` Rename `config.py.tmp` to `config.py` and change the values. Uncomment any backend you want to use. diff --git a/cloudflare_backend.py b/cloudflare_backend.py index a2ed721..e192e11 100644 --- a/cloudflare_backend.py +++ b/cloudflare_backend.py @@ -6,8 +6,6 @@ logger = logging.getLogger(__name__) -URL = 'https://api.cloudflare.com/client/v4/' - def on_ip_change(new_ip, log_handler=logger): log_handler.debug('Attempting to update Cloudflare IP for host {} to {}'.format( diff --git a/ip-trigger.py b/ip-trigger.py index ef85c41..42444e9 100755 --- a/ip-trigger.py +++ b/ip-trigger.py @@ -48,18 +48,24 @@ def curl_get(url, interface=None): def store_ip(ip): logger.debug('Attempting to open "{}" for appending'.format(STORAGE_FILE)) - file = open(STORAGE_FILE, "a") - file.write(ip + "\n") - file.close() + with open(STORAGE_FILE, "a") as file: + file.write(ip + "\n") logger.debug("IP {} stored in file".format(ip)) def retrieve_last_ip_from_storage(): logger.debug('Attempting to retrieve last IP from "{}"'.format(STORAGE_FILE)) - last_ip = subprocess.check_output(["tail", "-1", STORAGE_FILE]) - last_ip = last_ip.strip().decode("utf-8") - logger.debug('Last IP: "{}"'.format(last_ip)) - return last_ip + try: + last_ip = subprocess.check_output(["tail", "-1", STORAGE_FILE]) + last_ip = last_ip.strip().decode("utf-8") + if not last_ip: + logger.debug("Storage file is empty") + return "" + logger.debug('Last IP: "{}"'.format(last_ip)) + return last_ip + except subprocess.CalledProcessError: + logger.debug("Storage file does not exist or cannot be accessed") + return "" def detect_current_ip(): diff --git a/railgun_backend.py b/railgun_backend.py index 19c5b38..b061a6d 100644 --- a/railgun_backend.py +++ b/railgun_backend.py @@ -22,7 +22,7 @@ def _send_simple_message(from_name, to, subject, text): def on_ip_change(new_ip, log_handler=logger): log_handler.debug( - 'Sending email to "{}", becuase IP changed to "{}"'.format(config.RAILGUN['EMAIL_RECIPIENT'], new_ip)) + 'Sending email to "{}", because IP changed to "{}"'.format(config.RAILGUN['EMAIL_RECIPIENT'], new_ip)) response = _send_simple_message( from_name=config.RAILGUN['FROM_NAME'],