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
- sort
- 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.
Code:
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`"
Should this be able to be run through Administration|Commands, as doesn't seem to work for me. Tried it through a ssh session and worked a treat (been bugging me for ages that it's not in ip order)
A coworker fixed my script. Here's a better version...all on 1 line...also it properly sorts the IP addresses as numeric values.
Code:
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.
-----edit----
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.
OMG. Thanks so much. I keep my addresses separated between mobiles, infrastructure, etc and with all that I've added aver the years it is a mess. It is now sorted and easier to find things.
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?
Joined: 08 May 2018 Posts: 14217 Location: Texas, USA
Posted: Mon Jul 20, 2020 15:03 Post subject:
The script in the OP or the fixed script that works correctly? Folks need to be aware in the Administration->Commands entry box, there are certain syntax semantics involved. This is why it is only recommended to use it to add startup, firewall, custom scripts... properly. _________________ "Life is but a fleeting moment, a vapor that vanishes quickly; All is vanity"
Contribute To DD-WRT Pogo - A minimal level of ability is expected and needed... DD-WRT Releases 2023 (PolitePol)
DD-WRT Releases 2023 (RSS Everything)
----------------------
Linux User #377467 counter.li.org / linuxcounter.net
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.
Code:
nvram get static_leases | tr ' ' '\n'
Worst case reboot after fixing so the fixed version gets used by the GUI.