HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/runners/__pycache__/bgp.cpython-310.pyc
o

�N�g�E�@s�dZddlZzddlmZddlmZmZdZ	Wne
y#dZ	YnwdZdZd	Z
dZd
Zgd�Zgd�Zd
ddddddd�Zdd�Zdd�Zd!dd�Zdd�Zeefdd�Zdd �ZdS)"a�
BGP Finder
==========

.. versionadded:: 2017.7.0

Runner to search BGP neighbors details.

Configuration
-------------

- Minion (proxy) config

    The ``bgp.neighbors`` function must be appened in the list of ``mine_functions``:

    .. code-block:: yaml

        mine_functions:
          bgp.neighbors: []

    Which instructs Salt to cache the data returned by the ``neighbors`` function
    from the :mod:`NAPALM BGP module <salt.modules.napalm_bgp.neighbors>`.

    How often the mines are refreshed, can be specified using:

    .. code-block:: yaml

        mine_interval: <X minutes>

- Master config

    By default the following options can be configured on the master.
    They are not mandatory, but available in case the user has different requirements.

    tgt: ``*``
        From what minions will collect the mine data.
        Default: ``*`` (collect mine data from all minions)

    tgt_type: ``glob``
        Minion matching expression form. Default: ``glob``.

    return_fields
        What fields to return in the output.
        It can display all the fields from the ``neighbors`` function
        from the :mod:`NAPALM BGP module <salt.modules.napalm_bgp.neighbors>`.

        Some fields cannot be removed:

        - ``as_number``: the AS number of the neighbor
        - ``device``: the minion ID
        - ``neighbor_address``: the neighbor remote IP address

        By default, the following extra fields are returned (displayed):

        - ``connection_stats``: connection stats, as described below
        - ``import_policy``: the name of the import policy
        - ``export_policy``: the name of the export policy

        Special fields:

        - ``vrf``: return the name of the VRF.
        - ``connection_stats``: returning an output of the form ``<State>
          <Active>/<Received>/<Accepted>/<Damped>``, e.g.  ``Established
          398/399/399/0`` similar to the usual output from network devices.
        - ``interface_description``: matches the neighbor details with the
          corresponding interface and returns its description. This will reuse
          functionality from the :mod:`net runner
          <salt.runners.net.interfaces>`, so the user needs to enable the mines
          as specified in the documentation.
        - ``interface_name``: matches the neighbor details with the
          corresponding interface and returns the name.  Similar to
          ``interface_description``, this will reuse functionality from the
          :mod:`net runner <salt.runners.net.interfaces>`, so the user needs to
          enable the mines as specified in the documentation.

    display: ``True``
        Display on the screen or return structured object? Default: ``True`` (return on the CLI).

    outputter: ``table``
        Specify the outputter name when displaying on the CLI. Default: :mod:`table <salt.output.table_out>`.

    Configuration example:

    .. code-block:: yaml

        runners:
          bgp:
            tgt: 'edge*'
            tgt_type: 'glob'
            return_fields:
              - up
              - connection_state
              - previous_connection_state
              - suppress_4byte_as
              - holdtime
              - flap_count
            outputter: yaml
�N)�helpers)�	IPAddress�	IPNetworkTF�bgp�*�glob�table)�device�	as_number�neighbor_address)�connection_stats�
import_policy�
export_policyZDevicez	AS NumberzNeighbor IPz&State|#Active/Received/Accepted/Dampedz	Policy INz
Policy OUTZVRF)r	r
rrr
r�vrfcCstrtSdS)N)Fz'The napalm module could not be imported)�
HAS_NAPALM�__virtualname__�rr�D/opt/saltstack/salt/lib/python3.10/site-packages/salt/runners/bgp.py�__virtual__�srcCsPt�di��di�}|�dt�|�dt�|�dt�t|�dt�|�dt�d�S)	z(
    Return the bgp runner options.
    Zrunnersr�tgt�tgt_type�display�
return_fields�	outputter)rrrrr)�__opts__�get�_DEFAULT_TARGET�_DEFAULT_EXPR_FORM�_DEFAULT_DISPLAY�_DEFAULT_INCLUDED_FIELDS�_DEFAULT_RETURN_FIELDS�_DEFAULT_OUTPUTTER)Zrunner_optsrrr�_get_bgp_runner_opts�s



�
�r"cCs$|st�}td|dd|dd�S)z\
    Helper to return the mine data from the minions, as configured on the runner opts.
    zmine.getrz
bgp.neighborsr)r)r"�__salt__��optsrrr�	_get_mine�sr&cCs0|��D]\}}||vr|||krdSqdS)zT
    Compare two dictionaries and return a boolean value if their values match.
    FT)�items)Zdict1Zdict2�kargZwargrrr�_compare_match�s
�r)cCsL|r$|dkrtjj||d�dt|ddd�}ntj�||t�}t|�dS|S)z%
    Display or return the rows.
    r)�rows�labelsr*r+)�titleZrows_keyZ
