File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/dnsutil.cpython-310.pyc
o
�N�g�- � @ s� d Z ddlZddlZddlZddlZddlZddlZe�e �Z
dd� Zd#dd�Zd$dd �Z
d#d
d�Zd%dd
�Zdd� Zdd� Zdd� Zd&dd�Zd&dd�Zd'dd�Zd(dd�Zd)dd�Zd*d!d"�ZdS )+z|
Compendium of generic DNS utilities.
.. note::
Some functions in the ``dnsutil`` execution module depend on ``dig``.
� Nc C s dS )z�
Generic, should work on any platform (including Windows). Functionality
which requires dependencies outside of Python do not belong in this module.
T� r r r �H/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/dnsutil.py�__virtual__ s r �
/etc/hostsc C s� |s0z#t jj�| d��}t jj�|�� �}W d � n1 sw Y W n
ty/ Y dS w i }|�� D ]"}|s;q6|� d�rAq6|�
� }|d }|dd� }|�|g ��|� q6|S )zr
Parse /etc/hosts file.
CLI Example:
.. code-block:: bash
salt '*' dnsutil.parse_hosts
�rNzError: hosts data was not found�#r � )
�salt�utils�files�fopen�stringutils�
to_unicode�read� Exception�
splitlines�
startswith�split�
setdefault�extend)� hostsfile�hosts�fp_Z hostsdict�line�comps�ip�aliasesr r r �parse_hosts s(
���
r c C s� |� d�}t| d�}||v r|D ]
}||| v r|�|� q|s%d| � �S d�|d�|��}tjj�| d��}|� tjj
�|�� W d � n1 sKw Y d| � d |� �S )
z�
Append a single line to the /etc/hosts file.
CLI Example:
.. code-block:: bash
salt '*' dnsutil.hosts_append /etc/hosts 127.0.0.1 ad1.yuk.co,ad2.yuk.co
�,)r z"No additional hosts were added to z
{} {}� �aNz The following line was added to �:)r r �remove�format�joinr r
r r �writer
�to_str)r �ip_addr�entries� host_listr �hostZappend_liner r r r �hosts_append; s
�
�r+ c C s$ t jj�| d��}t jj�|�� �}W d � n1 sw Y |�d�}t jj�| d��Z}|�� D ]L}|r>|� � �
d�rL|�t jj�|� d��� q3|�� }|D ]}||dd� v ra|�
|� qRt|�dkr|�t jj�d�|��� |�t jj�d�� q3W d � dS 1 s�w Y dS ) a�
Remove a host from the /etc/hosts file. If doing so will leave a line
containing only an IP address, then the line will be deleted. This function
will leave comments and blank lines intact.
CLI Examples:
.. code-block:: bash
salt '*' dnsutil.hosts_remove /etc/hosts ad1.yuk.co
salt '*' dnsutil.hosts_remove /etc/hosts ad2.yuk.co,ad1.yuk.co
r Nr �wr �
r r )r r
r r r
r r r r �stripr r% r&