예제 #1
0
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()
예제 #2
0
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)