def test_success_response(self):
        transform = CheckCommandTransform()

        root = etree.Element("foo_response")
        root.set("status", "200")

        response = etree.tostring(root).decode("utf-8")

        self.assertEqual(transform(response), '<foo_response status="200"/>')
    def test_success_response(self):
        transform = CheckCommandTransform()

        root = etree.Element('foo_response')
        root.set('status', '200')

        response = etree.tostring(root).decode('utf-8')

        self.assertEqual(transform(response), '<foo_response status="200"/>')
    def test_no_success_500status_transform(self):
        transform = CheckCommandTransform()

        root = etree.Element('foo_response')
        root.set('status', '500')
        root.set('status_text', 'Foo error')

        response = etree.tostring(root).decode('utf-8')

        with self.assertRaises(GvmServerError):
            transform(response)
    def test_no_success_500status_transform(self):
        transform = CheckCommandTransform()

        root = etree.Element("foo_response")
        root.set("status", "500")
        root.set("status_text", "Foo error")

        response = etree.tostring(root).decode("utf-8")

        with self.assertRaises(GvmServerError):
            transform(response)
Пример #5
0
def main():
    do_not_run_as_root()

    parser = create_parser(description=HELP_TEXT, logfilename='gvm-cli.log')

    parser.add_protocol_argument()

    parser.add_argument('-X', '--xml', help='XML request to send')
    parser.add_argument('-r',
                        '--raw',
                        help='Return raw XML',
                        action='store_true',
                        default=False)
    parser.add_argument(
        '--pretty',
        help='Pretty format the returned xml',
        action='store_true',
        default=False,
    )
    parser.add_argument('--duration',
                        action='store_true',
                        help='Measure command execution time')
    parser.add_argument('infile',
                        nargs='?',
                        help='File to read XML commands from.')

    args = parser.parse_args()

    # If timeout value is -1, then the socket has no timeout for this session
    if args.timeout == -1:
        args.timeout = None

    if args.xml is not None:
        xml = args.xml
    else:
        try:
            xml = _load_infile(args.infile)
        except IOError as e:
            print(e, file=sys.stderr)
            sys.exit(1)

    # If no command was given, program asks for one
    if len(xml) == 0:
        xml = input()

    try:
        validate_xml_string(xml)
    except GvmError as e:
        print(e, file=sys.stderr)
        sys.exit(1)

    connection = create_connection(**vars(args))

    if args.raw:
        transform = None
    else:
        transform = CheckCommandTransform()

    if args.protocol == PROTOCOL_OSP:
        protocol_class = Osp
    else:
        protocol_class = Gmp

    try:
        with protocol_class(connection, transform=transform) as protocol:

            if args.protocol == PROTOCOL_GMP:
                # Ask for password if none are given
                authenticate(protocol, args.gmp_username, args.gmp_password)

            if args.duration:
                starttime = time.time()

            result = protocol.send_command(xml)

            if args.duration:
                duration = time.time() - starttime
                print(f'Elapsed time: {duration} seconds')
            elif args.pretty:
                pretty_print(result)
            else:
                print(result)

    except Exception as e:  # pylint: disable=broad-except
        logger.error(e)
        sys.exit(1)
    sys.exit(0)
    def test_no_status_transform(self):
        transform = CheckCommandTransform()

        with self.assertRaises(GvmError):
            transform('<foo/')
Пример #7
0
def main():
    parser = create_parser(description=HELP_TEXT, logfilename='gvm-cli.log')

    parser.add_argument('-X', '--xml', help='XML request to send')
    parser.add_argument('-r',
                        '--raw',
                        help='Return raw XML',
                        action='store_true',
                        default=False)
    parser.add_argument('infile', nargs='?', type=open, default=sys.stdin)

    args = parser.parse_args()

    # 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()

    # 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 + ': ')

    connection = create_connection(**vars(args))

    if args.raw:
        transform = None
    else:
        transform = CheckCommandTransform()

    gvm = Gmp(connection, transform=transform)

    if args.gmp_username:
        gvm.authenticate(args.gmp_username, args.gmp_password)

    try:
        result = gvm.send_command(xml)

        print(result)
    except Exception as e:  # pylint: disable=broad-except
        print(e)
        sys.exit(1)

    gvm.disconnect()

    sys.exit(0)
Пример #8
0
def main():
    do_not_run_as_root()

    parser = create_parser(description=HELP_TEXT, logfilename='gvm-cli.log')

    parser.add_protocol_argument()

    parser.add_argument('-X', '--xml', help='XML request to send')
    parser.add_argument('-r',
                        '--raw',
                        help='Return raw XML',
                        action='store_true',
                        default=False)
    parser.add_argument('infile',
                        nargs='?',
                        help='File to read XML commands from.')

    args = parser.parse_args()

    # If timeout value is -1, then the socket has no timeout for this session
    if args.timeout == -1:
        args.timeout = None

    if args.xml is not None:
        xml = args.xml
    else:
        try:
            xml = _load_infile(args.infile)
        except IOError as e:
            print(e, file=sys.stderr)
            sys.exit(1)

    # If no command was given, program asks for one
    if len(xml) == 0:
        xml = input()

    try:
        validate_xml_string(xml)
    except GvmError as e:
        print(e, file=sys.stderr)
        sys.exit(1)

    connection = create_connection(**vars(args))

    if args.raw:
        transform = None
    else:
        transform = CheckCommandTransform()

    if args.protocol == PROTOCOL_OSP:
        protocol = Osp(connection, transform=transform)
    else:
        protocol = Gmp(connection, transform=transform)

        # 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 + ': ')

        if args.gmp_username:
            protocol.authenticate(args.gmp_username, args.gmp_password)

    try:
        result = protocol.send_command(xml)

        print(result)
    except Exception as e:  # pylint: disable=broad-except
        print(e, file=sys.stderr)
        sys.exit(1)

    protocol.disconnect()

    sys.exit(0)