过滤期望的远程命令输出

问题描述 投票:0回答:0

我希望在远程虚拟服务器中执行一些配置。

  • 我需要在此远程服务器上运行正常运行时间

  • 获取正常运行时间输出

  • 如果正常运行时间少于 5 分钟,我需要等待,否则我可以继续并配置此服务器

      child = pexpect.spawn(f"virsh console {hostname} --force", timeout=3)
      child.send("\r\r\r")
      child.expect(".*ogin:")
    
      # Step 1: Log in as root
      print("Step 1: Log in as root")
      self.logger.debug(f"sending USER: {USER}")
      child.sendline(USER)
      child.expect("root.*", timeout=3)
      child.send("\r")
      child.sendline('uptime')
      child.expect("root.*", timeout=3)
      child.send("\r")
      child.expect("root.*", timeout=3)
      r = child.before.decode('utf-8').split("\r")
      print(r)
      child.send("exit")
      child.sendcontrol("]")
    

下面是我的 print(r) 输出

uptime
12:04AM  up  4 mins, 1 user, load averages: 0.87, 0.54, 0.45

我真的不知道我正在做的是否是获得正常运行时间输出的最佳方法

在这种情况下我确实需要使用 pexpect!

感谢这里的任何建议/帮助。

python-3.x pexpect
© www.soinside.com 2019 - 2024. All rights reserved.