Script to sort static leases

Post new topic   Reply to topic    DD-WRT Forum Forum Index -> Contributions Upload
Author Message
smeisner
DD-WRT Novice


Joined: 13 Aug 2012
Posts: 4

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: 4

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: 4

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: 131

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: 3

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)
Display posts from previous:    Page 1 of 1
Post new topic   Reply to topic    DD-WRT Forum 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