What's new

Using a script to populate an ipset set with DHCP addresses

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Denna

Senior Member
I'm trying to create a script that adds DHCP addresses to an IP set.

1) Determine the DHCP address range with the following values.
Code:
nvram get dhcp_start = 192.168.1.2

nvram get dhcp_end = 192.168.1.4
2) Create a DHCP IP set called DHCP_CLI.
Code:
ipset create DHCP_CLI bitmap:ip range 192.168.1.0/24
3) Add all IP addresses with the range of dhcp_start and dhcp_end to the DHCP_CLI IP set.
Code:
ipset add DHCP_CLI <dhcp_start value>
...
ipset add DHCP_CLI <dhcp_end value>
In step 3, how would you iterate through the dhcp_start and dhcp_end values to add the following to the DHCP_CLI set ?

192.168.1.2
192.168.1.3
192.168.1.4​
 
Last edited:
Step 3:
Code:
#!/bin/sh
startIP=$(nvram get dhcp_start)
endIP=$(nvram get dhcp_end)
loopIndex=${startIP##*.}
while [ $loopIndex -le ${endIP##*.} ]; do
  echo ${startIP%.*}.$loopIndex
  loopIndex=$((loopIndex+1))
done
 
@redhat27,

That's amazing !

The DHCP range would probably be less than 10.

To add each IP address to an IP set, would you replace the echo line with ...
Code:
ipset add DHCP_CLI ${startIP%.*}.$loopIndex
 

Similar threads

Latest threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top