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.