def execute(self, argv): try: opts, _ = getopt.gnu_getopt( argv, 'i:v', ['instance=', 'verbose', 'debug', 'help']) except getopt.GetoptError as e: logger.error(e) self.usage() sys.exit(1) instance_name = 'pki-tomcat' for o, a in opts: if o in ('-i', '--instance'): instance_name = a elif o == '--debug': logging.getLogger().setLevel(logging.DEBUG) elif o in ('-v', '--verbose'): logging.getLogger().setLevel(logging.INFO) elif o == '--help': self.usage() sys.exit() else: logger.error('Unknown option: %s', o) self.usage() sys.exit(1) instance = pki.server.instance.PKIServerFactory.create(instance_name) if not instance.exists(): logger.error('Invalid instance %s.', instance_name) sys.exit(1) instance.load() if not instance.banner_installed(): logger.error('Banner is not installed') sys.exit(1) print(instance.get_banner())
def execute(self, argv): try: opts, _ = getopt.gnu_getopt( argv, 'i:v', ['instance=', 'file=', 'silent', 'verbose', 'debug', 'help']) except getopt.GetoptError as e: logger.error(e) self.usage() sys.exit(1) instance_name = 'pki-tomcat' banner_file = None silent = False for o, a in opts: if o in ('-i', '--instance'): instance_name = a elif o == '--file': banner_file = a elif o == '--debug': logging.getLogger().setLevel(logging.DEBUG) elif o in ('-v', '--verbose'): logging.getLogger().setLevel(logging.INFO) elif o == '--silent': silent = True elif o == '--help': self.usage() sys.exit() else: logger.error('Unknown option: %s', o) self.usage() sys.exit(1) try: if banner_file: # load banner from file with io.open(banner_file, encoding='utf-8') as f: banner = f.read().strip() else: # load banner from instance instance = pki.server.instance.PKIServerFactory.create( instance_name) if not instance.exists(): logger.error('Invalid instance %s.', instance_name) sys.exit(1) instance.load() if not instance.banner_installed(): if not silent: self.print_message('Banner is not installed') return banner = instance.get_banner() except UnicodeDecodeError as e: if logger.isEnabledFor(logging.DEBUG): logger.exception('Banner contains invalid character(s): %s', e) else: logger.error('Banner contains invalid character(s): %s', e) sys.exit(1) if not banner: logger.error('Banner is empty') sys.exit(1) if not silent: self.print_message('Banner is valid')