Convert DHCP Reservation extracted from a NVRAM CFG file into a JSON file for the GUI

With this you can extract DHCP Reservation from an older backup from before the GUI backup feature was introduced and apply them to the GUI with the output JSON file.

This need to be executed from a router CLI or SSH session.

static2json.sh
#!/bin/sh
 
# Convert NVRAM DHCP Reservation to GUI JSON format
# For FreshTomato
# By HommeOursPorc - 2026-04-21
 
# Usage:
# static2json.sh <infile.cfg> <outfile.json>
 
# $1 is infile.cfg file
# $2 is outfile.json file
 
usage() {
    echo "Convert NVRAM DHCP Reservation to GUI JSON format"
    echo "Usage: static2json.sh <infile.cfg> <outfile.json>"
}
 
NV_CFG="$1"
NV_CONV="/tmp/2json_nv.tmp"
DHCPD_STATIC="/tmp/2json_static.tmp"
CSTATS="/tmp/2json_cstats.tmp"
JSON="$2"
 
[ $# -ne 2 ] && usage && exit 1
[ ! -s "$NV_CFG" -o ! -d "$(dirname "$JSON")" ] && usage && exit 2
 
nvram convert "$NV_CFG" "$NV_CONV" &>/dev/null
grep "dhcpd_static" "$NV_CONV" | awk -F'=' '{print $2}' | tr ">" "\n" > "$DHCPD_STATIC"
grep "cstats_include" "$NV_CONV" | awk -F'=' '{print $2}' > "$CSTATS"
 
exec > "$JSON"
 
echo '{
  "nextpage": "basic-static.asp",
  "grids": {
    "bs-grid": ['
 
c=0
for i in $(cat "$DHCPD_STATIC"); do
    MAC="$(echo "$i" | cut -d'<' -f1)"
    IP="$(echo "$i" | cut -d'<' -f2)"
    HOSTNAME="$(echo "$i" | cut -d'<' -f3)"
    ARP="$(echo "$i" | cut -d'<' -f4)"
    if [ $(grep -cw "$IP" "$CSTATS") -gt 0 ]; then
        IPTRAFFIC=1
    else
        IPTRAFFIC=0
    fi
 
    if [ $(echo "$MAC" | grep -c ',') -eq 1 ]; then
        MAC1="$(echo "$MAC" | cut -d',' -f1)"
        MAC2="$(echo "$MAC" | cut -d',' -f2)"
    else
        MAC1="$MAC"
        MAC2="00:00:00:00:00:00"
    fi
 
    [ $c -gt 0 ] && echo ","
    echo -n "      [
        \"$MAC1\",
        \"$MAC2\",
        \"$ARP\",
        \"$IP\",
        \"$IPTRAFFIC\",
        \"$HOSTNAME\"
      ]"
 
      c=$(($c+1))
done
 
echo -ne "\n    ]
  }
}"
 
rm -f "$NV_CONV" "$DHCPD_STATIC" "$CSTATS"
 
exit 0