本文整理汇总了Python中libnmap.process.NmapProcess.is_running方法的典型用法代码示例。如果您正苦于以下问题:Python NmapProcess.is_running方法的具体用法?Python NmapProcess.is_running怎么用?Python NmapProcess.is_running使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类libnmap.process.NmapProcess
的用法示例。
在下文中一共展示了NmapProcess.is_running方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: nmap_scan
# 需要导入模块: from libnmap.process import NmapProcess [as 别名]
# 或者: from libnmap.process.NmapProcess import is_running [as 别名]
def nmap_scan(hosts):
'''
Do Nmap scan
'''
# -sV is included by default in NmapProcess nmap cmd
# To add more: options = '-T4 -sU -p-'
# hosts = ['192.168.0.1', '192.168.0.2']
#nmap_args = '-T4 -sV -sS -pU:161,137,139'# -sS -sU --top-ports'
nmap_args = '-T4 -sS -sV --max-rtt-timeout 150ms --max-retries 3'
print '[*] Running: nmap {0} -iL <hostlist>'.format(nmap_args)
nmap_proc = NmapProcess(targets=hosts, options=nmap_args)
#rc = nmap_proc.sudo_run()
rc = nmap_proc.sudo_run_background()
while nmap_proc.is_running():
print("[*] Nmap progress: {1}%".format(nmap_proc.etc, nmap_proc.progress))
time.sleep(2)
xml = nmap_proc.stdout
try:
report = NmapParser.parse(nmap_proc.stdout)
except NmapParserException as e:
print 'Exception raised while parsing scan: {0}'.format(e.msg)
sys.exit()
return report
示例2: scan_host
# 需要导入模块: from libnmap.process import NmapProcess [as 别名]
# 或者: from libnmap.process.NmapProcess import is_running [as 别名]
def scan_host(target_host, num_tries):
nm = NmapProcess(target_host, options='-sT -Pn -T3')
cur_try = 0
while cur_try < num_tries:
logger.debug(
"Now running scan attempt #%d for host at %s."
% (cur_try + 1, target_host)
)
result = nm.run_background()
while nm.is_running():
logger.debug(
"Scan running. ETC: %s. DONE: %s."
% (nm.etc, nm.progress)
)
sleep(2)
if nm.rc != 0:
logger.warning(
"Scan #%d for host at %s failed!"
% (cur_try + 1, target_host)
)
cur_try += 1
else:
logger.debug(
"Scan for host at %s succeeded!"
% (target_host,)
)
break
if str(nm.state) != str(nm.DONE):
logger.warning(
"Scan for host at %s failed!"
% (target_host,)
)
return
else:
try:
parsed = NmapParser.parse(nm.stdout)
except NmapParserException as e:
logger.error(
"NmapParserException thrown: %s."
% (e.msg,)
)
host = parsed.hosts[0]
to_return = []
for service in host.services:
to_return.append(
"%s//%s//%s//%s"
% (
host.ipv4,
service.protocol,
str(service.port),
service.state
)
)
return to_return
示例3: run_nmap_scan
# 需要导入模块: from libnmap.process import NmapProcess [as 别名]
# 或者: from libnmap.process.NmapProcess import is_running [as 别名]
def run_nmap_scan(scan_targets, scan_options):
'''
Accepts scan targets and scan options for NmapProcess and launches scan
Prints scan status updates and summary to stdout
Returns NmapProcess object for further use
TODO - catch keyboard interrupts and kill tasks so we can exit gracefully!
nmap_proc.stop does not appear to fully kill threads in script scans
so program will continue to execute but leaves an orphaned nmap process
'''
status_update_interval = 5
#Check for sudo and disable scan options that require root
if os.getuid()!=0:
logging.warn("Certain nmap scans require root privileges (SYN, UDP, ICMP, etc)...")
logging.warn("Disabling incompatible scan types and OS / service version detection options if enabled")
scan_options = scan_options.replace("-sS", "-sT")
scan_options = scan_options.replace("-sU", "")
scan_options = scan_options.replace("-sP", "")
scan_options = scan_options.replace("-sn", "")
scan_options = scan_options.replace("-sV", "")
scan_options = scan_options.replace("-O", "")
nmap_proc = NmapProcess(targets=scan_targets, options=scan_options)
print "Running scan command:\n"+nmap_proc.command
nmap_proc.run_background()
while nmap_proc.is_running():
try:
time.sleep(status_update_interval)
if nmap_proc.progress > 0:
#Nmap only updates ETC periodically and will sometimes return a result that is behind current system time
etctime = datetime.datetime.fromtimestamp(int(nmap_proc.etc))
systime = datetime.datetime.now().replace(microsecond=0)
if etctime < systime:
etctime = systime
timeleft = etctime - systime
print("{0} Timing: About {1}% done; ETC: {2} ({3} remaining)".format(nmap_proc.current_task.name, nmap_proc.progress, etctime, timeleft))
except KeyboardInterrupt:
print "Keyboard Interrupt - Killing Current Nmap Scan!"
nmap_proc.stop()
if nmap_proc.rc == 0:
print nmap_proc.summary + "\n"
else:
print nmap_proc.stderr + "\n"
return nmap_proc
示例4: do_scan
# 需要导入模块: from libnmap.process import NmapProcess [as 别名]
# 或者: from libnmap.process.NmapProcess import is_running [as 别名]
def do_scan(self):
"""scan start flag"""
if(self._flg_is_scanning != True):
self._flg_is_scanning = True
if(self._flg_scan_finished != False):
self._flg_scan_finished = False
"""运行次数初始化"""
trycnt = 0
while True:
"""运行时间初始化"""
runtime = 0
if trycnt >= self.retrycnt:
print '-' * 50
return 'retry overflow'
try:
nmap_proc = NmapProcess(targets=self.targets,options=self.options,safe_mode=False)
self._flg_is_scanning = True
nmap_proc.run_background()
while(nmap_proc.is_running()):
"""运行超时,结束掉任务,休息1分钟,再重启这个nmap任务"""
if runtime >= self.timeout:
print '-' * 50
print "timeout....terminate it...."
nmap_proc.stop()
"""休眠时间"""
sleep(60)
trycnt += 1
break
else:
print 'running[%ss]:%s' %(runtime,nmap_proc.command)
sleep(5)
runtime += 5
if nmap_proc.is_successful():
"""scan finished flag"""
if(self._flg_is_scanning != False):
self._flg_is_scanning = False
if(self._flg_scan_finished != True):
self._flg_scan_finished = True
print '-' * 50
print nmap_proc.summary
return nmap_proc.stdout
except Exception,e:
print e
trycnt +=1
if trycnt >= retrycnt:
print '-' * 50
print 'retry overflow'
return e
示例5: do_scan
# 需要导入模块: from libnmap.process import NmapProcess [as 别名]
# 或者: from libnmap.process.NmapProcess import is_running [as 别名]
def do_scan(targets, options):
parsed = None
nmproc = NmapProcess(targets, options)
rc = nmproc.run_background()
while nmproc.is_running():
print("Nmap Scan is running: ETC: {0} DONE: {1}%".format(nmproc.etc, nmproc.progress))
sleep(2)
print("rc: {0} output: {1}".format(nmproc.rc, nmproc.summary))
if rc != 0:
print("nmap scan failed: {0}".format(nmproc.stderr))
print(type(nmproc.stdout))
try:
parsed = NmapParser.parse(nmproc.stdout)
except NmapParserException as e:
print("Exception raised while parsing scan: {0}".format(e.msg))
return parsed
示例6: do_nmap_scan
# 需要导入模块: from libnmap.process import NmapProcess [as 别名]
# 或者: from libnmap.process.NmapProcess import is_running [as 别名]
def do_nmap_scan(targets, options=global_options):
# 运行次数初始化
trycnt = 0
while True:
# 运行时间初始化
runtime = 0
if trycnt >= retrycnt:
print '-' * 50
return 'retry overflow'
try:
nmap_proc = NmapProcess(targets=targets, options=options, safe_mode=False)
nmap_proc.run_background()
while nmap_proc.is_running():
if runtime >= timeout: # 运行超时,结束掉任务,休息1分钟, 再重启这个nmap任务
print '-' * 50
print "* timeout. terminate it..."
nmap_proc.stop()
# 休眠时间
sleep(60)
trycnt += 1
break
else:
print 'running[%ss]:%s' % (runtime, nmap_proc.command)
sleep(5)
runtime += 5
if nmap_proc.is_successful():
print '-' * 50
print nmap_proc.summary
return nmap_proc.stdout
except Exception, e:
# raise e
print e
trycnt += 1
if trycnt >= retrycnt:
print '-' * 50
print '* retry overflow'
return e
示例7: do_nmap_scan
# 需要导入模块: from libnmap.process import NmapProcess [as 别名]
# 或者: from libnmap.process.NmapProcess import is_running [as 别名]
def do_nmap_scan(targets, options=NMAP_GLOBAL_OPTIONS):
# 运行次数初始化
trycnt = 0
while True:
# 运行时间初始化
runtime = 0
if trycnt >= NMAP_RETRYCNT:
print "-" * 50
return "retry overflow"
try:
nmap_proc = NmapProcess(targets=targets, options=options, safe_mode=False)
nmap_proc.run_background()
while nmap_proc.is_running():
if runtime >= NMAP_TIMEOUT: # 运行超时,结束掉任务,休息1分钟, 再重启这个nmap任务
print "-" * 50
print "* timeout. terminate it..."
nmap_proc.stop()
# 休眠时间
time.sleep(60)
trycnt += 1
break
else:
print "running[%ss]:%s" % (runtime, nmap_proc.command)
time.sleep(5)
runtime += 5
if nmap_proc.rc == 0:
return nmap_proc.stdout
except Exception, e:
# raise e
print e
trycnt += 1
if trycnt >= NMAP_RETRYCNT:
print "-" * 50
print "* retry overflow"
return e
示例8: ScanEngine
# 需要导入模块: from libnmap.process import NmapProcess [as 别名]
# 或者: from libnmap.process.NmapProcess import is_running [as 别名]
class ScanEngine(object):
def __init__(self,config):
self.engine = NmapProcess(config.targets, config.options)
def run(self):
"""Execute scan"""
syslog.syslog("Starting scan")
self.engine.run_background()
while self.engine.is_running():
time.sleep(15)
print "Scan is %s percent complete" % self.engine.progress
syslog.syslog("Completed scan")
def process(self):
"""parse and pre-process scan results"""
try:
self.parsed = NmapParser.parse(self.engine.stdout)
syslog.syslog("Processing output")
except Exception as e:
syslog.syslog("Failed to parse output"+e)
for h in self.parsed.hosts:
print h.address
示例9: NmapProcess
# 需要导入模块: from libnmap.process import NmapProcess [as 别名]
# 或者: from libnmap.process.NmapProcess import is_running [as 别名]
#!/usr/bin/env python
from libnmap.process import NmapProcess
from time import sleep
nmap_proc = NmapProcess(targets="scanme.nmap.org", options="-sT")
nmap_proc.run_background()
while nmap_proc.is_running():
nmaptask = nmap_proc.current_task
if nmaptask:
print "Task {0} ({1}): ETC: {2} DONE: {3}%".format(nmaptask.name,
nmaptask.status,
nmaptask.etc,
nmaptask.progress)
sleep(0.5)
print "rc: {0} output: {1}".format(nmap_proc.rc, nmap_proc.summary)
print nmap_proc.stdout
示例10: perform_scan
# 需要导入模块: from libnmap.process import NmapProcess [as 别名]
# 或者: from libnmap.process.NmapProcess import is_running [as 别名]
def perform_scan(config_file, section, targets, output_dir):
# merge targets to create the smallest possible list of CIDR subnets
subnets = list()
for target in targets:
subnets.append(IPNetwork(target))
subnets = cidr_merge(subnets)
targets = list()
for subnet in subnets:
targets.append(str(subnet))
# check if required args declaration is supplied
config = ConfigParser.ConfigParser()
config.read(config_file)
args = config_section_map(config, section)['args']
if not args:
error('No \'args\' declaration found in %s' % section)
exit()
# check for options that will interfere with this script
illegal_options = ['-oG', '-oN', '-iL', '-oA', '-oS', '-oX', '--iflist',
'--resume', '--stylesheet', '--datadir', '--stats-every']
for option in illegal_options:
if option in args:
error('\'args\' declaration contains incompatible option \'%s\'' %
(section, option))
exit()
# store raw nmap output as well
raw_file = '%s/%s.nmap' % (output_dir, section)
args += ' -oN %s' % raw_file
# perform scan
info('Starting scan \'%s\'' % section)
print(' -> %s' % config_section_map(config, section)['desc'])
nmap_proc = NmapProcess(targets=targets, options=args, safe_mode=False)
nmap_proc.sudo_run_background()
while nmap_proc.is_running():
nmap_task = nmap_proc.current_task
if nmap_task:
m, s = divmod(int(nmap_task.remaining), 60)
h, m = divmod(m, 60)
remaining = "%d:%02d:%02d" % (h, m, s)
sys.stdout.write(
'[+] Task: {0} - ETC: {1} DONE: {2}%'
' \r'.format(
nmap_task.name,
remaining,
nmap_task.progress))
sys.stdout.flush()
sleep(1)
# save results
if nmap_proc.rc == 0:
info('%s' % nmap_proc.summary.replace('Nmap done at',
'Scan completed on'))
run('chown %s:%s %s' % (os.getuid(), os.getgid(), raw_file), sudo=True)
print(" -> Nmap report saved to '%s'" % raw_file)
if nmap_proc.stdout:
xml_file = '%s/%s.xml' % (output_dir, section)
out = open(xml_file, 'w')
out.write(nmap_proc.stdout)
out.close()
print(" -> XML report saved to '%s'" % xml_file)
#if nmap_proc.stderr:
# err_file = '%s/%s.err' % (output_dir, section)
# out = open(err_file, 'w')
# out.write(nmap_proc.stderr)
# out.close()
# print(" -> Standard error output saved to '%s'" %
# err_file)
else:
error('Error occurred:')
print(' -> %s' % nmap_proc.stderr.rstrip())