Ejemplo n.º 1
0
def main():
    parser = create_parser(description=HELP_TEXT, logfilename='gvm-script.log')

    parser.add_protocol_argument()

    parser.add_argument(
        'scriptname',
        metavar="SCRIPT",
        help='Path to script to be executed (example: myscript.gmp)',
    )
    parser.add_argument('scriptargs',
                        nargs='*',
                        metavar="ARG",
                        help='Arguments for the script')

    args = parser.parse_args()

    if 'socket' in args.connection_type and args.sockpath:
        print(
            'The --sockpath parameter has been deprecated. Please use '
            '--socketpath instead',
            file=sys.stderr,
        )

    connection = create_connection(**vars(args))

    transform = EtreeCheckCommandTransform()

    global_vars = {
        '__version__': __version__,
        '__api_version__': __api_version__,
    }

    username = None
    password = None

    if args.protocol == PROTOCOL_OSP:
        protocol = Osp(connection, transform=transform)
        global_vars['osp'] = protocol
        global_vars['__name__'] = '__osp__'
    else:
        protocol = Gmp(connection, transform=transform)
        global_vars['gmp'] = protocol
        global_vars['__name__'] = '__gmp__'

        if args.gmp_username:
            (username, password) = authenticate(protocol,
                                                username=args.gmp_username,
                                                password=args.gmp_password)

    argv = [os.path.abspath(args.scriptname), *args.scriptargs]

    shell_args = Namespace(
        username=username,
        password=password,
        argv=argv,
        # for backwards compatibility we add script here
        script=argv,
    )

    global_vars['args'] = shell_args

    run_script(args.scriptname, global_vars)
Ejemplo n.º 2
0
def main():
    parser = create_parser(description=HELP_TEXT,
                           logfilename='gvm-pyshell.log')

    parser.add_protocol_argument()

    parser.add_argument('-i',
                        '--interactive',
                        action='store_true',
                        default=False,
                        help='Start an interactive Python shell')

    parser.add_argument(
        'scriptname',
        nargs='?',
        metavar="SCRIPT",
        help='Path to script to be preloaded (example: myscript.gmp)')
    parser.add_argument('scriptargs',
                        nargs='*',
                        metavar="ARG",
                        help='Arguments for preloaded script')

    args = parser.parse_args()

    if 'socket' in args.connection_type and args.sockpath:
        print(
            'The --sockpath parameter has been deprecated. Please use '
            '--socketpath instead',
            file=sys.stderr)

    connection = create_connection(**vars(args))

    transform = EtreeCheckCommandTransform()

    global_vars = {
        'help': Help(),
        '__version__': __version__,
        '__api_version__': __api_version__,
    }

    username = None
    password = None

    if args.protocol == PROTOCOL_OSP:
        protocol = Osp(connection, transform=transform)
        global_vars['osp'] = protocol
        global_vars['__name__'] = '__osp__'
    else:
        protocol = Gmp(connection, transform=transform)
        global_vars['gmp'] = protocol
        global_vars['__name__'] = '__gmp__'

        if args.gmp_username:
            (username, password) = authenticate(protocol,
                                                username=args.gmp_username,
                                                password=args.gmp_password)

    shell_args = Arguments(username=username, password=password)

    global_vars['args'] = shell_args

    with_script = args.scriptname and len(args.scriptname) > 0

    if with_script:
        argv = [os.path.abspath(args.scriptname), *args.scriptargs]
        shell_args.argv = argv
        # for backwards compatibility we add script here
        shell_args.script = argv

    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 only_interactive or no_script_no_interactive:
        enter_interactive_mode(global_vars)

    if script_and_interactive or only_script:
        script_name = args.scriptname
        load(script_name, global_vars)

        if not only_script:
            enter_interactive_mode(global_vars)

    protocol.disconnect()
Ejemplo n.º 3
0
 def test_osp_version(self):
     self.assertEqual(Osp.get_protocol_version(), '1.2')
Ejemplo n.º 4
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='?', 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()

    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)
        sys.exit(1)

    protocol.disconnect()

    sys.exit(0)