Created
June 10, 2022 00:09
-
-
Save jonathanduke/10f6c05223847fed322d37d713305cd2 to your computer and use it in GitHub Desktop.
C# code to read a binary DD-WRT backup file and export commands to set the values on the router
This file contains 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
// read a DD-WRT nvrambak_xyz.bin backup file and create "nvram set" commands FOR COMPARISON ONLY | |
// NOTE: some values may have issues, so you can't just copy and paste the whole output or it can mess up your router | |
// The format should be similar to processes to export from the router via the command line: | |
// https://gist.github.com/Aikhjarto/5b1c6b5e5d373d8d60c004e075b29acc | |
// https://gist.github.com/rambotech/d1e2486228c5e972c3c9c8936920f2fc | |
var set = new List<string>(); | |
using var file = System.IO.File.OpenRead(args[0]); | |
var buffer = new byte[0xFFFF]; | |
int len = 6; | |
int count = file.Read(buffer, 0, len); | |
if (count != len || System.Text.Encoding.ASCII.GetString(buffer, 0, count) != "DD-WRT") throw new InvalidDataException("unrecognized format"); | |
len = (file.ReadByte() | file.ReadByte() << 8); | |
while (-1 != (len = file.ReadByte())) | |
{ | |
count = file.Read(buffer, 0, len); | |
if (count != len) throw new InvalidDataException("unexpected end of name"); | |
var name = System.Text.Encoding.ASCII.GetString(buffer, 0, count); | |
len = (file.ReadByte() | file.ReadByte() << 8); | |
if (len > 0) | |
{ | |
count = file.Read(buffer, 0, len); | |
if (count != len) throw new InvalidDataException("unexpected end of value"); | |
var value = System.Text.Encoding.ASCII.GetString(buffer, 0, count); | |
set.Add($"nvram set {name}='{value.Replace("'", "\\'")}'"); | |
} | |
} | |
set.Sort(); | |
foreach (var cmd in set) Console.WriteLine(cmd); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Project file to use dotnet run: