"Fossies" - the Fresh Open Source Software Archive

Member "gvm-tools-21.6.1/gvmtools/cli.py" (21 Jun 2021, 4328 Bytes) of package /linux/misc/openvas/gvm-tools-21.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # -*- coding: utf-8 -*-
    2 # Copyright (C) 2018-2021 Greenbone Networks GmbH
    3 #
    4 # SPDX-License-Identifier: GPL-3.0-or-later
    5 #
    6 # This program is free software: you can redistribute it and/or modify
    7 # it under the terms of the GNU General Public License as published by
    8 # the Free Software Foundation, either version 3 of the License, or
    9 # (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
   18 
   19 import logging
   20 import sys
   21 import time
   22 
   23 from gvm.errors import GvmError
   24 from gvm.protocols.latest import Osp
   25 from gvm.protocols.gmp import Gmp
   26 from gvm.transforms import CheckCommandTransform
   27 from gvm.xml import validate_xml_string, pretty_print
   28 
   29 from gvmtools.helper import authenticate, do_not_run_as_root
   30 from gvmtools.parser import (
   31     create_parser,
   32     create_connection,
   33     PROTOCOL_OSP,
   34     PROTOCOL_GMP,
   35 )
   36 
   37 logger = logging.getLogger(__name__)
   38 
   39 HELP_TEXT = """
   40     Command line tool to access services via GMP (Greenbone Management Protocol) and OSP (Open Scanner Protocol)
   41 
   42     Examples:
   43       gvm-cli socket --help
   44       gvm-cli tls --help
   45       gvm-cli ssh --help
   46 
   47       gvm-cli socket --xml "<get_version/>"
   48       gvm-cli socket --xml "<commands><authenticate><credentials><username>myuser</username><password>mypass</password></credentials></authenticate><get_tasks/></commands>"
   49       gvm-cli socket --gmp-username foo --gmp-password foo myfile.xml
   50 
   51     The protocol specifications for GMP and OSP are available at:
   52       https://docs.greenbone.net/index.html#api_documentation"""
   53 
   54 
   55 def _load_infile(filename=None):
   56     if not filename:
   57         return None
   58 
   59     with open(filename) as f:
   60         return f.read()
   61 
   62 
   63 def main():
   64     do_not_run_as_root()
   65 
   66     parser = create_parser(description=HELP_TEXT, logfilename='gvm-cli.log')
   67 
   68     parser.add_protocol_argument()
   69 
   70     parser.add_argument('-X', '--xml', help='XML request to send')
   71     parser.add_argument(
   72         '-r', '--raw', help='Return raw XML', action='store_true', default=False
   73     )
   74     parser.add_argument(
   75         '--pretty',
   76         help='Pretty format the returned xml',
   77         action='store_true',
   78         default=False,
   79     )
   80     parser.add_argument(
   81         '--duration', action='store_true', help='Measure command execution time'
   82     )
   83     parser.add_argument(
   84         'infile', nargs='?', help='File to read XML commands from.'
   85     )
   86 
   87     args = parser.parse_args()
   88 
   89     # If timeout value is -1, then the socket has no timeout for this session
   90     if args.timeout == -1:
   91         args.timeout = None
   92 
   93     if args.xml is not None:
   94         xml = args.xml
   95     else:
   96         try:
   97             xml = _load_infile(args.infile)
   98         except IOError as e:
   99             print(e, file=sys.stderr)
  100             sys.exit(1)
  101 
  102     # If no command was given, program asks for one
  103     if len(xml) == 0:
  104         xml = input()
  105 
  106     try:
  107         validate_xml_string(xml)
  108     except GvmError as e:
  109         print(e, file=sys.stderr)
  110         sys.exit(1)
  111 
  112     connection = create_connection(**vars(args))
  113 
  114     if args.raw:
  115         transform = None
  116     else:
  117         transform = CheckCommandTransform()
  118 
  119     if args.protocol == PROTOCOL_OSP:
  120         protocol_class = Osp
  121     else:
  122         protocol_class = Gmp
  123 
  124     try:
  125         with protocol_class(connection, transform=transform) as protocol:
  126 
  127             if args.protocol == PROTOCOL_GMP:
  128                 # Ask for password if none are given
  129                 authenticate(protocol, args.gmp_username, args.gmp_password)
  130 
  131             if args.duration:
  132                 starttime = time.time()
  133 
  134             result = protocol.send_command(xml)
  135 
  136             if args.duration:
  137                 duration = time.time() - starttime
  138                 print(f'Elapsed time: {duration} seconds')
  139             elif args.pretty:
  140                 pretty_print(result)
  141             else:
  142                 print(result)
  143 
  144     except Exception as e:  # pylint: disable=broad-except
  145         logger.error(e)
  146         sys.exit(1)
  147     sys.exit(0)
  148 
  149 
  150 if __name__ == '__main__':
  151     main()