
    Vhx                     x    d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZ d Zd Zd	 Zd
 Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: nxos_ping
extends_documentation_fragment:
- cisco.nxos.nxos
short_description: Tests reachability using ping from Nexus switch.
description:
- Tests reachability using ping from switch to a remote destination.
- For a general purpose network module, see the M(ansible.netcommon.net_ping) module.
- For Windows targets, use the M(ansible.windows.win_ping) module instead.
- For targets running Python, use the M(ansible.builtin.ping) module instead.
version_added: 1.0.0
author:
- Jason Edelman (@jedelman8)
- Gabriele Gerbino (@GGabriele)
options:
  dest:
    description:
    - IP address or hostname (resolvable by switch) of remote node.
    required: true
    type: str
  count:
    description:
    - Number of packets to send.
    default: 5
    type: int
  source:
    description:
    - Source IP Address or hostname (resolvable by switch)
    type: str
  vrf:
    description:
    - Outgoing VRF.
    type: str
  df_bit:
    description:
    - Set the DF bit.
    default: false
    type: bool
  size:
    description:
    - Size of packets to send.
    type: int
  state:
    description:
    - Determines if the expected result is success or fail.
    choices:
    - absent
    - present
    default: present
    type: str
notes:
- Unsupported for Cisco MDS
- For a general purpose network module, see the M(ansible.netcommon.net_ping) module.
- For Windows targets, use the M(ansible.windows.win_ping) module instead.
- For targets running Python, use the M(ansible.builtin.ping) module instead.
a  
- name: Test reachability to 8.8.8.8 using mgmt vrf
  cisco.nxos.nxos_ping:
    dest: 8.8.8.8
    vrf: management
    host: 68.170.147.165

- name: Test reachability to a few different public IPs using mgmt vrf
  cisco.nxos.nxos_ping:
    dest: "{{ item }}"
    vrf: management
    host: 68.170.147.165
  with_items:
    - 8.8.8.8
    - 4.4.4.4
    - 198.6.1.4

- name: Test reachability to 8.8.8.8 using mgmt vrf, size and df-bit
  cisco.nxos.nxos_ping:
    dest: 8.8.8.8
    df_bit: true
    size: 1400
    vrf: management
aU  
commands:
    description: Show the command sent
    returned: always
    type: list
    sample: ["ping 8.8.8.8 count 2 vrf management"]
rtt:
    description: Show RTT stats
    returned: always
    type: dict
    sample: {"avg": 6.264, "max": 6.564, "min": 5.978}
packets_rx:
    description: Packets successfully received
    returned: always
    type: int
    sample: 2
packets_tx:
    description: Packets successfully transmitted
    returned: always
    type: int
    sample: 2
packet_loss:
    description: Percentage of packets lost
    returned: always
    type: str
    sample: "0.00%"
)AnsibleModule)run_commandsc                 r   | |dz      }|j                  d      }t        t        |d   j                  d      d   j                               t        |d   j                  d      d   j                               |d   j                  d      d   j                               }d| |dz
     vrd	}||fS d
}||fS )N   ,r   packets   packet)
packets_tx
packets_rxpacket_lossz
bytes fromFT)splitdictintstrip)results_listreference_pointsummary_stringsummary_listsummary	ping_passs         h/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cisco/nxos/plugins/modules/nxos_ping.pyget_summaryr      s    !/A"56N!'',L|A,,Y7:@@BC|A,,Y7:@@BC O))(3A6<<>G <!(;<<	 I 	I    c                    t        d d d       }|dk7  rn| |   }|j                  d      d   }|j                  d      }t        |d   j                               |d<   t        |d         |d<   t        |d	   d d
       |d<   |S )N)minavgmaxz100.00%=r	   /r   r   r    r   r!   )r   r   floatlstrip)r   r   locationrtt
rtt_stringbasertt_lists          r   get_rttr,      s    
4Tt
,Ci!(+
$Q'::c?8A;--/0E
8A;'E
8A;s+,E
Jr   c                 @    | D ]  }d|v s| j                  |      } S )Nz---)index)response_as_listeachr.   s      r   get_statistics_summary_liner1      s/      1D=$**40E1 Lr   c           
         | dd}t        ||g      d   }d\  }}}|sZ|j                  d| |j                  d   |j                  d   |j                  d   |j                  d	   |j                  d
          n|d|v r|j                  d|        ndd|v r"|j                  d| |j                  d          n>|j                  d      }t	        |      }t        ||      \  }}t        ||d   |dz         }|||fS )Ntext)commandoutputr   )NNNz/An unexpected error occurred. Check all params.destvrfsourcesizedf_bit)msgr4   destinationr7   r8   r9   r:   zcan't bind to addresszCan't bind to source address.)r;   r4   zbad contextzWrong VRF name inserted.)r;   r4   r7   
r   r   )r   	fail_jsonparamsr   r1   r   r,   )	r4   modulecmdpingr   r(   r   splitted_pingr   s	            r   get_ping_resultsrD      s   
0C&q)D.GS)Af-e$==*v&==* 	 	
 
!D	(<gN	$	*e$ 	 	
 

4(5mD(HmW]%;_q=PQS)$$r   c                     t        t        d      t        ddd      t        d      t        d      t        dd      t        ddd      t        dd	d
gd	            } t        | d      }|j                  d   }|j                  d   }|j                  d   }|j                  d   }dj                  |      }dD ])  }|j                  |   }|s|dj                  ||      z  }+ |r|dj                  |      z  }|r|dz  }t	        ||      \  }	}
}|	}|
|d<   |g|d<   |r|d
k(  r|j                  d       n|s|d	k(  r|j                  d        |j                  di | y )NT)requiredF   r   )rF   defaulttype)rF   rI   boolpresentabsent)rF   choicesrH   )r6   countr7   r8   r9   r:   state)argument_specsupports_check_moder6   rO   r9   r:   zping {0})rN   r8   r7   z {0} {1}z packet-size {0}z df-bitr(   commandszPing succeeded unexpectedly)r;   zPing failed unexpectedly )r   r   r?   formatrD   r>   	exit_json)rP   r@   r<   rO   r9   r:   ping_commandr4   argr   r(   r   resultss                r   mainrY      s   4 E159% U#5u-UE?EIx+@)TM DQF--'KMM'"E== D]]8$F$$[1L- <mmG$J--gs;;L<
 *11$77	!.|VDGS)GGEN'.GJUh&:;5I-78Fwr   __main__N)
__future__r   r   r   rI   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Eansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxosr   r   r,   r1   rD   rY   __name__rS   r   r   <module>rc      sb   $ A @ 7r2
6 5 ^$%D) X zF r   