def start(self): username = "" password = "" if len(self.options) <= 2: username = prompt("please input the username:"******"").strip() else: username = self.options[2] if not username or len(re.findall("[^a-zA-Z0-9]", username)) > 0: self.logger.error("username invalid", False) return if len(self.options) <= 3: password = prompt("please input the password:"******"").strip() else: password = self.options[3] if not password: self.logger.error("invalid password", False) return try: isok, res = self.api.login(username, password) if not isok: self.logger.error(res) return self.set_kindo_setting("username", username) self.set_kindo_setting("password", password) self.logger.info("login successfully") except Exception as e: self.logger.debug(traceback.format_exc()) self.logger.error(e)
def start(self): if len(self.activate_hosts) == 0: try: host = prompt("please input host: ", default="") if host is None or not host: self.logger.error("hosts not found") return port = 22 username = "******" pos = host.rfind(":") if pos == -1: host = host else: host = host[:pos] port = host[pos + 1 :] pos = host.find("@") if pos != -1: username = host[:pos] host = host[pos + 1 :] password = getpass.getpass("please input password: "******"passowrds not found") return self.activate_hosts.append({"host": host, "port": port, "username": username, "password": password}) except KeyboardInterrupt as e: pass except: self.logger.debug(traceback.format_exc()) if len(self.activate_hosts) > 1: self.logger.error("too many hosts") return try: self.execute(self.options[2:]) except KeyboardInterrupt as e: pass except EOFError as e: pass except Exception as e: self.logger.debug(traceback.format_exc()) self.logger.error(e)
def get_input_number(self, kis, max_times=3, now_time=0): if now_time < max_times: number = prompt("please input the number what you want to install:", default="") if not number: return try: number = int(number) - 1 if number < 0 or number >= len(kis): self.logger.error("number invalid") return self.get_input_number(kis, max_times, now_time + 1) return number except: return self.get_input_number(kis, max_times, now_time + 1) return -1
def execute(self, commands): for activate_host in self.activate_hosts: with KiSSHClient( activate_host["host"], int(activate_host["port"]), activate_host["username"], activate_host["password"] ) as ssh_client: while True: for command in commands: stdouts, stderrs = ssh_client.execute(command) for stdout in stdouts: self.logger.info(stdout) for stderr in stderrs: self.logger.info(stderr) commands = [ prompt( "[%s@%s:%s ~]# " % (activate_host["username"], activate_host["host"], activate_host["port"]), default="", ) ]