def match_ptn(self, s): rc = re.search(self.ptn, s) if rc is None: self.result = False else: self.result = True logger.info("Match '{0}', result {1}".format(self.ptn, self.result))
def operate(self, x1, x2): if self.val == self.OPER_NOT: result = not x1 elif self.val == self.OPER_AND: result = x1 and x2 elif self.val == self.OPER_OR: result = x1 or x2 logger.info("Operator {0}, x1 {1}, x2 {2}, result {3}".format(\ self.val, x1, x2, result)) return result
def run(self, session): ssh = session.ssh log = session.log result = session.result for cmd in self.cmd_list: rc = self.run_one_cmd(cmd, ssh, log, result) if rc != UtCmd.RC_OK: logger.info("Fail on CMD {0}, exit".format(cmd.cmdline)) break self.rc = rc return rc
def connect(self): logger.info("SSH: connect {0}".format(self.ssh_detail())) try: ssh = self.ssh #avoid xxx not found in known_hosts ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) if self.key_file != "": ssh.connect(self.host_ip, self.host_port, self.username, key_filename=self.key_file) else: ssh.connect(self.host_ip, self.host_port, self.username, self.password) except Exception as e: err = "SSH {0} failed, {1}".format(self.ssh_detail(), str(e)) raise Exception(err)
def run_one_cmd(self, cmd, ssh, log, result): rc = UtCmd.RC_OK for i in xrange(cmd.exec_cnt): logger.info("CMD: exec round #{0}".format(i)) for j in xrange(cmd.retry + 1): logger.info("CMD: try round #{0}".format(j)) log.add_action_entry(cmd.cmdline) rc, out, err = cmd.execute(ssh) log.add_result_entry(rc, out, err) if rc == UtCmd.RC_OK: result.inc_success() break if rc != UtCmd.RC_OK: self.save_last_fail_cmd(cmd, out, err) return rc
def exec_cmd(self, cmdline): logger.info("SSH: Run cmd '{0}'".format(cmdline)) rc = True try: stdin, stdout, stderr = self.ssh.exec_command(cmdline) out = stdout.read() err = stderr.read() except Exception as e: rc = False err = "Fail to exec cmd {0}, {1}, SSH {2}".format( cmdline, str(e), self.ssh_detail) out = err logger.debug("SSH: exec_cmd rc {0}".format(rc)) logger.debug("SSH: exec_cmd out:\n{0}".format(out)) logger.debug("SSH: exec_cmd err:\n{0}".format(err)) return rc, out, err
def execute(self, ssh): logger.info("CMD: Execute cmd '{0}'".format(self.cmdline)) rc, out, err = ssh.exec_cmd(self.cmdline) if rc == False or len(err) > 0: logger.info("CMD: Execute return {0}".format(rc)) return self.RC_CMD_FAIL, out, err logger.info("CMD: Evaluate Regex Group, if any") err = self.regex_grp.evaluate(out) if err != "": rc = self.RC_REGEX_FAIL else: rc = self.RC_OK return rc, out, err