
    Vh+                        d dl mZ dZdZd dlZd dlZd dlmZmZ d dl	m
Z d dlmZ d dlmZmZ d d	lmZmZmZ d d
lmZ d dlmZ  G d deee      Zy)    )annotationsaZ  
author: Unknown (!UNKNOWN)
name: nmap
short_description: Uses nmap to find hosts to target
description:
  - Uses a YAML configuration file with a valid YAML extension.
extends_documentation_fragment:
  - constructed
  - inventory_cache
requirements:
  - nmap CLI installed
options:
  plugin:
    description: token that ensures this is a source file for the 'nmap' plugin.
    type: string
    required: true
    choices: ['nmap', 'community.general.nmap']
  sudo:
    description: Set to V(true) to execute a C(sudo nmap) plugin scan.
    version_added: 4.8.0
    default: false
    type: boolean
  address:
    description: Network IP or range of IPs to scan, you can use a simple range (10.2.2.15-25) or CIDR notation.
    type: string
    required: true
    env:
      - name: ANSIBLE_NMAP_ADDRESS
        version_added: 6.6.0
  exclude:
    description:
      - List of addresses to exclude.
      - For example V(10.2.2.15-25) or V(10.2.2.15,10.2.2.16).
    type: list
    elements: string
    env:
      - name: ANSIBLE_NMAP_EXCLUDE
        version_added: 6.6.0
  port:
    description:
      - Only scan specific port or port range (C(-p)).
      - For example, you could pass V(22) for a single port, V(1-65535) for a range of ports,
        or V(U:53,137,T:21-25,139,8080,S:9) to check port 53 with UDP, ports 21-25 with TCP, port 9 with SCTP, and ports 137, 139, and 8080 with all.
    type: string
    version_added: 6.5.0
  ports:
    description: Enable/disable scanning ports.
    type: boolean
    default: true
  ipv4:
    description: use IPv4 type addresses
    type: boolean
    default: true
  ipv6:
    description: use IPv6 type addresses
    type: boolean
    default: true
  udp_scan:
    description:
      - Scan via UDP.
      - Depending on your system you might need O(sudo=true) for this to work.
    type: boolean
    default: false
    version_added: 6.1.0
  icmp_timestamp:
    description:
      - Scan via ICMP Timestamp (C(-PP)).
      - Depending on your system you might need O(sudo=true) for this to work.
    type: boolean
    default: false
    version_added: 6.1.0
  open:
    description: Only scan for open (or possibly open) ports.
    type: boolean
    default: false
    version_added: 6.5.0
  dns_resolve:
    description: Whether to always (V(true)) or never (V(false)) do DNS resolution.
    type: boolean
    default: false
    version_added: 6.1.0
  dns_servers:
    description: Specify which DNS servers to use for name resolution.
    type: list
    elements: string
    version_added: 10.5.0
  use_arp_ping:
    description: Whether to always (V(true)) use the quick ARP ping or (V(false)) a slower but more reliable method.
    type: boolean
    default: true
    version_added: 7.4.0
notes:
  - At least one of O(ipv4) or O(ipv6) is required to be V(true); both can be V(true), but they cannot both be V(false).
  - 'TODO: add OS fingerprinting'
a  
---
# inventory.config file in YAML format
plugin: community.general.nmap
strict: false
address: 192.168.0.0/24

---
# a sudo nmap scan to fully use nmap scan power.
plugin: community.general.nmap
sudo: true
strict: false
address: 192.168.0.0/24

---
# an nmap scan specifying ports and classifying results to an inventory group
plugin: community.general.nmap
address: 192.168.0.0/24
exclude: 192.168.0.1, web.example.com
port: 22, 443
groups:
  web_servers: "ports | selectattr('port', 'equalto', '443')"
N)PopenPIPE)	constants)AnsibleParserError)	to_nativeto_text)BaseInventoryPluginConstructable	Cacheable)get_bin_path)make_unsafec                       e Zd ZdZ ej
                  d      Z ej
                  d      Z fdZd Z	 fdZ
