diff --git a/examples/example_legacy.py b/examples/example_legacy.py index 219d43b..1f85f04 100644 --- a/examples/example_legacy.py +++ b/examples/example_legacy.py @@ -1,42 +1,45 @@ -#!/usr/bin/env python +# #!/usr/bin/env python -from __future__ import print_function + +import base64 +import os +import sys +import argparse from openvas_lib import VulnscanManager, VulnscanException from threading import Semaphore from functools import partial from xml.etree import ElementTree -import base64 -import os,sys -import argparse + def my_print_status(i): print(str(i)), sys.stdout.flush() -def write_report(manager, report_id, ip): + +def write_report(_manager, report_id, ip): result_dir = os.path.dirname(os.path.abspath(__file__)) + "/results" if not os.path.exists(result_dir): os.makedirs(result_dir) try: - report = manager.get_report_xml(report_id) + report = _manager.get_report_xml(report_id) except Exception as e: - print(e) + print(str(e)) return else: fout_path = result_dir + "/xml/" if not os.path.exists(fout_path): os.makedirs(fout_path) - + fout = open(fout_path + ip + ".xml", "wb") fout.write(ElementTree.tostring(report, encoding='utf-8', method='xml')) fout.close() try: - report = manager.get_report_html(report_id) + report = _manager.get_report_html(report_id) except Exception as e: - print(e) + print(str(e)) return else: fout_path = result_dir + "/html/" @@ -51,42 +54,38 @@ def write_report(manager, report_id, ip): fout.write(base64.b64decode(html_text)) fout.close() -def run(manager, ip): - Sem = Semaphore(0) - scan_id, target_id = manager.launch_scan( - target=ip, - profile="Full and fast", - callback_end=partial(lambda x: x.release(), Sem), + +def run(_manager, _target, _profile): + sem = Semaphore(0) + scan_id, target_id = _manager.launch_scan( + target=_target, + profile=_profile, + callback_end=partial(lambda x: x.release(), sem), callback_progress=my_print_status ) - Sem.acquire() - report_id = manager.get_report_id(scan_id) + sem.acquire() + report_id = _manager.get_report_id(scan_id) + write_report(_manager, report_id, _target) + _manager.delete_scan(scan_id) + _manager.delete_target(target_id) - write_report(manager, report_id, ip) - manager.delete_scan(scan_id) - manager.delete_target(target_id) -if __name__ == '__main__': +def main(ip, user, password, target, profile): + try: + manager = VulnscanManager(ip, user, password) + except VulnscanException as e: + print(str(e)) + else: + run(manager, target, profile) + +if __name__ == '__main__': parser = argparse.ArgumentParser(description='Features Selection') parser.add_argument('-u', '--user', required=True, help='OpenVas user') parser.add_argument('-p', '--password', required=True, help='OpenVas password') parser.add_argument('-i', '--ip', required=True, help='OpenVas ip host') parser.add_argument('-t', '--target', required=True, help='Host target') - + parser.add_argument('--profile', required=True, help='OpenVas profile, ex: --profile="Full and fast"') args = parser.parse_args() - if args.user: - admin_name = args.user - if args.user: - admin_password = args.password - if args.ip: - openvas_ip = args.ip - if args.target: - ip = args.target - - try: - manager = VulnscanManager(openvas_ip, admin_name, admin_password) - run(manager, ip) - except Exception as e: - print(e) + print(args.ip, args.user, args.password, args.target, args.profile) diff --git a/openvas_lib/__init__.py b/openvas_lib/__init__.py index da812ac..1de6664 100644 --- a/openvas_lib/__init__.py +++ b/openvas_lib/__init__.py @@ -106,7 +106,10 @@ def report_parser(path_or_file, ignore_log_info=True): This functions transform XML OpenVas file report to OpenVASResult object structure. To pass StringIO file as parameter, you must do that: - >>> import StringIO + >>> try: + >>> import StringIO + >>> except ImportError: + >>> from io import StringIO >>> xml='' >>> f=StringIO.StringIO(xml) >>> report_parser(f)