Skip to main content

This query returns a list of devices, their public BGP peers, and the prefixes advertised to each peer.

Note that the line

     where neighbor.neighborAddress not in nonPublicIps 

limits the output to peers whose IP is not an RFC 1918 or link-local IP address.

/**
* @intent Lists public BGP peers and what prefixes are advertised to them
*/

import "@fwd/L3/IpAddressUtils";

// The prefixes that are advertised from device to neighborAddress
getAdvRoutes(device, neighborAddress) =
foreach x in 1]
where isPresent(device.bgpRib)
let bgpRib = device.bgpRib
foreach afiSafi in bgpRib.afiSafis
foreach neighbor in afiSafi.neighbors
where neighbor.neighborAddress == neighborAddress
where isPresent(neighbor.adjRibOutPost)
let adjRibOutPost = neighbor.adjRibOutPost
foreach route in adjRibOutPost.routes
select distinct route.prefix;

// List of IP subnets to exclude
nonPublicIps = ipAddressSet(privateSubnets + sipSubnet("169.254.0.0/16")]);

foreach device in network.devices
foreach networkInstance in device.networkInstances
foreach protocol in networkInstance.protocols
where isPresent(protocol.bgp)
let bgp = protocol.bgp
foreach neighbor in bgp.neighbors
where isPresent(neighbor.sessionState)
where neighbor.enabled
where neighbor.neighborAddress not in nonPublicIps
select {
Device: device.name,
"Neighbor Address": neighbor.neighborAddress,
"Session State": neighbor.sessionState,
"Neighbor Router ID": neighbor.peerRouterId,
"Advertised Prefixes": getAdvRoutes(device, neighbor.neighborAddress)
}

 

Be the first to reply!

Reply