{"id":1416,"date":"2023-05-03T23:33:28","date_gmt":"2023-05-03T21:33:28","guid":{"rendered":"https:\/\/cln.io\/blog\/?p=1416"},"modified":"2023-05-03T23:33:29","modified_gmt":"2023-05-03T21:33:29","slug":"edgerouter-vyatta-static-ip-mapping-converter-to-openwrt-luci","status":"publish","type":"post","link":"https:\/\/cln.io\/blog\/edgerouter-vyatta-static-ip-mapping-converter-to-openwrt-luci\/","title":{"rendered":"Edgerouter \/ Vyatta Static IP Mapping Converter to OpenWRT \/ LuCI"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Converting DHCP Reservations from an EdgeRouter to OpenWrt<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On this page<\/p>\n\n\n\n<nav aria-label=\"Table of Contents\" class=\"wp-block-table-of-contents\"><ol><li><a class=\"wp-block-table-of-contents__entry\" href=\"https:\/\/cln.io\/blog\/edgerouter-vyatta-static-ip-mapping-converter-to-openwrt-luci\/#my-edge-router-died-needed-translation-of-ip-reservations\">My edge router died, needed translation of IP reservations<\/a><\/li><li><a class=\"wp-block-table-of-contents__entry\" href=\"https:\/\/cln.io\/blog\/edgerouter-vyatta-static-ip-mapping-converter-to-openwrt-luci\/#demo-live-html\">Demo\/live HTML<\/a><\/li><li><a class=\"wp-block-table-of-contents__entry\" href=\"https:\/\/cln.io\/blog\/edgerouter-vyatta-static-ip-mapping-converter-to-openwrt-luci\/#source-code\">Source code<\/a><\/li><\/ol><\/nav>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"my-edge-router-died-needed-translation-of-ip-reservations\">My edge router died, needed translation of IP reservations<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cln.io\/blog\/wp-content\/uploads\/2023\/05\/image-848x1024.png\" alt=\"\" class=\"wp-image-1419\" width=\"432\" height=\"522\" srcset=\"https:\/\/cln.io\/blog\/wp-content\/uploads\/2023\/05\/image-848x1024.png 848w, https:\/\/cln.io\/blog\/wp-content\/uploads\/2023\/05\/image-248x300.png 248w, https:\/\/cln.io\/blog\/wp-content\/uploads\/2023\/05\/image-768x927.png 768w, https:\/\/cln.io\/blog\/wp-content\/uploads\/2023\/05\/image.png 1012w\" sizes=\"auto, (max-width: 432px) 100vw, 432px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">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 &#8220;static-mapping&#8221; keyword followed by a unique identifier, the IP address, and the MAC address. Here&#8217;s what the regular expression looks like:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/static-mapping\\s+(\\S+)\\s+\\{[\\s\\S]*?ip-address\\s+([\\d.]+)[\\s\\S]*?mac-address\\s+([:\\da-fA-F]+)[\\s\\S]*?\\}\/gm<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first capture group captures the unique identifier (which we&#8217;ll use as the &#8220;name&#8221; option in OpenWrt), the second capture group captures the IP address, and the third capture group captures the MAC address.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once the information is parsed out, the code formats it into OpenWrt format, with &#8220;config host&#8221; as the first line, followed by the &#8220;option&#8221; lines for MAC address, IP address, and name (using the unique identifier we captured earlier). Here&#8217;s what the output looks like:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"routeros\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">config host '00a0deb11f25'\n    option mac '00:a0:de:b1:1f:25'\n    option ip '192.168.1.41'\n    option name 'RX-V777'<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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&#8217;s a console text box to display any errors or output from the code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Overall, this small piece of code was a lifesaver for me when switching routers. It&#8217;s a great example of how regex and some basic JavaScript can be used to quickly solve a real-world problem. If you&#8217;re in a similar situation, give it a try!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"demo-live-html\">Demo\/live HTML<\/h2>\n\n\n\n<!DOCTYPE html>\n<html>\n<head>\n    <title>Edgerouter\/vyatta Static IP Mapping Converter to OpenWRT\/LuCI<\/title>\n     <style>\n        body {\n            font-family: Arial, sans-serif;\n            margin: 0;\n            padding: 0;\n            background-color: #f2f2f2;\n        }\n\n        h1 {\n            background-color: #333;\n            color: #fff;\n            margin: 0;\n            padding: 10px;\n        }\n\n        .container {\n            margin: 20px;\n        }\n\n        .input-label {\n            display: block;\n            font-size: 18px;\n            margin-bottom: 10px;\n        }\n\n        textarea {\n            font-size: 16px;\n            padding: 10px;\n            border-radius: 5px;\n            border: none;\n            background-color: #fff;\n            box-shadow: 0px 0px 5px rgba(0,0,0,0.1);\n            resize: none;\n        }\n\n        button {\n            font-size: 18px;\n            padding: 10px 20px;\n            border-radius: 5px;\n            border: none;\n            background-color: #333;\n            color: #fff;\n            cursor: pointer;\n        }\n\n        button:hover {\n            background-color: #555;\n        }\n\n        .output-label {\n            display: block;\n            font-size: 18px;\n            margin-top: 20px;\n            margin-bottom: 10px;\n        }\n\n        #output {\n            font-family: monospace;\n        }\n\n        .console-label {\n            display: block;\n            font-size: 18px;\n            margin-top: 20px;\n            margin-bottom: 10px;\n        }\n    <\/style>\n<\/head>\n<body>\n    <h1>Edgerouter\/vyatta Static IP Mapping Converter to OpenWRT\/LuCI<\/h1>\n    <div class=\"container\">\n        <label class=\"input-label\" for=\"input\">Paste your config.boot file or static-mapping entries below:<\/label>\n        <textarea id=\"input\" rows=\"10\" cols=\"80\"><\/textarea><br><br>\n        <button onclick=\"convert()\">Convert<\/button><br><br>\n        <label class=\"output-label\" for=\"output\">Converted output:<\/label>\n        <textarea id=\"output\" rows=\"10\" cols=\"80\"><\/textarea><br><br>\n        <label class=\"console-label\" for=\"console\">Console:<\/label>\n        <textarea id=\"console\" rows=\"10\" cols=\"80\"><\/textarea><br><br>\n    <\/div>\n    <script>\n        function convert() {\n            const input = document.getElementById(\"input\").value;\n            const regex = \/static-mapping\\s+(\\S+)\\s+\\{[\\s\\S]*?ip-address\\s+([\\d.]+)[\\s\\S]*?mac-address\\s+([:\\da-fA-F]+)[\\s\\S]*?\\}\/gm;\n            let output = \"\";\n\n            let match;\n            while ((match = regex.exec(input)) !== null) {\n                output += \"config host '\" + match[3].toLowerCase().replace(\/:\/g, '') + \"'\\n\";\n                output += \"\\toption mac '\" + match[3] + \"'\\n\";\n                output += \"\\toption ip '\" + match[2] + \"'\\n\";\n                output += \"\\toption name '\" + match[1] + \"'\\n\";\n                output += \"\\n\";\n            }\n\n            document.getElementById(\"output\").value = output;\n            console.log(output);\n            document.getElementById(\"console\").value += output + \"\\n\";\n        }\n    <\/script>\n<\/body>\n<\/html>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"source-code\">Source code<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!DOCTYPE html>\n&lt;html>\n&lt;head>\n    &lt;title>Edgerouter\/vyatta Static IP Mapping Converter to OpenWRT\/LuCI&lt;\/title>\n     &lt;style>\n        body {\n            font-family: Arial, sans-serif;\n            margin: 0;\n            padding: 0;\n            background-color: #f2f2f2;\n        }\n\n        h1 {\n            background-color: #333;\n            color: #fff;\n            margin: 0;\n            padding: 10px;\n        }\n\n        .container {\n            margin: 20px;\n        }\n\n        .input-label {\n            display: block;\n            font-size: 18px;\n            margin-bottom: 10px;\n        }\n\n        textarea {\n            font-size: 16px;\n            padding: 10px;\n            border-radius: 5px;\n            border: none;\n            background-color: #fff;\n            box-shadow: 0px 0px 5px rgba(0,0,0,0.1);\n            resize: none;\n        }\n\n        button {\n            font-size: 18px;\n            padding: 10px 20px;\n            border-radius: 5px;\n            border: none;\n            background-color: #333;\n            color: #fff;\n            cursor: pointer;\n        }\n\n        button:hover {\n            background-color: #555;\n        }\n\n        .output-label {\n            display: block;\n            font-size: 18px;\n            margin-top: 20px;\n            margin-bottom: 10px;\n        }\n\n        #output {\n            font-family: monospace;\n        }\n\n        .console-label {\n            display: block;\n            font-size: 18px;\n            margin-top: 20px;\n            margin-bottom: 10px;\n        }\n    &lt;\/style>\n&lt;\/head>\n&lt;body>\n    &lt;h1>Edgerouter\/vyatta Static IP Mapping Converter to OpenWRT\/LuCI&lt;\/h1>\n    &lt;div class=\"container\">\n        &lt;label class=\"input-label\" for=\"input\">Paste your config.boot file or static-mapping entries below:&lt;\/label>\n        &lt;textarea id=\"input\" rows=\"10\" cols=\"80\">&lt;\/textarea>&lt;br>&lt;br>\n        &lt;button onclick=\"convert()\">Convert&lt;\/button>&lt;br>&lt;br>\n        &lt;label class=\"output-label\" for=\"output\">Converted output:&lt;\/label>\n        &lt;textarea id=\"output\" rows=\"10\" cols=\"80\">&lt;\/textarea>&lt;br>&lt;br>\n        &lt;label class=\"console-label\" for=\"console\">Console:&lt;\/label>\n        &lt;textarea id=\"console\" rows=\"10\" cols=\"80\">&lt;\/textarea>&lt;br>&lt;br>\n    &lt;\/div>\n    &lt;script>\n        function convert() {\n            const input = document.getElementById(\"input\").value;\n            const regex = \/static-mapping\\s+(\\S+)\\s+\\{[\\s\\S]*?ip-address\\s+([\\d.]+)[\\s\\S]*?mac-address\\s+([:\\da-fA-F]+)[\\s\\S]*?\\}\/gm;\n            let output = \"\";\n\n            let match;\n            while ((match = regex.exec(input)) !== null) {\n                output += \"config host '\" + match[3].toLowerCase().replace(\/:\/g, '') + \"'\\n\";\n                output += \"\\toption mac '\" + match[3] + \"'\\n\";\n                output += \"\\toption ip '\" + match[2] + \"'\\n\";\n                output += \"\\toption name '\" + match[1] + \"'\\n\";\n                output += \"\\n\";\n            }\n\n            document.getElementById(\"output\").value = output;\n            console.log(output);\n            document.getElementById(\"console\").value += output + \"\\n\";\n        }\n    &lt;\/script>\n&lt;\/body>\n&lt;\/html>\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1419,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,37],"tags":[],"class_list":["post-1416","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-it","category-networking"],"_links":{"self":[{"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/posts\/1416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/comments?post=1416"}],"version-history":[{"count":5,"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/posts\/1416\/revisions"}],"predecessor-version":[{"id":1422,"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/posts\/1416\/revisions\/1422"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/media\/1419"}],"wp:attachment":[{"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/media?parent=1416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/categories?post=1416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/tags?post=1416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}