HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/hosts.cpython-310.pyc
o

�N�g�)�@s�dZddlZddlZddlZddlZddlmmZddl	Ze�
e�Zdd�Z
dd�Zdd�Zd	d
�Zdd�Zd
d�Zdd�Zddd�Zdd�Zdd�Zdd�Zdd�ZdS)z*
Manage the information in the hosts file
�NcCs6ztdWStytdd�td<tdYSw)z7
    Return the path to the appropriate hosts file
    zhosts.__get_hosts_filenamez
config.optionz
hosts.file)�__context__�KeyErrorZ__salt__�rr�F/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/hosts.py�__get_hosts_filenames
��rcCsTt�}|dur	d}tj�|�s(tjj�|d��	Wd�|S1s#wY|S)zX
    Wrapper of __get_hosts_filename but create host file if it
    does not exist.
    N��w)r�os�path�exists�salt�utils�files�fopen)�hfnrrr�_get_or_create_hostfiles
��rc	
Cs�ztdWSty�d}t�}t��}z�tjj�|��~}|D]s}tjj	�
|���}|s.q |�d�rD|�
d|��g��|�|d7}q d}d|vrb||�d�dd���}|d|�d����}|��}|�d�}|r�|�
|i��
dg��|�|�
|i��d|i�q |�
|i��
dg��|�q Wd�n1s�wYWn!ty�}ztjjj||tjd	�|WYd}~YSd}~ww|td<|YSw)
zG
    Return the hosts found in the hosts file in as an OrderedDict
    �hosts._list_hostsr�#zcomment-�N�aliases�comment)�ignore)rrr�odict�OrderedDictrr
rr�stringutilsZ
to_unicode�strip�
startswith�
setdefault�append�index�lstrip�split�pop�extend�update�OSErrorZprocess_read_exception�errno�ENOENT)	�countr�retZifile�liner�comps�ip�excrrr�_list_hosts,sJ


������r.cCs
tt��S)z�
    Return the hosts found in the hosts file in this format::

        {'<ip addr>': ['alias1', 'alias2', ...]}

    CLI Example:

    .. code-block:: bash

        salt '*' hosts.list_hosts
    )�dictr.rrrr�
list_hostsUs

r0cCsRt�}|sdS|D]}t||t�r&d||vr&||d}||vr&|Sq	dS)z�
    Return the ip associated with the named host

    CLI Example:

    .. code-block:: bash

        salt '*' hosts.get_ip <hostname>
    rr)r.�
isinstancer/)�host�hosts�addrZ_hostsrrr�get_ipes
�r5cCs"t�}|t|�vr||dSgS)aB
    Return the list of aliases associated with an ip

    Aliases (host names) are returned in the order in which they
    appear in the hosts file.  If there are no aliases associated with
    the IP, an empty list is returned.

    CLI Example:

    .. code-block:: bash

        salt '*' hosts.get_alias <ip addr>
    r)r.�list)r,r3rrr�	get_alias|sr7cCsPt�}zt|t�rt|��||d�WS|||dvWSty'YdSw)z�
    Return true if the alias is set

    CLI Example:

    .. code-block:: bash

        salt '*' hosts.has_pair <ip> <alias>
    rF)r.r1r6�set�issubsetr)r,�aliasr3rrr�has_pair�s

�r;cCs�t�}d}tj�|�s
dSt�dd�|r(tjj�	|d|d|tj
�}ntjj�	|d|tj
�}|��s<d}tjj�
|d��}|��}Wd�n1sSwYt|�D]/\}}	||��}
|
siq\|
�d�roq\|
��}|d	tjj�	|�kr�|s�|||<d
}q\d||<q\tjj�	tj
�}|s�|r�|d�|�s�|d|7<|}
|�|
�tjj�
|d��}|�|�Wd�d
S1s�wYd
S)
a�
    Set the host entry in the hosts file for the given ip, this will overwrite
    any previous entry for the given ip

    .. versionchanged:: 2016.3.0
        If ``alias`` does not include any host names (it is the empty
        string or contains only whitespace), all entries for the given
        IP address are removed.

    CLI Example:

    .. code-block:: bash

        salt '*' hosts.set_host <ip> <alias>
    FrNz		z		# ��rb�#rT����wb)rr	r
�isfilerr"rr
r�to_bytes�lineseprrr�	readlines�	enumeraterr!�endswithr�
writelines)r,r:rrZovrZline_to_add�fp_�lines�ind�_�tmpliner+Z
linesep_bytesr*�ofilerrr�set_host�sP�
�
�

��rNcCs�t||�sdSt�dd�t�}tjj�|d��}|��}Wd�n1s'wYt	|�D]�\}}||�
�}|s=q0|�d�rCq0|��}d}	d|vr^|�d�\}
}	tjj
�|	���}	n|}
tjj
�|
�}
|
��}tjj
�|�}tjj
�|�}|d|kr�|dd}
|dd�D]
}||kr�q�|
|d	7}
q�|
�
�|kr�d
||<q0|	r�|
d|	tjj
�tj�||<q0|
tjj
�tj�||<q0tjj�|d��}|�|�Wd�dS1s�wYdS)
z�
    Remove a host entry from the hosts file

    CLI Example:

    .. code-block:: bash

        salt '*' hosts.rm_host <ip> <alias>
    TrNr=r>rs		r� r<s# r@)r;rr"rrr
rrrDrErrr!rrBr r	rCrG)r,r:rrHrIrJrKrLr+r�	host_infoZb_ipZb_alias�newline�existingrMrrr�rm_host�s`


�

�����
��rSc	Cs�t�}tj�|�sdSt||�rdSt�}t�dd�d}|��D]#\}}t	|�D]\}}t
|t�rC|�d�rC||krC|�
||�d}q)q!|sT|�|i��dg��|�t|�dS)z�
    Add a host to an existing entry, if the entry is not in place then create
    it with the given host

    CLI Example:

    .. code-block:: bash

        salt '*' hosts.add_host <ip> <alias>
    FTrNrr)rr	r
rAr;r.rr"�itemsrEr1r6r�insertrr�_write_hosts)	r,r:rr3Zinserted�i�h�numr2rrr�add_hosts(

��rZcCs�t�}tj�|�sdSt�}t�dd�||vrdSd||vr6|||dkr4|||d<t|�dSdS|||d<t|�dS)z�
    Set the comment for a host to an existing entry,
    if the entry is not in place then return False

    CLI Example:

    .. code-block:: bash

        salt '*' hosts.set_comment <ip> <comment>
    FrNrT)rr	r
rAr.rr"rV)r,rrr3rrr�set_comment@s �r[c	Cs�g}|��D]4\}}|r5|�d�rd�|�}nd|vr*d�|d�|d�|d�}nd�|d�|d��}|�|�qt�}tjj�	|d��#}|D]}|�
�r`|�tjj�
|�
�ttj���qIWd�dS1slwYdS)Nrrz{}		{}		# {}� rz{}		{}zw+)rTr�join�formatrrrr
rrr�writerZto_str�strr	rC)r3rIr,rPr*rrMrrrrVcs*
����"�rV)N)�__doc__r&�loggingr	Zsalt.utils.filesrZsalt.utils.odictr
rZsalt.utils.stringutils�	getLogger�__name__�logrrr.r0r5r7r;rNrSrZr[rVrrrr�<module>s(

)
=;$#