Script to sort static leases

Post new topic   Reply to topic    DD-WRT Forum Index -> Contributions Upload
Goto page 1, 2  Next
Author Message
smeisner
DD-WRT Novice


Joined: 13 Aug 2012
Posts: 5

PostPosted: Tue Jan 05, 2016 1:57    Post subject: Script to sort static leases Reply with quote
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! Laughing

Code:

#!/bin/sh
data=`nvram get static_leases | awk 'BEGIN { FS="="; RS=" " } {a=1; while ( a<NF ) { b=a+1; c=a+2; d=a+3; print $c "=" $b "=" $a "=" $d; a=+4; } }' | sort | awk 'BEGIN { FS="="; RS="\n" } { a=1; while ( a<NF ) { b=a+1; c=a+2; d=a+3; printf $c "=" $b "=" $a "=" $d " "; a+=4 } }' | awk '{sub(/[ \t]+$/, "")};1'`
nvram set static_leases="${data}"
nvram commit


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.

Steve
Sponsor
smeisner
DD-WRT Novice


Joined: 13 Aug 2012
Posts: 5

PostPosted: Wed Jan 06, 2016 18:38    Post subject: Reply with quote
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`"
JaffaBoy
DD-WRT Novice


Joined: 21 May 2015
Posts: 3

PostPosted: Sun Feb 07, 2016 13:22    Post subject: Reply with quote
smeisner wrote:
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)
smeisner
DD-WRT Novice


Joined: 13 Aug 2012
Posts: 5

PostPosted: Sun Feb 07, 2016 16:07    Post subject: Reply with quote
Yeah...I only ever tested the script via an ssh terminal session.
Peppeddu
DD-WRT Novice


Joined: 07 Jul 2011
Posts: 5

PostPosted: Mon Apr 04, 2016 3:07    Post subject: Reply with quote
smeisner wrote:
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`"


Here's another version I use to sort the Static Leases by Hostname, and it works also when copied into the Startup Command.
Code:
nvram set static_leases="`nvram get static_leases | sed 's/ *$//g' | tr ' ' '\n' | sort -k2 -t= | tr '\n' ' '; echo`"
Paint
DD-WRT User


Joined: 22 Jun 2015
Posts: 135

PostPosted: Mon Apr 11, 2016 3:50    Post subject: Reply with quote
smeisner wrote:
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
djk44883
DD-WRT Novice


Joined: 04 Jan 2009
Posts: 10

PostPosted: Mon Jul 25, 2016 18:29    Post subject: Reply with quote
smeisner wrote:
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`"


This is what everyone with issues needs! I feel so much better with a sorted list.
Greatly appreciated
DJ
scoutice
DD-WRT Novice


Joined: 09 Jun 2017
Posts: 1

PostPosted: Fri Jun 09, 2017 10:29    Post subject: Reply with quote
so simple and so helpful! this makes me sleep better. it was so painful to add a line in between.
neerav
DD-WRT Novice


Joined: 08 Jul 2010
Posts: 44

PostPosted: Sat Jul 08, 2017 18:45    Post subject: Reply with quote
THANK YOU. Thank you.

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.

Did I mention "thank you"?!?

_________________
LinkSys E1000 ...... DD-WRT
LinkSys E2000 ...... DD-WRT
Netgear R8000 ...... Finally DD-WRT
TP-Link WR710N ..... Stock (considering DD-WRT)
johnnyNobody999
DD-WRT User


Joined: 10 Jan 2014
Posts: 499

PostPosted: Mon Sep 24, 2018 15:53    Post subject: Reply with quote
Is the <nvram commit> still necessary? It sure would be nice if the developers would add a sorting routine to the firmware.
keithg
DD-WRT User


Joined: 07 Jan 2011
Posts: 53

PostPosted: Sun May 12, 2019 20:20    Post subject: Reply with quote
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.
lexridge
DD-WRT Guru


Joined: 07 Jun 2006
Posts: 928
Location: WV, USA

PostPosted: Thu May 14, 2020 23:55    Post subject: Reply with quote
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?
MostlySilentAlabatross
DD-WRT Novice


Joined: 18 Feb 2019
Posts: 6

PostPosted: Mon Jul 20, 2020 14:10    Post subject: Reply with quote
For me the script somehow didnt work at all, neither in the command line, nor via ssh Sad When doing it via ssh, it puts them in a completely random order

kernel-panic69
DD-WRT Guru


Joined: 08 May 2018
Posts: 14101
Location: Texas, USA

PostPosted: Mon Jul 20, 2020 15:03    Post subject: Reply with quote
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
yoyoma2
DD-WRT User


Joined: 24 Sep 2016
Posts: 371

PostPosted: Tue Jul 28, 2020 15:03    Post subject: Reply with quote
MostlySilentAlabatross wrote:
For me the script somehow didnt work at all, neither in the command line, nor via ssh Sad 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.
Goto page 1, 2  Next Display posts from previous:    Page 1 of 2
Post new topic   Reply to topic    DD-WRT Forum Index -> Contributions Upload All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum