child = pexpectspawnselfssh_cmd maxread=2000 timeout=20 timesleep5 ret = childexpectpexpectTIMEOUT Ppassword yesno r$怎么把expect里面的实际字符串打出来
可以使用child.before属性来获取expect方法匹配到的实际字符串,例如:
child = pexpect.spawn(self.ssh_cmd, maxread=2000, timeout=20) time.sleep(5) ret = child.expect([pexpect.TIMEOUT, "[P|p]assword:", "yes/no", r"$"]) if ret == 0: print("Timeout occurred") elif ret == 1: print("Password prompt detected") print(child.before) elif ret == 2: print("SSH key fingerprint prompt detected") elif ret == 3: print("Shell prompt detected") print(child.before)
原文地址: https://www.cveoy.top/t/topic/fFTa 著作权归作者所有。请勿转载和采集!