From e258ff2ed6bdd28c18218937126c3dee193d109c Mon Sep 17 00:00:00 2001 From: Sebastian Wiesinger Date: Wed, 24 Jan 2024 11:34:31 +0100 Subject: [PATCH] shut-unestablished-bgp-peers: Add support IPv6 neighbors Add support to also shut down IPv6 BGP neighbors. Also report which neighbors were shut down in the log message for better debugging. --- .../shut-unestablished-bgp-peers/script.py | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/shut-unestablished-bgp-peers-action-pack/shut-unestablished-bgp-peers/script.py b/shut-unestablished-bgp-peers-action-pack/shut-unestablished-bgp-peers/script.py index 93f6d6f..df2dfb4 100644 --- a/shut-unestablished-bgp-peers-action-pack/shut-unestablished-bgp-peers/script.py +++ b/shut-unestablished-bgp-peers-action-pack/shut-unestablished-bgp-peers/script.py @@ -30,9 +30,15 @@ cmdOut: List[Dict] = ctx.runDeviceCmds(["enable", "show ip bgp summary vrf all"]) errs: List[str] = [resp.get('error') for resp in cmdOut if resp.get('error')] if errs: - raise ActionFailed(f"Unable to get BGP summary, failed with: {errs[0]}") + raise ActionFailed(f"Unable to get IPv4 BGP summary, failed with: {errs[0]}") bgpSummary = cmdOut[1]["response"] +cmdOut: List[Dict] = ctx.runDeviceCmds(["enable", "show ipv6 bgp summary vrf all"]) +errs: List[str] = [resp.get('error') for resp in cmdOut if resp.get('error')] +if errs: + raise ActionFailed(f"Unable to get IPv6 BGP summary, failed with: {errs[0]}") +bgpv6Summary = cmdOut[1]["response"] + checkEvpn = True cmdOut: List[Dict] = ctx.runDeviceCmds(["enable", "show bgp evpn summary"]) errs: List[str] = [resp.get('error') for resp in cmdOut if resp.get('error')] @@ -46,23 +52,25 @@ if pendingPeersVrfList: bgpASN = None for vrf in pendingPeersVrfList: - for peer in bgpSummary['vrfs'][vrf]['peers']: + for peer, peerSummary in tuple(bgpSummary['vrfs'][vrf]['peers'].items()) + tuple(bgpv6Summary['vrfs'][vrf]['peers'].items()): # Check to see that the reason the the peer state is # pending is not due to administrative action if ( - bgpSummary['vrfs'][vrf]['peers'][peer]['peerState'] != "Established" + peerSummary['peerState'] != "Established" and not ( - bgpSummary['vrfs'][vrf]['peers'][peer]['peerState'] == "Idle" - and bgpSummary['vrfs'][vrf]['peers'][peer]['peerStateIdleReason'] == "Admin" + peerSummary['peerState'] == "Idle" + and peerSummary['peerStateIdleReason'] == "Admin" ) ): shutdownBgpPeerList.append((vrf, peer)) - bgpASN = bgpSummary['vrfs'][vrf]['asn'] + if ":" in peer: + bgpASN = bgpv6Summary['vrfs'][vrf]['asn'] + else: + bgpASN = bgpSummary['vrfs'][vrf]['asn'] if checkEvpn: bgpEvpnSummary = cmdOut[1]["response"] # Check the peer EVPN status for the default vrf - for peer in bgpEvpnSummary['vrfs']['default']['peers']: - peerSummary = bgpEvpnSummary['vrfs']['default']['peers'][peer] + for peer, peerSummary in bgpEvpnSummary['vrfs']['default']['peers'].items(): if ( peerSummary['peerState'] != "Established" and not ( @@ -108,6 +116,6 @@ if errs: raise ActionFailed(f"Failed to shut down all peers with: {errs[0]}") - ctx.info("Inactive BGP peers successfully shutdown") + ctx.info(f"Inactive BGP peers successfully shutdown: {shutdownBgpPeerList}") else: ctx.info("No inactive BGP peers to shutdown")