Pages

Monday, November 11, 2013

More OpenWRT Modifying

So this time I decided I didn't like the way the DNS and DHCP tab displayed my static leases and the current leases..

The end result:


I did this in two parts... part number 1 was to modify the lease status page thats included a few different places so that the leases are sorted by ip address the same way I did the other page.

Inside /usr/lib/lua/luci/view/admin_network/lease_status.htm at the top:


        function ipaddr(tmp) {
                var a = tmp.split(".");
                for (var i = 0; i < a.length; i++) {
                        while (a[i].length < 3) {
                                a[i] = "0" + a[i];
                        }
                }
                return a[0] + a[1] + a[2] + a[3];
        }
        function displayIP(tmp) {
                var a = tmp.split(".");

                for (var i = 0; i < a.length; i++) {
                        while (a[i].length < 3) {
                                a[i] = "0" + a[i];
                        }
                }
                return a[0] + "." + a[1] + "." + a[2] + "." + a[3];
        }
above the line
        XHR.poll(5, '<%=luci.dispatcher.build_url
("admin", "network", "dhcplease_status")%>', null,


And then I added
            st[0].sort(
                   function (a, b) {
                     return (ipaddr(a.ipaddr) < ipaddr(b.ipaddr) ? -1 : 
                       ipaddr(a.ipaddr) > ipaddr(b.ipaddr) ? 1: 0);
                   }
                 );     

above the lines


        for( var i = 0; i < st[0].length; i++ )
             {                 
                       var timestr;

                       if (st[0][i].expires <= 0)
                         {



Then I made the following line:
           tr.insertCell(-1).innerHTML = st[0][i].ipaddr;

Look like this:
           tr.insertCell(-1).innerHTML = displayIP(st[0][i].ipaddr);


This accomplishes the first part... the second part I used a php script to sort my /etc/config/dhcp by ip address, so that it's listed in form. (Yes, I could have modified the LUA script that load the dhcp leases from the file, but then when it's written back, it'd be reread from the file. Seemed to me like a whole lot of work to rearrange that file when that's all I really cared about.

sort.php
--

 $lines = split("config", join(file("/etc/config/dhcp")));

 foreach ($lines as $value) {
  if ($value != "") {
   if (substr($value, 1, 4) != "host") {
    $output = $output . "config" . $value;
   }
  }
 }

 foreach ($lines as $value) {
  if ($value != "\n") {
   if (substr($value, 1, 4) == "host") {
    list($junk, $good) = split("option ip '", $value);
    list($good, $junk) = split("'", $good);

    $values[$good] = $value;
   } 
  }
 }


 function cmp($a, $b) {
  return ip2long($a) - ip2long($b);
 }

 uksort($values, "cmp");

 foreach ($values as $value) {
  $output = $output . "config" . $value;
 }

 unlink("/etc/config/dhcp1");

 $fp = fopen("/etc/config/dhcp1", 'w');

 fwrite($fp, $output);

 fclose($fp);

Yes, I know it's stupid, but I like knowing what ip addresses are free, and it's easier for me to see that if my static leases are in order!

Hope this helps someone else' ocd.

Friday, September 27, 2013

OpenWRT DHCP Listing

I have openwrt on my router and my dhcp listing is constantly displayed in some erratic order. As far as I can tell it's not sorted in any fashion, it's just kinda tossed in there (maybe clients that refresh their lease don't move in the file)

It looks like this...


Kinda makes me irritated because there's no web controls to sort the listing and I like to see my clients by IP address (OCD or Autism, who knows).

So, I wrote this...

Ahhhh... Much better!

#!/usr/bin/php-cli
<?php
    foreach (file("/tmp/dhcp.leases") as $line) {
        $fields = explode(" ", $line);
        $data[] = Array($fields[0], $fields[1], $fields[2], $fields[3]);
    }

    function cmp($a, $b) {
        $a_long = sprintf('%u', ip2long($a[2]));
        $b_long = sprintf('%u', ip2long($b[2]));

        return ($a_long > $b_long);
    }

    usort($data, 'cmp');

    foreach ($data as $var=>$value) {
        foreach ($value as $subvar=>$subvalue) {
            echo "\t\t" . $subvalue;
        }
        echo "\n";
    }
?>

EDIT:

I've done this inside the openwrt LUCI by modifying "/usr/lib/lua/luci/view/admin_status/index.htm"

I added
function ipaddr(tmp) {
 var a = tmp.split(".");

 for (var i = 0; i < a.length; i++) {
  while (a[i].length < 3) {
   a[i] = "0" + a[i];
  }
 }

 return a[0] + a[1] + a[2] + a[3];
}

function displayIP(tmp) {
 var a = tmp.split(".");

 for (var i = 0; i < a.length; i++) {
  while (a[i].length < 3) {
   a[i] = "0" + a[i];
  }
 }
 return a[0] + "." + a[1] + "." + a[2] + "." + a[3];
}
above the lines
<% if has_dhcp then %>
var ls = document.getElementById('lease_status_table');
if (ls)
and then I added
info.leases.sort(
function(a, b) {
return (ipaddr(a.ipaddr) < ipaddr(b.ipaddr) ? -1 : 
      (ipaddr(a.ipaddr) > ipaddr(b.ipaddr) ? 1: 0));
}
);
above the lines
for( var i = 0; i < info.leases.length; i++ )
{
 var timestr;

 if (info.leases[i].expires <= 0)
  timestr = '<%:expired%>';
 else

And TAADAA!