rnio DD-WRT User
Joined: 21 Apr 2012 Posts: 94
|
Posted: Sat Apr 01, 2017 4:02 Post subject: Backup / Restore of NVRAM settings using EXCEL ... 100% copy |
|
For a long time I have been using the OLD B/R script initially developed by FRATER (http://www.dd-wrt.com/phpBB2/viewtopic.php?t=44324), however it stopped working with the latest version of DD-WRT.
I used EXCEL to do the processing of the NVRAM dump, which I created on the router.
I used a SAMBA connection to access the file from my windows machine.
I used PUTTY / PLINK to automate this process as much as possible. PLINK is used to execute a shell command on the router from my Windows machine.
All this does is create a 100% identical copy of the NVRAM state at the time of backup. The resulting "SetAll.sh" script can be used to restore the router to that state whenever it is run.
The same can be done with this ONE-LINER ... however I wanted to process the variables further ... and this was just the start.
Code: | nvram show | grep = | cut -d"=" -f 1 | while read key; do echo nvram set $key=\"$(nvram get $key)\"; done' > SetAll.sh |
I am not necessarily a UNIX guy ... and Excel lets me do a lot of things a little more comfortable.
IN EXCEL:
Please cut and paste the code below as a MACRO into the VBA-IDE.
VERSION: 2.0
Code: |
' ##############################################################################################
' NVRAM Backup for DD-WRT
'
' Robert Nio (c) :)
'
' ##############################################################################################
'
' This will SAVE all settings on the DD-WRT router and create a "SetAll.sh" command to RESTORE these in case you need to
'
'
' WHAT YOU NEED:
'
' EXCEL obviously :)
' current copy of PUTTY: http://www.putty.org/
' USB Stick @ the router
'
'
' GET STARTED:
'
' Have SAMBA access to the USB stick @ the router ... on WINDOWS mine is mapped as a "Z"-drive
'
' CONFIRM / ADJUST the settings in this VBA module to YOUR environment
'
' Execute the NVRAM Macro (see below)
'
'
'
' ##############################################################################################
' CUSTOMIZATIONS:
'
' Please ADJUST / MODIFY these seeetings to reflect your environment
'
' ##############################################################################################
' DIRECTORY LOCATION for temp files (on your WINDOWS system)
' e.g. "C:\temp\"
Public Const TEMPDIR = "C:\temp\"
' DIRECTORY LOCATION for PUTTY installation (on your WINDOWS system)
' e.g. "C:\Program Files\PuTTY"
Public Const PUTTYDIR = "C:\Program Files\PuTTY\"
' The ROOT (mount) of your USB stick on the router
' e.g. "/mnt/sda1" or "/opt"
Public Const USBRoot = "/opt"
' The location of the USB-ROOT. (where you mounted the SAMBA drive to)
' e.g. "Z:\"
Public Const WINMAP = "Z:\"
Public Const RouterIP = "192.168.1.1"
Public Const AdminPW = "secret"
' ##############################################################################################
' Some DECLARATIONS needed for supervised command executions (remote SHELL-SCRIPTS on router)
' ##############################################################################################
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Private Declare PtrSafe Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare PtrSafe Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As Long
Private Declare PtrSafe Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long
Private Declare PtrSafe Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function CloseWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Public Sub ExecuteShellCommand(commando As String)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ReturnValue As Integer
' Initialize the STARTUPINFO structure:
start.cb = Len(start)
' EXECUTE remote DD-WRT command:
'
' FIRST write the command to a TEXT file on the Windows Machine
Komando = TEMPDIR & "C.TXT"
Open Komando For Output As #1
Print #1, "cd " & USBRoot
Print #1, commando
Close #1
' NOW configure the PLINK.EXE command to execute the newly created TXT file on the router
'
software = PUTTYDIR & "plink.exe"
myFile = Quote((software))
Komando = Quote((Komando))
switches = " -ssh -pw " & AdminPW & " -m " & Komando & " root@" & RouterIP
cmdline = myFile & switches
' Execute PLINK
'
' Start the shelled application:
ReturnValue = CreateProcessA(0&, cmdline, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
' Wait for the shelled application to finish:
Do
ReturnValue = WaitForSingleObject(proc.hProcess, 0)
DoEvents
Loop Until ReturnValue <> 258
ReturnValue = CloseHandle(proc.hProcess)
End Sub
' ##############################################################################################
' This is the MAIN macro
' ##############################################################################################
Sub nvram()
'
' nvram Macro
' Robert Nio (c) :)
'
' This will SAVE all settings on the DD-WRT router and create a "SetAll.sh" command to RESTORE these in case you need to
'
'
' WHAT YOU NEED:
'
' EXCEL obviously :)
' current copy of PUTTY: http://www.putty.org/
' USB Stick @ the router
'
'
' GET STARTED:
'
' Have SAMBA access to the USB stick @ the router ... on WINDOWS mine is mapped as a "Z"-drive
'
' CONFIRM / ADJUST the settings in this VBA module to YOUR environment
'
' Execute THIS Macro
'
' EXECUTE remote DD-WRT command:
'
' nvram show | grep = | cut -d"=" -f 1 | while read key; do echo nvram set $key=\"$(nvram get $key)\"; done' > nvramall.txt
'
Befehl = "nvram show | grep = | cut -d" & Chr(34) & "=" & Chr(34) & " -f 1 | while read key; do echo nvram set $key=\" & Chr(34) & "$(nvram get $key)\" & Chr(34) & "; done > ./nvramall.txt"
ExecuteShellCommand (Befehl)
Workbooks.OpenText Filename:=WINMAP & "nvramall.txt", Origin:=437, StartRow:=1 _
, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
, Space:=False, Other:=False, FieldInfo:=Array(1, 1), _
TrailingMinusNumbers:=True
wb = ActiveWorkbook.Name
SH = ActiveSheet.Name
Range("A1").End(xlDown).Select
reihe = ActiveCell.Row()
Workbooks(wb).Sheets(SH).Select
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "Set Variables"
SH2 = "Set Variables"
Sheets(SH2).Range("A1").Value = "#!/bin/sh"
Sheets(SH).Range("D1").Formula = "=MID(INDIRECT(" & Chr(34) & "A" & Chr(34) & "&MATCH(" & Chr(34) & "*os_version*" & Chr(34) & ",A1:A" & reihe & ",0)),LEN(INDIRECT(" & Chr(34) & "A" & Chr(34) & "&MATCH(" & Chr(34) & "*os_version*" & Chr(34) & ",A1:A" & reihe & ",0)))-6,6)"
Sheets(SH).Range("E1").Formula = "=TEXT(NOW()," & Chr(34) & "mm-dd-yy" & Chr(34) & ")"
Sheets(SH2).Range("A2").Value = "# " & Sheets(SH).Range("D1").Value & " DATE: " & Sheets(SH).Range("E1").Value
Sheets(SH2).Range("A3").Value = "echo " & Chr(34) & "Write variables" & Chr(34)
Sheets(SH2).Range("A4").Value = ""
Sheets(SH).Select
Range("A1:A" & reihe).Select
Selection.Copy
Sheets(SH2).Select
Range("A5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A5").End(xlDown).Select
reihe = ActiveCell.Row()
Sheets(SH2).Range("A" & reihe + 2).Value = "# Commit variables"
Sheets(SH2).Range("A" & reihe + 3).Value = "#"
Sheets(SH2).Range("A" & reihe + 4).Value = "echo " & Chr(34) & "Save variables to nvram" & Chr(34)
Sheets(SH2).Range("A" & reihe + 5).Value = "nvram commit"
Sheets(SH2).Range("A" & reihe + 6).Value = "reboot"
Sheets(SH2).Range("A" & reihe + 7).Value = "ENDE"
Application.CutCopyMode = False
Application.DisplayAlerts = False
FN = Sheets(SH).Range("D1").Value & " " & Sheets(SH).Range("E1").Value & " SetAll"
ActiveWorkbook.SaveAs Filename:=WINMAP & FN, FileFormat:=xlTextPrinter _
, CreateBackup:=False
ActiveWorkbook.Close
Application.DisplayAlerts = True
' EXECUTE remote DD-WRT command:
'
' sed -i -e '/ENDE/,$d' '31575M 04-06-17 SetAll.prn' && sed `echo "s/\r//"` './31575M 04-06-17 SetAll.prn' > './31575M 04-06-17 SetAll.sh' && chmod 775 './31575M 04-06-17 SetAll.sh' && rm './31575M 04-06-17 SetAll.prn' && rm ./nvramall.txt
'
Befehl = "sed -i -e '/ENDE/,$d' " & Chr(39) & FN & ".prn" & Chr(39) & _
"&& sed `echo " & Chr(34) & "s/\r//" & Chr(34) & "` " & Chr(39) & "./" & FN & ".prn" & Chr(39) & " > " & Chr(39) & "./" & FN & ".sh" & Chr(39) & _
"&& chmod 775 " & Chr(39) & "./" & FN & ".sh" & Chr(39) & _
"&& rm " & Chr(39) & "./" & FN & ".prn" & Chr(39) & _
"&& rm ./nvramall.txt"
ExecuteShellCommand (Befehl)
End Sub
|
USAGE:
Have SAMBA access to the USB stick @ the router ... on WINDOWS mine is mapped as a "Z"-drive
NOW EXECUTE THE EXCEL-MACRO ON YOUR WINDOWS MACHINE.
Once the file is processed you will end up with a file "SetAll.sh"
Any feedback / comments welcome.
I hope this can be of use to some of you.
Robert
PS: BIG thanks to KONG and BS for all the good work.
Last edited by rnio on Fri Apr 07, 2017 14:16; edited 4 times in total |
|