d fd	Z xZS )	InventoryModulezcommunity.general.nmapz<^Nmap scan report for ([\w,.,-]+)(?: \(([\w,.,:,\[,\]]+)\))?z^(\d+)/(\w+)\s+(\w+)\s+(\w+)c                8    d | _         t        t        |           y )N)_nmapsuperr   __init__)self	__class__s    l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/inventory/nmap.pyr   zInventoryModule.__init__   s    
ot-/    c                   | j                  d      }|D ]  }t        |      }|d   }| j                  j                  |       |j	                         D ]"  \  }}| j                  j                  |||       $ | j                  | j                  d      |||       | j                  | j                  d      |||       | j                  | j                  d      |||        y )Nstrictnamecompose)r   groupskeyed_groups)	
get_optionr   	inventoryadd_hostitemsset_variable_set_composite_vars_add_host_to_composed_groups_add_host_to_keyed_groups)r   hostsr   hosthostnamevarvalues          r   	_populatezInventoryModule._populate   s    * 	kDt$DF|HNN##H-"jjl B
U++Hc5AB $$T__Y%?xX^$_ --dooh.Gx`f-g **4??>+JDRZci*j	kr   c                    d}t         t        |   |      r8t        j                  j                  |      \  }}|r|t        j                  v rd}|S )NFT)r   r   verify_fileospathsplitextCYAML_FILENAME_EXTENSIONS)r   r0   valid	file_nameextr   s        r   r.   zInventoryModule.verify_file   sI    $3D9WW--d3NIs#!;!;;r   c           	        	 t        d      | _        t        t
        |   ||||       | j                  |       | j                  |      }| j                  d      }|xr |}|xr | }	|r	 | j                  |   }
|r|	rl| j                  g}| j                  d      r|j                  dd       | j                  d      r1|j                  d	       |j                  | j                  d             | j                  d
      s|j                  d       | j                  d      r#| j                  d      s|j                  d       na| j                  d      r#| j                  d      s|j                  d       n-| j                  d      s| j                  d      st        d      | j                  d      r@|j                  d       |j                  dj                  | j                  d                   | j                  d      r|j                  d       | j                  d      r@|j                  d       |j                  dj                  | j                  d                   | j                  d      r|j                  d       | j                  d      r|j                  d       | j                  d      r|j                  d       | j                  d      s|j                  d       |j                  | j                  d              	 t        |t         t         !      }|j#                         \  }}|j$                  dk7  r$t        d"|j$                   d#t'        |             d }d }g }g }
	 t)        |d$%      }|j-                         D ]D  }| j.                  j1                  |      }|r|
|r||
d'   d
<   |j3                  d(      j5                  d)      r|j3                  d*      }n|j3                  d(      }|j3                  d*      |j3                  d*      }n|j3                  d(      }|+|
j                  t7                      ||
d'   d+<   ||
d'   d,<   g }| j8                  j1                  |      }||s|j                  |j3                  d(      |j3                  d*      |j3                  d-      |j3                  d.      d/       G |r
|r||
d'   d
<   |	r
| j                  |<   | j=                  
       y # t        $ r}t        d|       d }~ww xY w# t        $ r d}	Y w xY w# t*        $ r}t        d&|       d }~ww xY w# t:        $ r }t        d0t'        |       d#| d1      d }~ww xY w)2Nnmapz:nmap inventory plugin requires the nmap cli tool to work: )cacher9   Tsudor   portz-pportsz-sPipv4ipv6z-4z-6z3One of ipv4 or ipv6 must be enabled for this pluginexcludez	--exclude,dns_resolvez-ndns_serversz--dns-serversudp_scanz-sUicmp_timestampz-PPopenz--openuse_arp_pingz--disable-arp-pingaddress)stdoutstderrzFailed to run nmap, rc=z: surrogate_or_strict)errorsz&Invalid (non unicode) input returned:    z.in-addr.arpa   r   ip      )r;   protocolstateservicezfailed to parse  )r   r   
ValueErrorr   r   r   parse_read_config_dataget_cache_keyr   _cacheKeyErrorinsertappendjoinr   r   communicate
returncoder   r	   UnicodeError
splitlines	find_hostmatchgroupendswithdict	find_port	Exceptionr,   )r   r    loaderr0   r9   e	cache_keyuser_cache_settingattempt_to_read_cachecache_needs_updateresultscmdprH   rI   r(   rO   r<   t_stdoutlinehits
host_portsr   s                         r   rW   zInventoryModule.parse   s   	g%f-DJ 	ot*9fd%*Pt$&&t,	 "__W5 !3 <u/=I *++i0
 "%7::,Cv&

1f%v&

4 

4??623??7+

5!v&tv/F

4 (1H

4 __V,T__V5L()^__y)

;'

388DOOI$>?@}-

4 }-

?+

388DOOM$BCDz*

5!/0

5!v&

8$??>2

/0JJty129U#d48!"<<1$,/Fq||nTVW`agWhVi-jkk [&v6KLH %//1  !D>>//5D+38GBK0  ::a=11/B#'::a=D#'::a=D  ::a=4!%AB!%AB+#NN46226GBK/02GBK-$&E !%!5!5d!;J'Jj.>.>q.A2<2B2B12E/9/?/?/B1;1A1A!1D&F G !A !F E+0GBK(
 %,DKK	"wI  	g$'abcad%eff	g*  *%)"*@ $ [,/UVWUX-YZZ[R  U(+;IdO;LBqcQR)STTUst   T -T/ A$U  <U 	C?U  	U  A!U  	T,T''T,/T>=T>	U
UUU   	V	)VV	)T)__name__
__module____qualname__NAMErecompilerc   rh   r   r,   r.   rW   __classcell__)r   s   @r   r   r      sF    #D

Z[I

:;I0k(	H  H r   r   )
__future__r   DOCUMENTATIONEXAMPLESr/   r{   
subprocessr   r   ansibler   r2   ansible.errorsr   +ansible.module_utils.common.text.convertersr   r	   ansible.plugins.inventoryr
   r   r   #ansible.module_utils.common.processr   Aansible_collections.community.general.plugins.plugin_utils.unsafer   r    r   r   <module>r      sM    #^~0 
 	 " " - J S S < Yq )=) q r   