Created
February 19, 2014 00:02
-
-
Save iserko/9083306 to your computer and use it in GitHub Desktop.
A simple script that gets your current public IP and then updates the no-ip service
This file contains 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
#!/usr/bin/env python | |
import argparse | |
import os.path | |
import requests | |
import sys | |
import ConfigParser | |
from datetime import datetime | |
IP_CACHE_FILE = "ipaddress.cache" | |
USER_AGENT = "no-ip Python Update Client/v1.0 [email protected]" | |
def print_out(msg): | |
dt = datetime.now().strftime("%Y-%m-%dT%H:%M:%S") | |
print dt, msg | |
def get_new_ip(): | |
headers = {'User-Agent': USER_AGENT} | |
req = requests.get('http://jsonip.com') | |
if int(req.status_code) != 200: | |
raise Exception("Couldn't get new IP from jsonip. " | |
"Code=%s. Text=%s" % (req.status_code, req.text)) | |
response = req.json() | |
return response["ip"] | |
def get_old_ip(): | |
if os.path.exists(IP_CACHE_FILE): | |
with open(IP_CACHE_FILE, 'r') as fname: | |
return fname.read().strip() | |
return "" | |
def set_new_ip(new_ip): | |
with open(IP_CACHE_FILE, 'w') as fname: | |
fname.write(new_ip) | |
def update_ip(new_ip, config): | |
headers = {'User-Agent': USER_AGENT} | |
params = {'hostname': config['hostname'], | |
'myip': new_ip} | |
url = 'https://dynupdate.no-ip.com/nic/update' | |
req = requests.get(url, | |
auth=(config['username'], config['password']), | |
params=params, | |
headers=headers) | |
if int(req.status_code) != 200: | |
raise Exception("no-ip returned an error. Status code=%s. " | |
"Text=%s" % (req.status_code, req.text)) | |
def get_configuration(conf_file): | |
config = ConfigParser.ConfigParser() | |
config.read(conf_file) | |
username = config.get('no-ip', 'username') | |
password = config.get('no-ip', 'password') | |
hostname = config.get('no-ip', 'hostname') | |
return {'username': username, | |
'password': password, | |
'hostname': hostname} | |
def run(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-c', '--config', | |
default='no-ip.config', | |
help='no-ip config file') | |
parser.add_argument('-v', '--verbose', | |
action='store_true') | |
options = parser.parse_args() | |
if not os.path.exists(options.config): | |
parser.error('Config file %s does not exist' % options.config) | |
config = get_configuration(options.config) | |
old_ip = get_old_ip() | |
if options.verbose: | |
print_out("Got old IP: %s" % old_ip) | |
new_ip = get_new_ip() | |
if options.verbose: | |
print_out("Got new IP: %s" % new_ip) | |
if new_ip != old_ip: | |
if options.verbose: | |
print_out("Public IP changed from %s to %s" % (old_ip, new_ip)) | |
update_ip(new_ip, config) | |
set_new_ip(new_ip) | |
if __name__ == '__main__': | |
sys.exit(run()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment