Edgerouter / Vyatta Static IP Mapping Converter to OpenWRT / LuCI
Converting DHCP Reservations from an EdgeRouter to OpenWrt
On this page
My edge router died, needed translation of IP reservations
Recently, my EdgeRouter 4 stopped working and I had to switch to a spare GL.iNet router running OpenWrt. However, I had a lot of DHCP reservations set up on the EdgeRouter that I needed to port over to the new router. Fortunately, I was able to use a small piece of code to quickly convert the EdgeRouter DHCP reservations to OpenWrt format.
The code works by taking in a config.boot file or a series of static-mapping entries and parsing out the necessary information using a regular expression. The regular expression looks for the “static-mapping” keyword followed by a unique identifier, the IP address, and the MAC address. Here’s what the regular expression looks like:
/static-mapping\s+(\S+)\s+\{[\s\S]*?ip-address\s+([\d.]+)[\s\S]*?mac-address\s+([:\da-fA-F]+)[\s\S]*?\}/gm
The first capture group captures the unique identifier (which we’ll use as the “name” option in OpenWrt), the second capture group captures the IP address, and the third capture group captures the MAC address.
Once the information is parsed out, the code formats it into OpenWrt format, with “config host” as the first line, followed by the “option” lines for MAC address, IP address, and name (using the unique identifier we captured earlier). Here’s what the output looks like:
config host '00a0deb11f25' option mac '00:a0:de:b1:1f:25' option ip '192.168.1.41' option name 'RX-V777'
The code also includes some basic CSS to make the web page look a little nicer. The input and output text boxes are labelled and styled, and there’s a console text box to display any errors or output from the code.
Overall, this small piece of code was a lifesaver for me when switching routers. It’s a great example of how regex and some basic JavaScript can be used to quickly solve a real-world problem. If you’re in a similar situation, give it a try!
Demo/live HTML
Edgerouter/vyatta Static IP Mapping Converter to OpenWRT/LuCI
Source code
<!DOCTYPE html> <html> <head> <title>Edgerouter/vyatta Static IP Mapping Converter to OpenWRT/LuCI</title> <style> body { font-family: Arial, sans-serif; margin: 0; padding: 0; background-color: #f2f2f2; } h1 { background-color: #333; color: #fff; margin: 0; padding: 10px; } .container { margin: 20px; } .input-label { display: block; font-size: 18px; margin-bottom: 10px; } textarea { font-size: 16px; padding: 10px; border-radius: 5px; border: none; background-color: #fff; box-shadow: 0px 0px 5px rgba(0,0,0,0.1); resize: none; } button { font-size: 18px; padding: 10px 20px; border-radius: 5px; border: none; background-color: #333; color: #fff; cursor: pointer; } button:hover { background-color: #555; } .output-label { display: block; font-size: 18px; margin-top: 20px; margin-bottom: 10px; } #output { font-family: monospace; } .console-label { display: block; font-size: 18px; margin-top: 20px; margin-bottom: 10px; } </style> </head> <body> <h1>Edgerouter/vyatta Static IP Mapping Converter to OpenWRT/LuCI</h1> <div class="container"> <label class="input-label" for="input">Paste your config.boot file or static-mapping entries below:</label> <textarea id="input" rows="10" cols="80"></textarea><br><br> <button onclick="convert()">Convert</button><br><br> <label class="output-label" for="output">Converted output:</label> <textarea id="output" rows="10" cols="80"></textarea><br><br> <label class="console-label" for="console">Console:</label> <textarea id="console" rows="10" cols="80"></textarea><br><br> </div> <script> function convert() { const input = document.getElementById("input").value; const regex = /static-mapping\s+(\S+)\s+\{[\s\S]*?ip-address\s+([\d.]+)[\s\S]*?mac-address\s+([:\da-fA-F]+)[\s\S]*?\}/gm; let output = ""; let match; while ((match = regex.exec(input)) !== null) { output += "config host '" + match[3].toLowerCase().replace(/:/g, '') + "'\n"; output += "\toption mac '" + match[3] + "'\n"; output += "\toption ip '" + match[2] + "'\n"; output += "\toption name '" + match[1] + "'\n"; output += "\n"; } document.getElementById("output").value = output; console.log(output); document.getElementById("console").value += output + "\n"; } </script> </body> </html>