netnix.org
Networking and Unix


IP Subnet Lookup using TemplateFx

 November 15th, 2013Nov 15th, 2013      

Let’s think of a scenario where you have a list of IP subnets and within a TemplateFx template you want to find out which subnet a certain IP address is within. There are numerous IP calculators out there that will tell you the first and last IP address of a subnet, but few which allow you to find a matching subnet.

In this scenario we have some regional pop sites that are serving customers around the country. Each of these pop sites has a pool of IP addresses that are being used by customers. By creating a simple template using JavaScript we can provide a simple lookup functionality – this could be used within a template to provision different configuration depending on a customer’s pop location:

<?
  var db = {
    "10.205.0.0/18":"10.205.0.0/18 - UKPOP-BE-001",
    "10.205.64.0/18":"10.205.64.0/18 - UKPOP-WG-001",
    "10.205.128.0/18":"10.205.128.0/18 - UKPOP-OW-001",
    "10.205.192.0/18":"10.205.192.0/18 - UKPOP-NW-001",
    "10.206.0.0/17":"10.206.0.0/17 - UKPOP-GD-001",
    "10.206.128.0/17":"10.206.128.0/17 - UKPOP-GD-002"
  };
 
  function lookup (ip) {
    for (var k in db) {
      if (insubnet(k, ip)) {
        return db[k];
      }
    }
    return null;
  }
?>

<<IP>> is in <?= lookup("<<IP>>") ?>

We can then provide a list of IP addresses within our source data as follows:

IP
10.206.129.17
10.205.134.233
10.205.0.192

Once you click on “Generate Output” you will be presented with the following:

10.206.129.17 is in 10.206.128.0/17 - UKPOP-GD-002
10.205.134.233 is in 10.205.128.0/18 - UKPOP-OW-001
10.205.0.192 is in 10.205.0.0/18 - UKPOP-BE-001

This is just one of the many powerful features of TemplateFx – for more information please see the TemplateFx page.

General TemplateFx