Skip to main content
Question

NQE for OneIP table

  • August 21, 2024
  • 1 reply
  • 89 views
  • Translate

How would one replicate the OneIP table with NQE, including info such as IP address, MAC address, vendor, switch name and switch port?

1 reply

Andreas
Employee
  • Employee
  • 23 replies
  • August 21, 2024

Hi @alineamorim ,

Great question. We will add a query for this to Forward Library in an upcoming release. In the meantime, here is a query you can use to provide this information. 

 

/**
 * @intent Finds IP addresses and subnets on interfaces and hosts.
 * @description Similar to IP Inventory query, but is limited to interfaces and 
 * subnets, and also takes no parameters.
 * 
 * Includes the following columns:
 * Subnet: The subnet found
 * Address Type: One of IPv4 or IPv6
 * Source Type: One of Interface, Host.
 * MAC: MAC address associated with the IP, if any.
 * Device: The device on which the subnet is found.
 */

import "@fwd/L3/IpAddressUtils";
import "@fwd/L3/Interface Utilities";

isIPv4(a) = isPresent(patternMatch(toString(a), `{ipv4Subnet}`));

addressType(a) = if isIPv4(a) then "IPv4" else "IPv6";

getIpSubnets(addresses) =
  foreach addr in addresses
  select ipSubnet(addr.ip, addr.prefixLength);

getInterfaceIps(device) =
  foreach l3Iface in getL3Interfaces(device)
  let ipv4Records = getIpv4Ips(device, l3Iface)
  let ipv6Records = getIpv6Ips(device, l3Iface)
  foreach record in ipv4Records + ipv6Records
  select record;

getIpv4Ips(device, l3Iface) =
  foreach subnet in getIpSubnets(l3Iface.ipv4.addresses)
  select {
    Subnet: subnet,
    "Address Type": "IPv4",
    "Source Type": "Interface",
    Source: l3Iface.name,
    MAC: null : MacAddress,
    Device: device.name,
    Interfaces: [l3Iface.name],
    Vlans: [toString(l3Iface.vlan)],
    Vendor: device.platform.vendor
  };

getIpv6Ips(device, l3Iface) =
  foreach subnet in getIpSubnets(l3Iface.ipv6.addresses)
  select {
    Subnet: subnet,
    "Address Type": "IPv6",
    "Source Type": "Interface",
    Source: l3Iface.name,
    MAC: null : MacAddress,
    Device: device.name,
    Interfaces: [l3Iface.name],
    Vlans: [toString(l3Iface.vlan)],
    Vendor: device.platform.vendor
  };

formatVlanRange(range) =
  join("-", distinct([toString(range.from), toString(range.to)]));

getHosts(device) =
  foreach host in device.hosts
  foreach subnet in host.addresses
  select {
    Subnet: subnet,
    "Address Type": addressType(subnet),
    "Source Type": "Host",
    Source: host.name,
    MAC: host.macAddress,
    Device: device.name,
    Interfaces: host.interfaces,
    Vlans: (foreach range in host.vlans
            select formatVlanRange(range)),
    Vendor: device.platform.vendor
  };

foreach device in network.devices
foreach ipRecord in getInterfaceIps(device) + getHosts(device)
select ipRecord

 

Translate

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings