Created
December 10, 2020 14:16
-
-
Save angely-dev/2c4edea1daa513d458837b058b907ad4 to your computer and use it in GitHub Desktop.
RIPE API bulk update of attributes (maintainer and contact)
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
import argparse | |
import requests | |
import urllib | |
# | |
# Usage | |
# | |
parser = argparse.ArgumentParser(description='RIPE bulk update of attributes') | |
parser.add_argument('--mntner', help='the current maintainer username', required=True) | |
parser.add_argument('--passwd', help='the current maintainer password', required=True) | |
parser.add_argument('--commit', help='to commit the RIPE bulk changes', action='store_true') | |
args = parser.parse_args() | |
# | |
# New contact and maintainer | |
# | |
NEW_CONTACT = '<CONTACT>-RIPE' | |
NEW_MNTNER = '<MNTNER>-MNT' | |
# | |
# Get all objects by maintainer | |
# | |
params = {'inverse-attribute': 'mnt-by', 'query-string': args.mntner} | |
params = urllib.parse.urlencode(params) | |
response = requests.get(f'https://rest.db.ripe.net/search.json?{params}') | |
objects = response.json()['objects']['object'] | |
# | |
# For summary purpose | |
# | |
obj_hrefs = [] | |
obj_types = [] | |
# | |
# For each object | |
# | |
for obj in objects: | |
# Ignore these objects for now | |
if obj['type'] in ['mntner', 'organisation', 'person', 'role']: | |
continue | |
# Look into object attributes | |
obj_has_changed = False | |
for attr in obj['attributes']['attribute']: | |
# Change maintainer | |
if attr['name'] in ['mnt-by', 'mnt-lower', 'mnt-routes', 'mnt-domains']: | |
attr['value'] = NEW_MNTNER | |
obj_has_changed = True | |
# Change contact | |
if attr['name'] in ['admin-c', 'tech-c', 'zone-c']: | |
attr['value'] = NEW_CONTACT | |
obj_has_changed = True | |
# Update object if it has changed | |
if obj_has_changed: | |
# For summary purpose | |
obj_hrefs.append(obj['link']['href']) | |
obj_types.append(obj['type']) | |
# On commit only | |
if args.commit: | |
obj_json = {'objects': {'object': [obj]}} | |
obj_href = obj['link']['href'] | |
password = {'password': args.passwd} | |
password = urllib.parse.urlencode(password) | |
response = requests.put(f'{obj_href}?{password}', json=obj_json) | |
print(response.status_code) # must be 200 | |
# | |
# Summary | |
# | |
obj_hrefs = '\n\t'.join(list(set(obj_hrefs))) | |
obj_types = '\n\t'.join(list(set(obj_types))) | |
print(f'Following objects will be updated:\n\t{obj_hrefs}') | |
print(f'Following object types are involved in the update:\n\t{obj_types}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feel free to adapt this gist to your needs (e.g., the first query or attributes to update).