diff --git a/README.md b/README.md index 6bdead7..cf46108 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,11 @@ AMPR Portal API Python bindings =============================== +Install Dependencies +-------------------- + + sudo pip install paramiko requests + Usage ----- diff --git a/amprapi.py b/amprapi.py index af005c0..028d5b8 100755 --- a/amprapi.py +++ b/amprapi.py @@ -49,7 +49,7 @@ class AMPRAPI: >>> import amprapi >>> ampr = amprapi.AMPRAPI() >>> for entry in ampr.encap: - ... print "%(network)s/%(netmask)s via %(gatewayIP)s" % entry + ... print "%(network)s/%(maskLength)s via %(gatewayIP)s" % entry ... 44.151.22.22/32 via 2.10.28.74 44.182.69.0/24 via 5.15.186.251 @@ -60,7 +60,7 @@ class AMPRAPI: 'encap': EncapEntry, } _api_version = 'v1' - _api_version_minor = "1.04" + _api_version_minor = "1.07" def __init__(self, url=settings.API_URL, user=settings.API_USER, api_key=settings.API_KEY): @@ -84,9 +84,9 @@ def get(self, endpoint): r = requests.get(self.url + self._api_version + '/' + endpoint, auth=(self.user, self.api_key)) if r.status_code == 200: - return json.loads(r.json()) + return json.loads(r.text) elif r.status_code == 404: - raise NotImplementedError(r.json()) + raise NotImplementedError(r.text) else: raise Exception(r.text) @@ -97,4 +97,4 @@ def __getattr__(self, name): if __name__ == "__main__": ampr = AMPRAPI() for entry in ampr.encap: - print "%(network)s/%(netmask)s via %(gatewayIP)s" % entry + print "%(network)s/%(maskLength)s via %(gatewayIP)s" % entry diff --git a/updateros.py b/updateros.py index c555a24..2308cc0 100755 --- a/updateros.py +++ b/updateros.py @@ -27,10 +27,14 @@ edge_router_ip = sys.argv[-1] ssh_port = 22 username = None +distance = 30 # blacklist BGP-announced networks with direct-routing agreements -hamwan_dstaddresses = ["44.24.240.0/20", "44.103.0.0/19", "44.34.128.0/21"] -hamwan_gateways = ["198.178.136.80", "209.189.196.68"] +bgp_networks = [ + "44.24.240.0/20", # HamWAN Puget Sound + "44.34.128.0/21", # HamWAN Memphis + "44.103.0.0/19", # W8CMN Mi6WAN +] def get_encap(): @@ -38,6 +42,12 @@ def get_encap(): return ampr.encap +def filter_encap(route): + if route.network() in bgp_networks: + return False + return route + + def parse_ros_route(line): dstaddress, gateway = None, None for field in line.split(" "): @@ -91,8 +101,13 @@ def export_ros_ipip_interfaces(ssh): export_ros(ssh, "/interface ipip export"))) +def filter_ipip_ampr(ipips): + return filter( + lambda (interface, gateway): interface.startswith('ampr-'), ipips) + + def main(): - encap = get_encap() + encap = filter(None, map(filter_encap, get_encap())) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) @@ -104,7 +119,7 @@ def main(): unchanged = 0 routes_to_add = set(encap) routes_to_remove = set(ros_routes) - ipips_to_remove = set(ros_ipips) + ipips_to_remove = set(filter_ipip_ampr(ros_ipips)) for entry in encap: dstaddress = entry.network() gateway = entry['gatewayIP'] @@ -131,18 +146,18 @@ def main(): if ipips_to_remove: commands.append("# removing orphaned ipip interfaces") for interface, gateway in ipips_to_remove: - commands.append("/interface ipip remove [find name=%s]" % interface) + commands.append("/interface ipip remove %s" % interface) if routes_to_add: commands.append("# adding new and modified routes") for entry in routes_to_add: interface = "ampr-%s" % entry['gatewayIP'] - commands.append("/interface ipip add !keepalive clamp-tcp-mss=yes " + commands.append("/interface ipip add !keepalive clamp-tcp-mss=yes allow-fast-path=yes " "local-address=%s name=%s remote-address=%s comment=\"%s\"" % ( edge_router_ip, interface, entry['gatewayIP'], "AMPR last updated %s, added %s" % ( entry['updated'].date(), date.today()))) - commands.append("/ip route add dst-address=%s gateway=%s distance=30" % (entry.network(), interface)) + commands.append("/ip route add dst-address=%s gateway=%s distance=%s" % (entry.network(), interface, distance)) commands.append("/ip neighbor discovery set %s discover=no" % (interface)) if "-v" in sys.argv: @@ -151,6 +166,8 @@ def main(): for command in commands: ssh.exec_command(command) time.sleep(0.1) + except KeyboardInterrupt: + print "KeyboardInterrupt received, closing..." except UserWarning, e: print e except socket.timeout: