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.
I just tried this script on a Linksys EA8500 running DD-WRT r43136 and failed miserably. It left me with only 1 out of 26 entries. Yes, I made a NVRAM backup before testing so all is well. Just wish it had worked.
EDIT: When running it from SSH instead of the Administrator/Commands, it did indeed work. Thanks! I guess it is a mystery why Administrator/Commands doesn't work, as it should in my mind work the same as a SSH session. Bug?
For me the script somehow didnt work at all, neither in the command line, nor via ssh When doing it via ssh, it puts them in a completely random order
Don't use the script in the OP, the ones posted later in this thread are better. Close the GUI while running the script via SSH/telnet and verify that it worked with this command which works both in terminal and Commands GUI.
nvram get static_leases | tr ' ' '\n'
Worst case reboot after fixing so the fixed version gets used by the GUI.