MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / scan

Method scan

pager_lib/nmap/nmap.py:201–311  ·  view source on GitHub ↗

Scan given hosts May raise PortScannerError exception if nmap output was not xml Test existance of the following key to know if something went wrong : ['nmap']['scaninfo']['error'] If not present, everything was ok. :param hosts: string for hosts a

(  # NOQA: CFQ001, C901
        self, hosts="127.0.0.1", ports=None, arguments="-sV", sudo=False, timeout=0
    )

Source from the content-addressed store, hash-verified

199 return self.all_hosts()
200
201 def scan( # NOQA: CFQ001, C901
202 self, hosts="127.0.0.1", ports=None, arguments="-sV", sudo=False, timeout=0
203 ):
204 """
205 Scan given hosts
206
207 May raise PortScannerError exception if nmap output was not xml
208
209 Test existance of the following key to know
210 if something went wrong : ['nmap']['scaninfo']['error']
211 If not present, everything was ok.
212
213 :param hosts: string for hosts as nmap use it 'scanme.nmap.org' or '198.116.0-255.1-127' or '216.163.128.20/20'
214 :param ports: string for ports as nmap use it '22,53,110,143-4564'
215 :param arguments: string of arguments for nmap '-sU -sX -sC'
216 :param sudo: launch nmap with sudo if True
217 :param timeout: int, if > zero, will terminate scan after seconds, otherwise will wait indefintely
218
219 :returns: scan_result as dictionnary
220 """
221 if sys.version_info[0] == 2:
222 assert type(hosts) in (
223 str,
224 ), f"Wrong type for [hosts], should be a string [was {type(hosts)}]"
225
226 assert type(ports) in (
227 str,
228 type(None),
229 ), f"Wrong type for [ports], should be a string [was {type(ports)}]"
230 assert type(arguments) in (
231 str,
232 ), f"Wrong type for [arguments], should be a string [was {type(arguments)}]"
233 else:
234 assert (
235 type(hosts) is str
236 ), f"Wrong type for [hosts], should be a string [was {type(hosts)}]"
237 assert type(ports) in (
238 str,
239 type(None),
240 ), f"Wrong type for [ports], should be a string [was {type(ports)}]"
241 assert (
242 type(arguments) is str
243 ), f"Wrong type for [arguments], should be a string [was {type(arguments)}]"
244
245 for redirecting_output in ["-oX", "-oA"]:
246 assert (
247 redirecting_output not in arguments
248 ), "Xml output can't be redirected from command line.\nYou can access it after a scan using:\nnmap.nm.get_nmap_last_output()" # noqa
249
250 h_args = shlex.split(hosts)
251 f_args = shlex.split(arguments)
252
253 # Launch scan
254 args = (
255 [self._nmap_path, "-oX", "-"]
256 + h_args
257 + ["-p", ports] * (ports is not None)
258 + f_args

Callers 3

listscanMethod · 0.95
__scan_progressive__Function · 0.45
scanMethod · 0.45

Calls 5

analyse_nmap_xml_scanMethod · 0.95
PortScannerTimeoutClass · 0.85
killMethod · 0.80
appendMethod · 0.80
decodeMethod · 0.45

Tested by

no test coverage detected