Skip to content

Instantly share code, notes, and snippets.

@katchy3132
Last active February 28, 2019 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katchy3132/f0e807335a2c0e0a640b720eaff70fc8 to your computer and use it in GitHub Desktop.
Save katchy3132/f0e807335a2c0e0a640b720eaff70fc8 to your computer and use it in GitHub Desktop.
Compare 2 nvram dumps for Kille72's Tomato-ARM
#!/opt/bin/python3
"""
compare 2 nvram hash dumps for FreshTomato-ARM
http://freshtomato.org/
before upgrading
nvram show > nvram.old
upgrade to new build
nvram show > nvram.new
then
./compare-nvram.py nvram.old nvram.new
OR
nvram show > nvram.new && ./compare-nvram.py nvram.old nvram.new
"""
import sys
import re
import subprocess
file1_NAME = sys.argv[1]
file2_NAME = sys.argv[2]
d1 = {}
d2 = {}
def process_file(filename, dict):
linenum = 0
lastkey = None
with open(filename) as f:
for line in f:
line = line.strip()
linenum = linenum + 1
match = re.search('^([a-zA-Z0-9./_]+)=(.+)', line)
if match:
(key, val) = match.group(1), match.group(2)
dict[str(key)] = val
lastkey = str(key)
# print( str(linenum) + ": " + str(key) + " " + val + "--" + lastkey)
# some SSH keys might have = in them
elif line.endswith("=") and len(line) < 40:
line = line.rstrip("=")
dict[line] = None
lastkey = str(line)
# print(str(linenum) + ": " + line +": EMPTY")
elif len(line) < 1:
# print(str(linenum) + ": BLANKLINE")
dict[lastkey] = str(dict.get(lastkey)) + "\n" + line
else:
dict[lastkey] = str(dict.get(lastkey)) + "\n" + line
# print(str(linenum) + line + ": PREVIOUS " + str(lastkey))
return
process_file(file1_NAME, d1)
process_file(file2_NAME, d2)
f1keysSet = set(d1.keys())
f2keysSet = set(d2.keys())
extraKeys1 = f1keysSet - f2keysSet
print("Keys only in f1: {}".format(extraKeys1))
extraKeys2 = f2keysSet - f1keysSet
print("\n Keys only in f2: {}\n".format(extraKeys2))
ignoredKeys = {"sshd_dsskey", "sshd_ecdsakey", "sshd_hostkey", "adblock_blacklist",
"tinc_hosts", "tinc_private_rsa", "tinc_private_ed25519",
"wan_ipaddr", "os_date", "http_id", "ddnsx0_cache", "ddnsx1_cache", "sch_c1_last", "wl0.1_hwaddr",
"wan_ppp_redialpid", "wan_pppd_pid"}
commonKeys = f1keysSet & f2keysSet
for k in sorted(commonKeys):
if k in ignoredKeys:
print("IGNORING {} ".format(k))
next
elif d1[k] == d2[k]:
# print (" same {}".format(k) )
next
elif k in f2keysSet:
print("DIFF var-> f1::f2 {} -> {} :: {}".format(k, d1[k], d2[k]))
else:
print("missing key {}".format(k))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment