This is usefull for many users, but be aware: Before clearing names, I would propose one should first check what will be cleared! Namely this command searches for the lines ending in =. I just run a check on two of my routers, and runing this command would damage:
sshd_hostkey=AAA...ZE=
sshd_dsskey=AAA...w==
which do indeed contain data, but the data (the keys) themselves end in = (and this command would write them back without the final = char). Not sure that would be a good idea
So, before this little cleanup, I would propose to first check what will be removed (or damaged) and be prepared to fix errors (scroll back and if variable name is not ending in = but in some other text, that must not be touched).
Keep in mind that on other routers there may be different items ending in = which should not be deleted / damaged. Probably a good idea to first copy such items and restore them afterwards manually.
In cases similar to mine, where I have a problem with two quite long keys, I would propose a simpler solution: limit the length of the string so change applies only to shorter strings than the given number (40 - in my case, 35 would also work but 30 would not be enough). But, this may not be an universal solution for everyone and I do not propose anyone blindly doing this. First check what will be removed or modified!
First, I would do a test to find the needed length:
Code:
for line in `nvram show | grep =$ `; do var=${line%*=}; [ ${#line} -lt 40 ] || echo $var; done
Whatever this command spits out, will not be affected by the modified cleanup with the next command (use the number you found with the previous command here too, instead of 40):
Code:
for line in `nvram show | grep =$ `; do var=${line%*=}; [ ${#line} -lt 40 ] && nvram unset $var; done
If now you run
you should see only the (in my case two) items which were not to be impacted.
On one of my routers, before and after numbers are:
size: 55068 bytes (10468 left)
size: 45773 bytes (19763 left)
Do not forget nvram commit if all was done well. I would not append this blindly and automatically to the above command. Let's ignore improper quoting style, it should work like this well.
As I was curious how long these keys of mine were, I may also add it here: it may be interesting to check the sizes of the longest NVRAM variables, this fills my screen nicely:
Code:
nvram show | awk -F= '{printf "%4s %s\n", length(), $1}' | sort -nr | head -n22