Script to convert static DHCP assignments from 1.12.0 to 1.13.0

General discussion about Gargoyle, OpenWrt or anything else even remotely related to the project

Moderator: Moderators

Post Reply
Mark
Posts: 1
Joined: Tue Apr 18, 2023 7:32 pm

Script to convert static DHCP assignments from 1.12.0 to 1.13.0

Post by Mark »

Hi all, happy Gargoyle user for many years.

I've just upgraded from 1.12 to 1.13, and wrote a little script to convert the static DHCP entries as they are now in a different format - thought I'd share it in case it's of use.

Here's what to do:

* Create a temp directory somewhere, doesn't have to be on the router, but does need BASH support.
* Create the below script in that temp directory.
* From the source router (1.12), copy /etc/hosts and /etc/ethers to the temp directory as the filenames "hosts" and "ethers"
* Open a terminal in the temp directory, chmod +x the script (name it what you like).
* On the target router (1.13), open an SSH shell.
* Make a backup of /etc/config/dhcp just in case.

Code: Select all

cd /etc/config
cp dhcp dhcp.backup
vi /etc/config/dhcp

(I use vi for editing, you may have something else)
* Go to the end of the file (SHIFT G). If there are any static hosts already defined on the 1.13 router, you will see an entry like:
config host 'static_host_3'
* You will need to modify the "count=1" line in the script line 25, to the next number. In my example, you would modify it to:

Code: Select all

count=4
* Run the script.
* It will create output.txt in the temp directory. Open this file in an editor and copy all the lines from 'static_host_1' to the highest static host number, to the clipboard.
* On the target 1.13 router, edit /etc/config/dhcp and paste those copied lines into the file, and save it.
* In the target 1.13 router web interface, refresh the DHCP page under Connection and you should see the static hosts listed under "Static IPs".
* If it's all gone downhill, restore your backup file on the target 1.13 router:

Code: Select all

cd /etc/config
mv dhcp dhcp.broken && mv dhcp.backup dhcp
Here's the script - note there's no error checking, but the above procedure has worked for me:

Code: Select all

#!/bin/bash

ethers_file="ethers"
hosts_file="hosts"
output_file="output.txt"

# Read MAC address and IP address from /etc/ethers
declare -A mac_ip_map
while read -r line; do
    mac=$(echo "$line" | awk '{print $1}')
    ip=$(echo "$line" | awk '{print $2}')
    mac_ip_map["$mac"]="$ip"
done < "$ethers_file"

# Read IP address and hostname from /etc/hosts
declare -A ip_name_map
while read -r line; do
    ip=$(echo "$line" | awk '{print $1}')
    name=$(echo "$line" | awk '{print $2}')
    ip_name_map["$ip"]="$name"
done < "$hosts_file"

# Generate output file with hostname, MAC, and IP address
echo -n "" > "$output_file"
count=1
for mac in "${!mac_ip_map[@]}"; do
    ip="${mac_ip_map[$mac]}"
    name="${ip_name_map[$ip]}"
    if [ -n "$name" ]; then
        output="config host 'static_host_$count'\n"
        output+="    option name '$name'\n"
        output+="    option mac '$mac'\n"
        output+="    option ip '$ip'\n\n"
        echo -e "$output" >> "$output_file"
        ((count++))
    fi
done


Lantis
Moderator
Posts: 6735
Joined: Mon Jan 05, 2015 5:33 am
Location: Australia

Re: Script to convert static DHCP assignments from 1.12.0 to 1.13.0

Post by Lantis »

Thanks mate very useful :)
http://lantisproject.com/downloads/gargoyle_ispyisail.php for the latest releases
Please be respectful when posting. I do this in my free time on a volunteer basis.

ispyisail
Moderator
Posts: 5180
Joined: Mon Apr 06, 2009 3:15 am
Location: New Zealand

Re: Script to convert static DHCP assignments from 1.12.0 to 1.13.0

Post by ispyisail »

I've just upgraded from 1.12 to 1.13
Will this work for 1.13 to 1.15?

Lantis
Moderator
Posts: 6735
Joined: Mon Jan 05, 2015 5:33 am
Location: Australia

Re: Script to convert static DHCP assignments from 1.12.0 to 1.13.0

Post by Lantis »

1.13 should already have the new format (double check) so it is not needed.
1.12 to 1.15, sure
http://lantisproject.com/downloads/gargoyle_ispyisail.php for the latest releases
Please be respectful when posting. I do this in my free time on a volunteer basis.

Post Reply