labels_keyN)�salt�outputZ
out_formatr�print)r*r+r,rr�retrrr�_display_runner�s�	r1c
s�t�}��dd�}��d|d�}��d|d�}i}|���|��D]\}}|�d�r2��|�q$|s?�s?|r=td�gS��dd�}	��dd�}
��d	d�}|rWt|�nd}g}
i}|d
D]}|tvrnt|||<qad�t	dd
�|�
d���||<qatt|d
�tt
��}t|d�}|s�g}|r�|�djd�dd�|D��d��|
r�|�dj|
d��|r�|�d|����r�|�djd�t	�fdd
����d��d�|�}|��D]�\}}|�d�s�q�|	r�||	kr�q�|�di�}|��D]�\}}|��D]�\}}|�r	||v�r	q�|D]�}��rt�|��s�q|
�r%|
|�d�k�r%�q|�r<|�d��r<t|�d��}||v�r<�q||�d�|d�}d |v�rN||d <d!|v�rud"j|�d#d$�|�d%d$�|�d&d$�|�d'd$�|�d(d$�d)�}||d!<d*|v�sd+|v�r�td,||�d�d-d.�}|�r�d*|v�r�|d/d*|d*<d+|v�r�|d/d0|d+<nd*|v�r�d1|d*<d+|v�r�d1|d+<|D]}||v�r�||||<�q�|
�|��qq�q�q�t|
||||d2�S)3a�
    Search for BGP neighbors details in the mines of the ``bgp.neighbors`` function.

    Arguments:

    asns
        A list of AS numbers to search for.
        The runner will return only the neighbors of these AS numbers.

    device
        Filter by device name (minion ID).

    ip
        Search BGP neighbor using the IP address.
        In multi-VRF environments, the same IP address could be used by
        more than one neighbors, in different routing tables.

    network
        Search neighbors within a certain IP network.

    title
        Custom title.

    display: ``True``
        Display on the screen or return structured object? Default: ``True`` (return on the CLI).

    outputter: ``table``
        Specify the outputter name when displaying on the CLI. Default: :mod:`table <salt.output.table_out>`.

    In addition, any field from the output of the ``neighbors`` function
    from the :mod:`NAPALM BGP module <salt.modules.napalm_bgp.neighbors>` can be used as a filter.

    CLI Example:

    .. code-block:: bash

        salt-run bgp.neighbors 13335 15169
        salt-run bgp.neighbors 13335 ip=172.17.19.1
        salt-run bgp.neighbors multipath=True
        salt-run bgp.neighbors up=False export_policy=my-export-policy multihop=False
        salt-run bgp.neighbors network=192.168.0.0/16

    Output example:

    .. code-block:: text

        BGP Neighbors for 13335, 15169
        ________________________________________________________________________________________________________________________________________________________________
        |    Device    | AS Number |         Neighbor Address        | State|#Active/Received/Accepted/Damped |         Policy IN         |         Policy OUT         |
        ________________________________________________________________________________________________________________________________________________________________
        | edge01.bjm01 |   13335   |          172.17.109.11          |        Established 0/398/398/0         |       import-policy       |        export-policy       |
        ________________________________________________________________________________________________________________________________________________________________
        | edge01.bjm01 |   13335   |          172.17.109.12          |       Established 397/398/398/0        |       import-policy       |        export-policy       |
        ________________________________________________________________________________________________________________________________________________________________
        | edge01.flw01 |   13335   |          192.168.172.11         |        Established 1/398/398/0         |       import-policy       |        export-policy       |
        ________________________________________________________________________________________________________________________________________________________________
        | edge01.oua01 |   13335   |          172.17.109.17          |          Established 0/0/0/0           |       import-policy       |        export-policy       |
        ________________________________________________________________________________________________________________________________________________________________
        | edge01.bjm01 |   15169   |             2001::1             |       Established 102/102/102/0        |       import-policy       |        export-policy       |
        ________________________________________________________________________________________________________________________________________________________________
        | edge01.bjm01 |   15169   |             2001::2             |       Established 102/102/102/0        |       import-policy       |        export-policy       |
        ________________________________________________________________________________________________________________________________________________________________
        | edge01.tbg01 |   13335   |          192.168.172.17         |          Established 0/1/1/0           |       import-policy       |        export-policy       |
        ________________________________________________________________________________________________________________________________________________________________
    r,NrrZ__pubz8Please specify at least an AS Number or an output filterr	�ip�networkr� cSs|��S�N)r,)�wordrrr�<lambda>;szneighbors.<locals>.<lambda>�_r$zBGP Neighbors for {asns}z, cSsg|]}t|��qSr)�str)�.0�asnrrr�
<listcomp>Dszneighbors.<locals>.<listcomp>)�asnsz:Selecting neighbors having the remote IP address: {ipaddr})Zipaddrz+Selecting neighbors within the IP network: z<Searching for BGP neighbors having the attributes: {attrmap}csdj|�|d�S)Nz
{key}={value})�key�value)�format)r>��kwargsrrr7Ts�)Zattrmap�
�result�outZremote_address)r	rr
rrz/{state} {active}/{received}/{accepted}/{damped}Zconnection_state���Zactive_prefix_countZreceived_prefix_countZaccepted_prefix_countZsuppressed_prefix_count)�stateZactiveZreceivedZacceptedZdampedZinterface_descriptionZinterface_nameznet.interfacesF)r	�ipnetrrZ	interface�)rr)r"�pop�updater'�
startswithr/r�_DEFAULT_LABELS_MAPPING�join�map�split�list�setrr&�appendr@rr)rr#r1)r=rBr%r,rrZkwargs_copyr(r8r	Zneighbor_iprHZ	ipnet_objr*r+�fieldZdisplay_fieldsZget_bgp_neighbors_allZtitle_partsZminionZget_bgp_neighbors_minionZget_bgp_neighbors_minion_outrZvrf_bgp_neighborsr;Zget_bgp_neighbors_minion_asnZneighborZneighbor_ip_obj�rowrZnet_findrrAr�	neighbors�s�B


� 
����
����
�
�
�






��	

�
�
�


����CrVr5)�__doc__Zsalt.outputr-Znapalm.baserZnapalm_helpersZnetaddrrrr�ImportErrorrrrrr!rr rMrr"r&r)r1rVrrrr�<module>s>c��


