Skip to content

Instantly share code, notes, and snippets.

@Aikhjarto
Last active May 17, 2024 21:31
Show Gist options
  • Save Aikhjarto/5b1c6b5e5d373d8d60c004e075b29acc to your computer and use it in GitHub Desktop.
Save Aikhjarto/5b1c6b5e5d373d8d60c004e075b29acc to your computer and use it in GitHub Desktop.
DD-WRT nvram tricks
This file provides some nifty command-line tools around DD-WRT's nvram command.
https://www.dd-wrt.com/wiki/index.php/Hardware#NVRAM
USE WITH CAUTION: fiddling with nvram can wipe your settings and you may loose access to your router.
Hint 1: Different version of DD-WRT might use different nvram variables. Thus, be carefull when updating.
Hint 2: The commands listed below should be issued via a ssh-connection since the webinterface for issuing commands might time out for the longer commands.
Background
==========
Extracting the content of configuration variables with nvram would provide an easy way of selective backup/restore of settings. However, listing the content of the nvram via
nvram show
produce a mess with multi-line parameters like certificates. The output of "nvram" show cannot be used directly to set values with
nvram set variable="value"
List of Variables
=================
Get a list of all variables that have a non-empty value
nvram show | grep = | grep -v -e =$ | awk -F'=' '{print $1}' | sort -f | while read VAR; do [ -n "$(nvram get ${VAR})" ] && echo ${VAR}; done
The more you know 1: The two grep calls search for lines containing a "=" (indicates a variable) but not at the end (would be an empty variable)
The more you know 2: awk is used to get the string on the left side of "=" on every line.
The more you know 3: If a variable has a "=" in it's value, a bogus variable name might occur in the list.
Convertion to "nvram set" syntax
The more you know 4: For better perception, the list of variables can be sorted alphanumerically.
================================
Get a list of all variables that have a value and format the list to be able to processed with 'nvram set':
nvram show | grep = | grep -v -e =$ | awk -F'=' '{print $1}' | sort -f | (IFS=''; while read VAR; do VAL=$(nvram get ${VAR}); [ -n "$VAL" ] && printf 'nvram set %s='\''%s'\''\n' ${VAR} ${VAL/\'/\'\\\'\'}; done)
The more you know 1: Some values may contain linebreaks and spaces. Thus, the "IFS" settings.
The more you know 2: If a variable has an "=" in its value, a bogus variable/value pair is detected. These are recognized by an empty output of "nvram get $VAR". This dection is done within the square brackets.
The more you know 3: Single quotes are more robust when it comes to character escaping, e.g., the $ sign and escape character \ might occur in password hashes and needs do be escaped when using in double quotes but not in single quotes. Using single quotes for "nvram set" (in contrast to double quotes as suggested by DD-WRT) it is only required to escape the single quotes.
@rambotech
Copy link

Very useful. Thanks for this cheat sheet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment