def _call_nmap_single(maincategory, options, accept_target_status, target): target = ivre.utils.int2ip(target) outfile = 'scans/%s/%%s/%s.xml' % (maincategory, target.replace('.', '/')) if STATUS_DONE_UP not in accept_target_status: try: os.stat(outfile % 'up') return except OSError: pass if STATUS_DONE_DOWN not in accept_target_status: try: os.stat(outfile % 'down') return except OSError: pass if STATUS_DONE_UNKNOWN not in accept_target_status: try: os.stat(outfile % 'unknown') return except OSError: pass ivre.utils.makedirs(os.path.dirname(outfile % 'current')) subprocess.call(options + ['-oX', outfile % 'current', target], preexec_fn=setnmaplimits) resdata = open(outfile % 'current').read() if '<status state="up"' in resdata: outdir = 'up' elif '<status state="down"' in resdata: outdir = 'down' else: outdir = 'unknown' ivre.utils.makedirs(os.path.dirname(outfile % outdir)) shutil.move(outfile % 'current', outfile % outdir)
def _call_nmap_single(maincategory, options, accept_target_status, target): target = ivre.utils.int2ip(target) outfile = 'scans/%s/%%s/%s.xml' % (maincategory, target.replace('.', '/')) if STATUS_DONE_UP not in accept_target_status: try: os.stat(outfile % 'up') return except OSError: pass if STATUS_DONE_DOWN not in accept_target_status: try: os.stat(outfile % 'down') return except OSError: pass if STATUS_DONE_UNKNOWN not in accept_target_status: try: os.stat(outfile % 'unknown') return except OSError: pass ivre.utils.makedirs(os.path.dirname(outfile % 'current')) subprocess.call(options + ['-oX', outfile % 'current', target], preexec_fn=setnmaplimits) resdata = open(outfile % 'current', 'rb').read() if b'<status state="up"' in resdata: outdir = 'up' elif b'<status state="down"' in resdata: outdir = 'down' else: outdir = 'unknown' ivre.utils.makedirs(os.path.dirname(outfile % outdir)) shutil.move(outfile % 'current', outfile % outdir)
def _call_nmap_single(maincategory, options, accept_target_status, target): target = ivre.utils.int2ip(target) outfile = "scans/%s/%%s/%s.xml" % (maincategory, target.replace(".", "/")) if STATUS_DONE_UP not in accept_target_status: try: os.stat(outfile % "up") return except OSError: pass if STATUS_DONE_DOWN not in accept_target_status: try: os.stat(outfile % "down") return except OSError: pass if STATUS_DONE_UNKNOWN not in accept_target_status: try: os.stat(outfile % "unknown") return except OSError: pass ivre.utils.makedirs(os.path.dirname(outfile % "current")) subprocess.call(options + ["-oX", outfile % "current", target], preexec_fn=setnmaplimits) resdata = open(outfile % "current", "rb").read() if b'<status state="up"' in resdata: outdir = "up" elif b'<status state="down"' in resdata: outdir = "down" else: outdir = "unknown" ivre.utils.makedirs(os.path.dirname(outfile % outdir)) shutil.move(outfile % "current", outfile % outdir)
def target_status(self, target): for status, statuscode in viewitems(self.status_paths): try: os.stat(os.path.join(self.path, status, target.replace('.', '/') + '.xml')) return statuscode except OSError: pass return STATUS_NEW
def target_status(self, target): for status, statuscode in self.status_paths.iteritems(): try: os.stat(os.path.join(self.path, status, target.replace('.', '/') + '.xml')) return statuscode except OSError: pass return STATUS_NEW
def target_status(self, target: str) -> int: for status, statuscode in self.status_paths.items(): try: os.stat( os.path.join(self.path, status, target.replace(".", "/") + ".xml") ) return statuscode except OSError: pass return STATUS_NEW
def _call_nmap_single(maincategory, options, accept_target_status, target): target = ivre.utils.int2ip(target) outfile = 'scans/%s/%%s/%s.xml' % (maincategory, target.replace('.', '/')) # 根据accept_target_status状态策略决定是否覆盖继续扫描并覆盖已有结果文件 # accept_target_status默认为set(STATUS_NEW), 即不覆盖任何已有结果文件 if STATUS_DONE_UP not in accept_target_status: try: # 这里使用os.stat 函数检查文件是否存在 # os.path中检查文件或文件夹是否存在的系列函数(isfile, isdir, exists等) # 里面都是调用os.stat以及结合其他属性来判断的 os.stat(outfile % 'up') return except OSError: pass if STATUS_DONE_DOWN not in accept_target_status: try: os.stat(outfile % 'down') return except OSError: pass if STATUS_DONE_UNKNOWN not in accept_target_status: try: os.stat(outfile % 'unknown') return except OSError: pass ivre.utils.makedirs(os.path.dirname(outfile % 'current')) # 命令行调用, 结果保存在outfile =================== # 对比 call_nmap函数使用Popen函数,这里是直接命令行存本地文件,无需使用Popen subprocess.call(options + ['-oX', outfile % 'current', target], preexec_fn=setnmaplimits) # 根据返回的扫描结果状态移动到相应路径 resdata = open(outfile % 'current', 'rb').read() if b'<status state="up"' in resdata: outdir = 'up' elif b'<status state="down"' in resdata: outdir = 'down' else: outdir = 'unknown' ivre.utils.makedirs(os.path.dirname(outfile % outdir)) shutil.move(outfile % 'current', outfile % outdir)