Delete static dhcp client information

I have added some info like below
uci add dhcp host && uci set dhcp.@host[-1].mac='{{mac_address}}' && uci set dhcp.@host[-1].ipaddrs='{{ip_address}}' && uci set dhcp.@host[-1].name='{{host_name}}' && uci commit dhcp && /etc/init.d/dnsmasq restart

After that output of ubus call luci-rpc getHostHints
{
"00:0A:F5:5C:00:64": {
"ipaddrs": [

	],
	"ip6addrs": [
		
	],
	"name": "Redmi5Plus-Redmi"
},
"04:B9:E3:5E:8C:9A": {
	"ipaddrs": [
		
	],
	"ip6addrs": [
		
	],
	"name": "Samsung"
},

Now i cant update the name of an mac address. How can i replace the name of the mac address ?

Take a look down at the bottom of /etc/config/dhcp and you'll see those host definitions. Just edit them in-place and do /etc/init.d/dnsmasq restart, or edit them in LuCI under Network -> DHCP and DNS on the Static Leases tab.

Thanks, and about LUCI network -> DHCP -> DNS. It keeps breaking every time i visit that page. At first it was working but stopped working after i did

uci add dhcp host && uci set dhcp.@host[-1].mac='{{mac_address}}' && uci set dhcp.@host[-1].ipaddrs='{{ip_address}}' && uci set dhcp.@host[-1].name='{{host_name}}' && uci commit dhcp && /etc/init.d/dnsmasq restart

After i all the config hosts in /etc/config/dhcp LUCI network -> DHCP -> DNS started to work. There were lot of duplicates and huge host config in /etc/config/dhcp

Using add and [-1] will just keep adding new hosts, and I assume you only want a small number of host entries that you keep managing? You can find the index of a desire host by doing something like this, then modifying the appropriate value specifically on that entry.

$ uci show dhcp | grep name
dhcp.@host[0].name='red_ws'
dhcp.@host[1].name='rtr001'
dhcp.@host[2].name='rtr002'
dhcp.@host[3].name='swi002'
dhcp.@host[4].name='swi003'

# Change IP of rtr002
$ name="rtr002"
$ index=$(uci show dhcp | awk -F'[\]\[]' -v name="$name" '$0 ~ name {print $2}')
$ uci set dhcp.@host[$index].ip='10.2.2.11'
$ uci commit
$ /etc/init.d/dnsmasq restart

Oh, and it might be breaking LuCI because of your use of ipaddrs, which is not a valid field in the host section. See https://openwrt.org/docs/guide-user/base-system/dhcp#static_leases

Ah I see I have putted mistakenly the ip.addr I don't use it. It breaks when /etc/config/dhcp has many entries like 20 and then the /network/dhcp/DNS tab stats stutter but loads after a while

Thanks was looking for this commands.

You could tune that up a bit, too, and keep the add functionality if the host is not already in the config.

index=$(...)

if [ -z "$index" ]; then
    # Host not present, so add a new one and set index accordingly.
    index="-1"
    uci add dhcp host
    uci set dhcp.@host[$index].name="$name"
fi

uci set ... using $index ...

Okay so im pulling the hostnames from mikrotik (gateway) what im doing is.

*/10 * * * * leases=$(curl -k -u admin:'pass' http://172.16.0.1/rest/ip/dhcp-server/lease | jq -r '.[] | "\(.["mac-address"]),\(.["host-name"])"' | grep -v null) && for lease in $leases; do mac=$(echo "$lease" | cut -d',' -f1); hostname=$(echo "$lease" | cut -d',' -f2); uci add dhcp host; uci set dhcp.@host[-1].mac="$mac"; uci set dhcp.@host[-1].name="$hostname"; uci commit dhcp; done && awk '!seen[$0]++ || /config host/' /etc/config/dhcp | tee -a /etc/config/dhcp.tmp && rm /etc/config/dhcp && mv /etc/config/dhcp.tmp /etc/config/dhcp && service dnsmasq restart

i know its ugly but kinda got it done. but in my /etc/config/dhcp i what im doing to remove duplicate hostnames is awk '!seen[$0]++ || /config host/' /etc/config/dhcp | tee -a /etc/config/dhcp.tmp && rm /etc/config/dhcp && mv /etc/config/dhcp.tmp /etc/config/dhcp which removes the duplicate hostnames but leaves the "config host" so the end result in dhcp is

that repeated config host lines is causing the stutter in netwok/dhcp & dns tab

maybe ill modify it more to check if the hostname of the mac address which im pulling already exists in the openwrt

I think you're better off sticking with uci commands to manage the config files, trying to do awks and seds and such on them usually is more trouble than it's worth.

Check out the uci -X show format. You can do things like this, where uci delete removes the whole config host section and all of it's values:

sections=$(uci -X show dhcp | awk -F'[\.=]' '/=host/ {print $2}')
for section_id in $sections; do
    section_name=$(uci get "dhcp.$section_id.name")
    if [ "$section_name" = "$name_of_section_to_delete" ]; then
        echo "Deleting section $section_id:"
        uci show dhcp.$section_id

        # Uncomment if you really want to delete it...
        # uci delete dhcp.$section_id
    fi
# or
    mac=$(uci get "dhcp.$section.mac")
    # and so on.
done

Is this code deleting the duplicate hosts?

I think you'll have to edit /etc/config/dhcp manually remove the extra config host lines for now, but once you've got the file cleaned up, you can subsequently use uci delete dhcp.$section_id to remove whole sections properly.

uci delete dhcp.$section_id

doesnt delete the deleted host config from the /etc/config/dhcp

as you can see i deleted the duplicates by that command but still it shows in the config file and if i print the uci show dhcp.$section_id the deleted hosts doesnt show but in /etc/config/dhcp there shows I even restarted dnsmasq and odhcpd didnt deleted from /etc/config/dhcp

Did you run a uci commit after doing the deletes? They only get staged by the uci delete and the file remains unchanged until you commit the changes.

1 Like

Thanks. I am new to openwrt you were very helpful to me i really appreciate. The final script to clean duplicates in /etc/config/dhcp i ended up with is.

#!/bin/bash

# Initialize an associative array to count occurrences of section names
declare -A sectionNameCount

# Get all section IDs
sections=$(uci -X show dhcp | awk -F'[\.=]' '/=host/ {print $2}')

for section_id in $sections; do
    section_name=$(uci get "dhcp.$section_id.name")

    # Increment the count for the current section name
    ((sectionNameCount[$section_name]++))
done

# Loop through the associative array to find and print duplicate section names
for name in "${!sectionNameCount[@]}"; do
    if [ ${sectionNameCount[$name]} -gt 1 ]; then
        echo "Duplicate section name: $name"

        # Flag to track if the first occurrence of a duplicate name has been encountered
        first_occurrence=true

        # Loop through all sections again to find and print the section IDs associated with the duplicate name
        for section_id in $sections; do
            section_name=$(uci get "dhcp.$section_id.name")
            if [ "$section_name" = "$name" ]; then
                # If it's the first occurrence of the duplicate name, set the flag to false and skip printing
                if $first_occurrence; then
                    first_occurrence=false
                else
                     echo "Section ID: $section_id"
                     uci del dhcp.$section_id
                     echo deleted $section_id
                fi
            fi
        done
    fi
done
uci commit dhcp

Lastly without your help I wouldnt be able to complete this. Thanks again keep helping to the community like a hero. <3

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.