Site Tools


static2json

This is an old revision of the document!


Export DHCP Reservations from an NVRAM cfg file into a JSON file for the web interface

This Bash script allows you to extract DHCP Reservations from an older backup .cfg file and output them to a JSON file. The JSON file file can then be imported via FreshTomato's web interface. This can be useful when the router/cfg file from which you want to get extract the reservations didn't support reservations backup feature in the web interface.


The script must be executed from within a router's command-line interface or an 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




static2json.1779128423.txt.gz · Last modified: by hogwild