Advertisement
rs232

nvram_backup

Apr 27th, 2023 (edited)
1,234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.46 KB | None | 0 0
  1. #!/bin/sh
  2. ver="nvram_backup v0.95" #- rs232 - 02/24
  3. b="\033[0;40m"
  4. w="\033[1;37m"
  5. alias helpout='echo -e "┌───────────────────────────────────────────────────────────────────────────────┐
  6. │                        ${w}FreshTomato $ver${b}                         │
  7. └───────────────────────────────────────────────────────────────────────────────┘
  8.                 ┌─────── optional ───┐   ┌─────── at least one is needed ──────┐
  9.  ${w}nvram_backup${b} + │ ${w}usbrestore │ erase${b} │ + │ ${w}sec ipv4 ipv6 vlan mac time dhcp dns${b}│
  10.                 └────────────────────┘   │ ${w}wl wl_hw mcast port qos usb adblock${b} │
  11.                                          │ ${w}tor vpn web tomatoanon restriction ${b} │
  12.                                          │ ${w}portal misc admin       --or--> all${b} │
  13.                                          └─────────────────────────────────────┘
  14. ┌─ Usage ───────────────────────────────────────────────────────────────────────┐
  15. │• usbrestore creates the restore.autorun file on the root of your USB volume   │
  16. │• restore.autorun runs atomatically at the next reboot (once only)             │
  17. │• If usbrestore is not defined the script nvram_backup prints out to stout     │
  18. │• The erase option adds a "nvram erase" at the top of the restore script         │
  19. │• to redirect the config to a file just use nvram_backup \$par1 \$par2 ... >file │
  20. │• mac is needed only in certain cases like WDS, wl_hw same device restore only │
  21. └───────────────────────────────────────────────────────────────────────────────┘
  22. " && exit'
  23. rar() {
  24.     r=''
  25.     for i in $1; do
  26.     [ "$i" != "$2" ] && r="$r $i"
  27.     done
  28.     echo "$r"
  29. }
  30. [ $# -eq 0 ] && helpout || {
  31. par=$@
  32. [ "$(echo "$par" | tr ' ' '\n' | grep -v 'usbrestore\|erase' | wc -l)" -eq 0 ] && helpout
  33. echo "$par" | grep -q "usbrestore" && { mount | grep -q /mnt && {
  34.         loc="$(mount | grep /mnt | awk '{print $3}')"
  35.         loc="${loc}/restore.autorun"
  36.         [ -s "${loc}" ] && rm -f "${loc}" &>/dev/null
  37.         touch "${loc}"
  38.         chmod +x "${loc}"
  39.         nvram set rstrd=0
  40.         nvram commit
  41.         exec >"${loc}"
  42.         echo -e "#!/bin/sh"
  43.         echo -e "nvram get rstrd 2>/dev/null | grep -q 1 && exit"
  44.         } || echo -e "No USB device found. USB config autorestore is not possible"; }
  45. par=$(rar "$par" "usbrestore")
  46. echo "$par" | grep -q "erase" && echo -e "nvram erase"
  47. par=$(rar "$par" "erase")
  48. exclude="_hw"
  49. force="wan[0-3]?_hw"
  50. echo "$par" | grep -q "status-autorun" && {
  51.     USB=$(mount | grep /mnt | awk '{print $3}')
  52.     [ ! -z "$USB" ] && ls -1 "${USB}"/restore.autorun &>/dev/null && { echo -e "☢️ ${USB}/restore.autorun is present and will autorun at the next reboot<br>"
  53.     grep -q 'nvram erase' "${USB}"/restore.autorun &>/dev/null && echo -e "☢️ restore.autorun contains nvram erase"
  54.     echo -e "<br><input type=button value='🧹 delete ' onclick=removeAutorun();></input>" ; }
  55.     exit
  56. }
  57. echo "$par" | grep -q "removeautorun" && { mount | grep -q /mnt && {
  58.         loc=$(mount | grep /mnt | awk '{print $3}')
  59.         loc="${loc}/restore.autorun"
  60.         rm -f "${loc}" 2>/dev/null
  61.         }
  62.     exit
  63. }
  64. nvram show 1>/tmp/nvshow 2>/dev/null
  65. for ar in $par; do
  66. if [[ $ar == 'help' ]]; then helpout
  67. elif [[ $ar == "sec" ]];         then items="pass|crt|^ssh|https?"
  68. elif [[ $ar == "ipv4" ]];        then items="ipaddr|br|wan|domain|hostname|portforward|_pptp|ppp|^wan|^lan|^router"
  69. elif [[ $ar == "ipv6" ]];        then items="^ipv6"
  70. elif [[ $ar == "vlan" ]];        then items="vlan|^ctf|manual_boot_nv"
  71. elif [[ $ar == "mac" ]];         then items="^wan[0-9]?_mac|^[0-9]:macadd"
  72. elif [[ $ar == "time" ]];        then items="^ntp|^ntpd|^tm_"
  73. elif [[ $ar == "dhcp" ]];        then items="^dhcp|^dhcpd_"
  74. elif [[ $ar == "dns" ]];         then items="^dnsmasq_|^ddns|^dns_|^dnssec_|^mdns_"
  75. elif [[ $ar == "wl" ]];          then items="^wl|territory_code"
  76. elif [[ $ar == "wl_hw" ]];       then items="^[0-9]:"
  77. elif [[ $ar == "mcast" ]];       then items="^udpxy|^multicast|force_igmpv2|^emf_"
  78. elif [[ $ar == "port" ]];        then items="^trigforward|upnp|^dmz|^portforward"
  79. elif [[ $ar == "qos" ]];         then items="qos|limit|bwl_enable|DSCP_fix_enable|^ne_v"
  80. elif [[ $ar == "usb" ]];         then items="usb|smb|^bt|^ftp|_ftp|^ms_|gro_disable|txworkq|idle_enable"
  81. elif [[ $ar == "adblock" ]];     then items="^adblock"
  82. elif [[ $ar == "tor" ]];         then items="^tor_"
  83. elif [[ $ar == "vpn" ]];         then items="^vpn_|^pptp|tinc|^wg_"
  84. elif [[ $ar == "web" ]];         then items="^nginx_|^mysql_|^web_"
  85. elif [[ $ar == "tomatoanon" ]];  then items="^tomatoanon"
  86. elif [[ $ar == "restriction" ]]; then items="^rrule"
  87. elif [[ $ar == "portal" ]];      then items="^NC_|^web_"
  88. elif [[ $ar == "misc" ]];        then items="^webmon|^jumbo|boot_wait|^console_|^fw_|^ne_|^nf_|router_name|^stealth|^telnetd_|wait_time|watchdog"
  89. elif [[ $ar == "admin" ]];       then items="^script_|anport|ttb|^[r|^c]stats|^sesx|cifs|^jffs2|debug|^nfs_|^snmp_|^log|^sch_|^remote_"
  90. elif [[ $ar == "all" ]];         then items="pass|crt|^ssh|https?|ipaddr|br|wan|domain|hostname|portforward|_pptp|ppp|^wan|^lan|^ipv6|vlan|^ctf|manual_boot_nv|^wan[0-9]?_mac|^[0-9]:macadd|^ntp|^ntpd|^tm_|^dhcp|^dhcpd_|^dnsmasq_|^ddns|^dns_|^dnssec_|^mdns_|^wl|^[0-9]:|territory_code|^trigforward|upnp|^dmz|^portforward|^udpxy|^multicast|force_igmpv2|^emf_|qos|limit|bwl_enable|DSCP_fix_enable|^ne_v|usb|smb|^bt|^ftp|_ftp|^ms_|gro_disable|txworkq|idle_enable|^adblock|^tor_|^vpn_|^pptp|tinc|^wg_|^nginx_|^mysql_|^web_|^tomatoanon|^rrule|^webmon|^NC_|^web_|^jumbo|boot_wait|^console_|^fw_|^ne_|^nf_|router_name|^stealth|^telnetd_|wait_time|watchdog|^script_|anport|ttb|^[r|^c]stats|^sesx|cifs|^jffs2|debug|^nfs_|^snmp_|^log|^sch_|^remote_"
  91. else echo "One or more parameters have not been understood"; exit
  92. fi
  93. echo "###### $ar ######"
  94. grepstr=$( echo "$items" | sed -e 's/[\t ]//g;/^$/d' | sed ':a;N;$!ba;s/\n/\\\|/g' )
  95. echo "### grepstr=$grepstr"
  96. {
  97. for item in $(grep "^.*=." /tmp/nvshow | awk -F= "{print \$1}" | grep -E "${grepstr}" | grep -v "${exclude}" | sort -u); do
  98.     item_value="$(nvram get "${item}"| sed 's!\([\$\"\`]\)!\\\1!g'; echo nvgetwasnull)"
  99.     case $item_value in
  100.         nvgetwasnull) ;;
  101.         *) echo "nvram set ${item}=\"${item_value%
  102. nvgetwasnull}\"" ;;
  103.     esac
  104. done
  105. for item in $(grep "^.*=." /tmp/nvshow | awk -F= "{print \$1}" | grep -E "${force}" | sort -u); do
  106.     item_value="$(nvram get "${item}"| sed 's!\([\$\"\`]\)!\\\1!g'; echo nvgetwasnull)"
  107.     case $item_value in
  108.         nvgetwasnull) ;;
  109.         *) echo "nvram set ${item}=\"${item_value%
  110. nvgetwasnull}\"" ;;
  111.     esac
  112. done
  113. }
  114. done
  115. rm -f /tmp/nvshow &>/dev/null
  116. echo $@ | grep -q "usbrestore" && echo "mv \"${loc}\" \"${loc}.$(date +%Y-%h-%d_%H_%m_%S)\""
  117. echo "nvram commit
  118. sleep 1
  119. /sbin/reboot"
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement