Skip to content

synching #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
AMPR Portal API Python bindings
===============================

Install Dependencies
--------------------

sudo pip install paramiko requests

Usage
-----

Expand Down
10 changes: 5 additions & 5 deletions amprapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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)

Expand All @@ -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
31 changes: 24 additions & 7 deletions updateros.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,27 @@
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():
ampr = amprapi.AMPRAPI()
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(" "):
Expand Down Expand Up @@ -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())
Expand All @@ -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']
Expand All @@ -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:
Expand All @@ -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:
Expand Down