def connection_with_unix_socket(xml, args): gvm = UnixSocketConnection(sockpath=args.sockpath, timeout=args.timeout) gvm.authenticate(args.gmp_username, args.gmp_password) gvm.send(xml) result = gvm.read() print(result) gvm.close()
def main(): parser = argparse.ArgumentParser(prog='gvm-cli', description=help_text, formatter_class=RawTextHelpFormatter, add_help=False, epilog=""" usage: gvm-cli [-h] [--version] [connection_type] ... or: gvm-cli connection_type --help""") subparsers = parser.add_subparsers(metavar='[connection_type]') subparsers.required = True subparsers.dest = 'connection_type' parser.add_argument('-h', '--help', action='help', help='Show this help message and exit.') parent_parser = argparse.ArgumentParser(add_help=False) parent_parser.add_argument( '-c', '--config', nargs='?', const='~/.config/gvm-tools.conf', help='Configuration file path. Default: ~/.config/gvm-tools.conf') args, remaining_args = parent_parser.parse_known_args() defaults = {'gmp_username': '', 'gmp_password': ''} # Retrieve data from config file if args.config: try: config = configparser.SafeConfigParser() path = os.path.expanduser(args.config) config.read(path) defaults = dict(config.items('Auth')) except Exception as e: print(str(e)) parent_parser.set_defaults(**defaults) parent_parser.add_argument( '--timeout', required=False, default=60, type=int, help='Wait <seconds> for response or if value -1, then wait ' 'continuously. Default: 60') parent_parser.add_argument( '--log', nargs='?', dest='loglevel', const='INFO', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], help='Activates logging. Default level: INFO.') parent_parser.add_argument('--gmp-username', help='GMP username.') parent_parser.add_argument('--gmp-password', help='GMP password.') parent_parser.add_argument('-X', '--xml', help='The XML request to send.') parent_parser.add_argument('infile', nargs='?', type=open, default=sys.stdin) parser_ssh = subparsers.add_parser( 'ssh', help='Use SSH connection for gmp service.', parents=[parent_parser]) parser_ssh.add_argument('--hostname', required=True, help='Hostname or IP-Address.') parser_ssh.add_argument('--port', required=False, default=22, help='Port. Default: 22.') parser_ssh.add_argument('--ssh-user', default='gmp', help='SSH Username. Default: gmp.') parser_tls = subparsers.add_parser( 'tls', help='Use TLS secured connection for gmp service.', parents=[parent_parser]) parser_tls.add_argument('--hostname', required=True, help='Hostname or IP-Address.') parser_tls.add_argument('--port', required=False, default=9390, help='Port. Default: 9390.') parser_socket = subparsers.add_parser( 'socket', help='Use UNIX-Socket connection for gmp service.', parents=[parent_parser]) parser_socket.add_argument( '--sockpath', nargs='?', default='/usr/local/var/run/gvmd.sock', help='UNIX-Socket path. Default: /usr/local/var/run/gvmd.sock.') parser.add_argument( '-V', '--version', action='version', version='%(prog)s {version}'.format(version=__version__), help='Show program\'s version number and exit') args = parser.parse_args(remaining_args) # Sets the logging if args.loglevel is not None: level = logging.getLevelName(args.loglevel) logging.basicConfig(filename='gvm-cli.log', level=level) # If timeout value is -1, then the socket has no timeout for this session if args.timeout == -1: args.timeout = None xml = '' if args.xml is not None: xml = args.xml else: # If this returns False, then some data are in sys.stdin if not args.infile.isatty(): try: xml = args.infile.read() except (EOFError, BlockingIOError) as e: print(e) # If no command was given, program asks for one if len(xml) == 0: xml = input() # Remove all newlines if the commands come from file xml = xml.replace('\n', '').replace('\r', '') # Ask for password if none are given if args.gmp_username and not args.gmp_password: args.gmp_password = getpass.getpass('Enter password for ' + args.gmp_username + ': ') # Open the right connection. SSH at last for default try: if 'socket' in args.connection_type: gvm = UnixSocketConnection(sockpath=args.sockpath, timeout=args.timeout) elif 'tls' in args.connection_type: gvm = TLSConnection(hostname=args.hostname, port=9390, timeout=args.timeout) else: gvm = SSHConnection(hostname=args.hostname, port=args.port, timeout=args.timeout, ssh_user=args.ssh_user, ssh_password='') except Exception as e: print(e) sys.exit(1) if args.gmp_username: gvm.authenticate(args.gmp_username, args.gmp_password) gvm.send(xml) result = gvm.read() print(result) gvm.close() sys.exit(0)
def main(): parser = argparse.ArgumentParser(prog='gvm-dialog', add_help=False, epilog=""" usage: gvm-dialog [-h] [--version] [connection_type] ... or: gvm-dialog connection_type --help""") subparsers = parser.add_subparsers(metavar='[connection_type]') subparsers.required = True subparsers.dest = 'connection_type' parser.add_argument('-h', '--help', action='help', help='Show this help message and exit.') parent_parser = argparse.ArgumentParser(add_help=False) parent_parser.add_argument( '--timeout', required=False, default=60, type=int, help='Wait <seconds> for response or if value -1, then wait ' 'continuously. Default: 60') parent_parser.add_argument( '--log', nargs='?', dest='loglevel', const='INFO', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], help='Activates logging. Default level: INFO.') parent_parser.add_argument('-i', '--interactive', action='store_true', default=False, help='Start an interactive Python shell.') parent_parser.add_argument('--gmp-username', help='GMP username.') parent_parser.add_argument('--gmp-password', help='GMP password.') parser_ssh = subparsers.add_parser( 'ssh', help='Use SSH connection for gmp service.', parents=[parent_parser]) parser_ssh.add_argument('--hostname', required=True, help='Hostname or IP-Address.') parser_ssh.add_argument('--port', required=False, default=22, help='Port. Default: 22.') parser_ssh.add_argument('--ssh-user', default='gmp', help='SSH Username. Default: gmp.') parser_tls = subparsers.add_parser( 'tls', help='Use TLS secured connection for gmp service.', parents=[parent_parser]) parser_tls.add_argument('--hostname', required=True, help='Hostname or IP-Address.') parser_tls.add_argument('--port', required=False, default=9390, help='Port. Default: 9390.') parser_socket = subparsers.add_parser( 'socket', help='Use UNIX-Socket connection for gmp service.', parents=[parent_parser]) parser_socket.add_argument( '--sockpath', nargs='?', default='/usr/local/var/run/gvmd.sock', help='UNIX-Socket path. Default: /usr/local/var/run/gvmd.sock.') parser.add_argument( '-V', '--version', action='version', version='%(prog)s {version}'.format(version=__version__), help='Show program\'s version number and exit') args = parser.parse_args() # Sets the logging if args.loglevel is not None: level = logging.getLevelName(args.loglevel) logging.basicConfig(filename='gvm-dialog.log', level=level) # If timeout value is -1, then the socket has no timeout for this session if args.timeout == -1: args.timeout = None # Open the right connection. SSH at last for default global gmp if 'socket' in args.connection_type: try: gmp = UnixSocketConnection(sockpath=args.sockpath, shell_mode=True, timeout=args.timeout) except OSError as e: print('{0}: {1}'.format(e, args.sockpath)) sys.exit(1) elif 'tls' in args.connection_type: try: gmp = TLSConnection(hostname=args.hostname, port=args.port, timeout=args.timeout, shell_mode=True) except OSError as e: print('{0}: Host: {1} Port: {2}'.format(e, args.hostname, args.port)) sys.exit(1) else: try: gmp = SSHConnection(hostname=args.hostname, port=args.port, timeout=args.timeout, ssh_user=args.ssh_user, ssh_password='', shell_mode=True) except Exception as e: print('{0}: Host: {1} Port: {2}'.format(e, args.hostname, args.port)) sys.exit(1) d = Dialog(dialog="dialog") # Dialog.set_background_title() requires pythondialog 2.13 or later d.set_background_title("gvm-dialog") code, credentials = d.mixedform( 'Please enter credentials:', [ # ('Hostname', 1, 1, '127.0.0.1', 1, 20, 15, 32, 0), ('Username', 1, 1, '', 1, 20, 15, 32, 0), ('Password', 2, 1, '', 2, 20, 15, 32, 1) ], insecure=True) gmp.authenticate(credentials[0], credentials[1]) code, tag = d.menu("What do you want to do?", choices=[("(1)", "Show Tasks"), ("(2)", "Exit")]) if code == d.OK: # 'tag' is now either "(1)" or "(2)" if tag in '(1)': task_list = gmp.get_tasks() names = [] tasks = dict() for task in task_list.xpath('task'): name = task.xpath('name/text()')[0] names.append((name, '')) tasks[name] = etree.tostring(task, pretty_print=True).decode('utf-8') while (True): code, tag = d.menu("Tasks", choices=names) if 'cancel' in code: break d.msgbox(tasks[tag], 60, 90) else: pass gmp.close()
def main(): parser = argparse.ArgumentParser(prog='gvm-pyshell', description=help_text, formatter_class=RawTextHelpFormatter, add_help=False, epilog=""" usage: gvm-pyshell [-h] [--version] [connection_type] ... or: gvm-pyshell connection_type --help""") subparsers = parser.add_subparsers(metavar='[connection_type]') subparsers.required = True subparsers.dest = 'connection_type' parser.add_argument('-h', '--help', action='help', help='Show this help message and exit.') parent_parser = argparse.ArgumentParser(add_help=False) parent_parser.add_argument( '-c', '--config', nargs='?', const='~/.config/gvm-tools.conf', help='Configuration file path. Default: ~/.config/gvm-tools.conf') args_before, remaining_args = parent_parser.parse_known_args() defaults = {'gmp_username': '', 'gmp_password': ''} # Retrieve data from config file if args_before.config: try: config = configparser.SafeConfigParser() path = os.path.expanduser(args_before.config) config.read(path) defaults = dict(config.items('Auth')) except Exception as e: print(str(e)) parent_parser.set_defaults(**defaults) parent_parser.add_argument( '--timeout', required=False, default=60, type=int, help='Wait <seconds> for response or if value -1, then wait ' 'continuously. Default: 60') parent_parser.add_argument( '--log', nargs='?', dest='loglevel', const='INFO', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], help='Activates logging. Default level: INFO.') parent_parser.add_argument('-i', '--interactive', action='store_true', default=False, help='Start an interactive Python shell.') parent_parser.add_argument('--gmp-username', help='GMP username.') parent_parser.add_argument('--gmp-password', help='GMP password.') parent_parser.add_argument( 'script', nargs='*', help='Preload gmp script. Example: myscript.gmp.') parser_ssh = subparsers.add_parser( 'ssh', help='Use SSH connection for gmp service.', parents=[parent_parser]) parser_ssh.add_argument('--hostname', required=True, help='Hostname or IP-Address.') parser_ssh.add_argument('--port', required=False, default=22, help='Port. Default: 22.') parser_ssh.add_argument('--ssh-user', default='gmp', help='SSH Username. Default: gmp.') parser_tls = subparsers.add_parser( 'tls', help='Use TLS secured connection for gmp service.', parents=[parent_parser]) parser_tls.add_argument('--hostname', required=True, help='Hostname or IP-Address.') parser_tls.add_argument('--port', required=False, default=9390, help='Port. Default: 9390.') parser_socket = subparsers.add_parser( 'socket', help='Use UNIX-Socket connection for gmp service.', parents=[parent_parser]) parser_socket.add_argument( '--sockpath', nargs='?', default='/usr/local/var/run/gvmd.sock', help='UNIX-Socket path. Default: /usr/local/var/run/gvmd.sock.') parser.add_argument( '-V', '--version', action='version', version='%(prog)s {version}'.format(version=__version__), help='Show program\'s version number and exit') global args args = parser.parse_args(remaining_args) # Sets the logging if args.loglevel is not None: level = logging.getLevelName(args.loglevel) logging.basicConfig(filename='gvm-pyshell.log', level=level) # If timeout value is -1, then the socket has no timeout for this session if args.timeout == -1: args.timeout = None # Open the right connection. SSH at last for default global gmp if 'socket' in args.connection_type: try: gmp = UnixSocketConnection(sockpath=args.sockpath, shell_mode=True, timeout=args.timeout) except OSError as e: print('{0}: {1}'.format(e, args.sockpath)) sys.exit(1) elif 'tls' in args.connection_type: try: gmp = TLSConnection(hostname=args.hostname, port=args.port, timeout=args.timeout, shell_mode=True) except OSError as e: print('{0}: Host: {1} Port: {2}'.format(e, args.hostname, args.port)) sys.exit(1) else: try: gmp = SSHConnection(hostname=args.hostname, port=args.port, timeout=args.timeout, ssh_user=args.ssh_user, ssh_password='', shell_mode=True) except Exception as e: print('{0}: Host: {1} Port: {2}'.format(e, args.hostname, args.port)) sys.exit(1) # Ask for login credentials if none are given if not args.gmp_username: while True: args.gmp_username = input('Enter username: '******'Enter password for ' + args.gmp_username + ': ') try: gmp.authenticate(args.gmp_username, args.gmp_password) except Exception as e: print('Please check your credentials!') print(e) sys.exit(1) with_script = args.script and len(args.script) > 0 no_script_no_interactive = not args.interactive and not with_script script_and_interactive = args.interactive and with_script only_interactive = not with_script and args.interactive only_script = not args.interactive and with_script if no_script_no_interactive: enterInteractiveMode() if only_interactive: enterInteractiveMode() if script_and_interactive: load(args.script[0]) enterInteractiveMode() if only_script: load(args.script[0]) gmp.close()
def testUnixSocketConnection(self): gmp = UnixSocketConnection(sockpath=self.sockpath, shell_mode=False) version = gmp.get_version() gmp.close() self.assertEqual(version, self.answer_ok)