首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不要等待整个输出- Ubuntu

不要等待整个输出- Ubuntu
EN

Stack Overflow用户
提问于 2018-01-05 21:51:12
回答 1查看 2.1K关注 0票数 0

我有一个简单的脚本将SSH放入网络交换机并运行命令并将输出保存到文件中。它对即时显示的输出很好,但是当我运行“显示It”时,它不会捕获任何输出。原因是当我在开关上直接运行相同的命令时,它会思考5-6秒,显示一串行,然后再思考,然后显示多几行,然后结束。我没有正确地等待整个命令执行,所以我有问题要修复:

代码语言:javascript
复制
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()

这是一个示例输出。输出有"#“,但没有”#“。

代码语言:javascript
复制
#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 ....

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

编辑:我增加了睡眠(60),这似乎很有效,但我不想使用它,因为我发送多个命令,有些命令非常快。我不想等待1分钟的每个命令,脚本将永远运行。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-26 18:04:45

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

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

代码语言:javascript
复制
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

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

代码语言:javascript
复制
command_details_dict = { "cmd_details" :[
                                         {'cmd': 'pwd',
                                          'timeout': 5,
                                          },
                                         {'cmd': 'iproute',
                                          'timeout': 60,
                                          }
                                         ]
                       } 

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

但是如果你有时间,我建议你用一个配置文件来代替

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48121769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档