【问题标题】:Pexpect not waiting for whole output - UbuntuPexpect 不等待整个输出 - Ubuntu
【发布时间】:2018-01-05 21:51:12
【问题描述】:

我有一个简单的脚本,可以通过 SSH 连接到网络交换机并运行命令并将输出保存到文件中。它适用于立即显示的输出,但是当我运行“show iproute”时,它不会捕获任何输出。原因是当我直接在交换机上运行相同的命令时,它会思考 5-6 秒,显示一堆行并再次思考并显示更多行然后结束。它没有正确等待整个命令执行,我有问题修复:

str_prompt = ' # '
command = "sh iproute"
device_name = "switch1.test.com"

# Spawn SSH session
ssh_command = 'ssh {}@{}'.format(username, device_name)
session = pexpect.spawn(ssh_command, timeout=5)

# Send the password
session.sendline(password)

# Expect the switch prompt (successful login)
expect_index = session.expect([pexpect.TIMEOUT, str_prompt])
# Success
if expect_index == 1:
    # Disable clipaging so that all the output is shown (not in pages) | same as term len 0 in Cisco
    session.sendline('disable clip')
    # Expect the switch prompt if command is successful
    expect_index = session.expect([pexpect.TIMEOUT, str_prompt])

    # Send show iproute command
    session.sendline(command)
    # < This is where it needs to wait >
    #session.expect(pexpect.EOF) - Tried this and wait() but that broke the scipt
    #session.wait()
    # Expect the switch prompt if command is successful
    session.expect(str_prompt)

    # Save output of "sh iproute" to a variable
    output = session.before
    # Save results to a file
    fp = open(host + '-route.txt', "w")
    fp.write(output)
    fp.close()

这是一个示例输出。输出确实有“#”但没有“#”。

#oa  10.10.10.0/24      10.0.0.1    4    UG-D---um--f- V-BB1 99d:0h:14m:49s
#oa  10.10.20.0/24      10.0.0.1    4    UG-D---um--f- V-BB2 99d:0h:14m:49s
#oa  10.10.30.0/24      10.0.0.1    4    UG-D---um--f- V-BB3 99d:0h:14m:49s
#oa  10.10.40.0/24      10.0.0.1    4    UG-D---um--f- V-BB4 99d:0h:14m:49s
and many more line ....

任何帮助将不胜感激。谢谢

编辑: 我添加了 sleep(60) ,这似乎可以解决问题,但我不想使用它,因为我正在发送多个命令,而且有些命令非常快。我不想为每个命令等待 1 分钟,脚本将永远运行。

【问题讨论】:

  • 撞我的问题....

标签: python python-2.7 pexpect


【解决方案1】:

因此您需要将超时与命令相关联。 我今天做的方式是有一个我的代码解析的xml文件格式,xml标签将有一个命令属性和一个超时属性,另一个用于命令的end_prompt等等..

我的代码读取命令及其超时,并在发送命令之前相应地设置相应的变量。

session.sendline(command) #command is read from a xml file
session.expect(end_prompt, timeout=int(tmout)) # end_prompt, tmout for the command read form the same file

对于您的情况,如果您不想解析文件以获取命令及其相关参数,您可以将它们作为脚本中的字典并使用它

command_details_dict = { "cmd_details" :[
                                         {'cmd': 'pwd',
                                          'timeout': 5,
                                          },
                                         {'cmd': 'iproute',
                                          'timeout': 60,
                                          }
                                         ]
                       } 

字典里面 cmd_details 是一个字典列表,这样你可以在迭代时保持命令的顺序,每个命令都是一个包含相关细节的字典,你可以在命令字典中添加更多的键,比如提示、唯一标识符等。 .

但是如果你有时间我会建议使用配置文件来代替

【讨论】:

  • 感谢您的反馈!
  • 这很好,但在大多数情况下,您不需要将不同的超时时间关联到不同的命令。您可以简单地为最慢的命令设置超时。与添加睡眠不同,超时仅在您没有在输出中看到命令提示符时生效。如果您有更长的超时时间,但很快就会看到命令提示符,则脚本会继续运行,并且超时不会产生任何影响。
猜你喜欢
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 2022-12-22
  • 2019-02-14
相关资源
最近更新 更多