Posted: Tue Jan 05, 2016 1:57 Post subject: Script to sort static leases
I keep modifying my list of DHCP static leases, and I prefer to have them sorted by IP address. Since the UI doesn't allow for sorting, I wrote a script. Please be aware, I am NOT a Linux developer...so be kind on the comments!
The idea is:
- get the list of leases
- using awk, reorganize them as 1 entry per line with IP address first
- using awk, recreate the "nvram set static_leases" command entry
- last 'awk' command is to remove the trailing " "
I am willing to bet someone that know awk better than I do can make this much shorter.
A coworker fixed my script. Here's a better version...all on 1 line...also it properly sorts the IP addresses as numeric values.
nvram set static_leases="`nvram get static_leases | sed 's/\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)/.\1./g' | tr ' ' '\n' | sort -n -k2,2n -k3,3n -k4,4n -k5,5n -t. | tr '\n' ' ' | sed 's/\.\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)\./\1/g' | sed 's/^ //'; echo`"
thanks for this. Cleaned up my dnsmasq static lease list.
can someone create a similar script to this to reorder port forwards by port and hostname? Thanks! _________________ pfSense i7-4510U + 2x Intel 82574 + 2x Intel i350 Mini-ITX Build
940/880 mbit Fiber Internet from FiOS
Dell PowerConnect 2716 Gigabit Switch
Netgear R8000 AP, running DD-WRT
Asus RT-66U AP, running DD-WRT
I have been frustrated with this "problem" for years, to the point of spending inordinate amounts of time manually sorting and saving after each new entry, adding dummy entries to give me a place to move devices around.
Now I can easily figure out what a new device needs to be statically assigned in my IP scheme.
It wasn't the end of the world, but my 25 static devices and growing is a bit more organized.