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 = {
    "":" - UKPOP-BE-001",
    "":" - UKPOP-WG-001",
    "":" - UKPOP-OW-001",
    "":" - UKPOP-NW-001",
    "":" - UKPOP-GD-001",
    "":" - 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:


Once you click on “Generate Output” you will be presented with the following: is in - UKPOP-GD-002 is in - UKPOP-OW-001 is in - UKPOP-BE-001

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

General TemplateFx