Skip to content

Instantly share code, notes, and snippets.

@robinkunde
Last active July 12, 2018 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinkunde/691384f6b77a93c4b03454519771d0d5 to your computer and use it in GitHub Desktop.
Save robinkunde/691384f6b77a93c4b03454519771d0d5 to your computer and use it in GitHub Desktop.
Use this script to generate `configure` commands for static DHCP IP address assignment on EdgeRouter X
#!/usr/bin/env python3
import argparse
import os
import csv
usage = '''Use this script to generate configure commands.
Expected input csv format: identifier,identifier_suffix(optional),ignored,mac-address-with-dashes,ip4-address
First line is ignored.
Any line that is missing required fields is ignored.
Assumes 192.168.1.0/24 subnet.
Steps:
1. Send output file to Edgerouter using scp
2. SSH to Edgerouter
3. Switch to root if needed
4. $ configure
5. $ source output-file
6. $ exit
'''
parser = argparse.ArgumentParser(description=usage, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('path')
args = parser.parse_args()
print("edit service dhcp-server shared-network-name LAN subnet 192.168.1.0/24")
print("delete static-mapping")
with open(args.path, 'r') as csvfile:
reader = csv.reader(csvfile)
next(reader, None) # skip the headers
for row in reader:
row = [c.strip() for c in row]
if not row[0]: continue
if not row[2]: continue
if not row[4]: continue
identifier = f"{row[0].lower()}"
if row[1]:
identifier += f"_{row[1].lower().replace(' ', '_')}"
print(f"set static-mapping {identifier} mac-address {row[2].upper()}")
print(f"set static-mapping {identifier} ip-address {row[4]}")
print("commit")
print("save")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment