pexpect-等待在继续之前完成命令

发布于 2025-02-01 15:05:47 字数 365 浏览 4 评论 0原文

我目前的情况与pexpect。

child = pexpect.spawn(f"virsh console {hostname} --force", timeout=200, maxread=4000)

child.sendline("sudo config ztp disable -y")
child.send("\r")
print(" Waiting ZTP ")
sleep(120)
# rest of my script

现在我要在“睡眠(120)”之后睡觉

这个命令可以花1-2分钟才能完成, pexect脚本一旦禁用命令完成而不是固定睡眠时间。

I have a current situation with pexpect.

child = pexpect.spawn(f"virsh console {hostname} --force", timeout=200, maxread=4000)

child.sendline("sudo config ztp disable -y")
child.send("\r")
print(" Waiting ZTP ")
sleep(120)
# rest of my script

This command can take 1-2min to finish, now I'm doing a sleep after it "sleep(120)"

I would like to know if there is a way to perform it using pexpect arguments instead of sleep, so I can continue my pexect script as soon as the disable command finishes instead of having a fixed sleep time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一影成城 2025-02-08 15:05:47

您可以尝试使用waitnoecho(),它适用于我的情况。 <如果您发现不同的工作解决方案,请与我分享。

You can try using waitnoecho(), it worked for my case. Here is the link for the docs. Share with me if you found different working solution.

喜你已久 2025-02-08 15:05:47
admin@sonic:~$ sudo config ztp disable -y
Running command: ztp disable -y
May 26 08:20:45.302973+00:00 2022 sonic WARNING sonic-ztp[2858]: Received terminate signal. Shutting down.
[  131.941412] e1000 0000:00:03.0 eth1: Reset adapter
[  132.053160] e1000 0000:00:03.0 eth1: Reset adapter
[  132.105350] e1000 0000:00:04.0 eth2: Reset adapter
[  132.165155] e1000 0000:00:03.0 eth1: Reset adapter
[  132.311991] e1000 0000:00:03.0 eth1: Reset adapter
[  132.317174] e1000 0000:00:05.0 eth3: Reset adapter
[  132.505185] e1000 0000:00:06.0 eth4: Reset adapter

May 26 08:20:56.865683 System is not ready - one or more services are not up
[  237.895018] e1000 0000:00:05.0 eth3: Reset adapter
Waiting for system online status before stopping ZTP. (This may take 30--180 seconds).
Removing ZTP configuration profile. Loading factory default configuration.
Platform and HWSKU Valid - /usr/share/sonic/device/x86_64-kvm_x86_64-r0/DellEMC-S5248f-P-25G-DPB
Running command: /usr/local/bin/db_migrator.py -o check_version -f /etc/sonic/config_db.json
Stopping SONiC target ...
Stopping service bgp.service ...
Stopping service caclmgrd.service ...
Stopping service dhcp_relay.service ...
Stopping service gbsyncd.service ...
Stopping service hostcfgd.service ...
Stopping service iccpd.service ...
Stopping service l2mcd.service ...
Stopping service lldp.service ...
Stopping service macsec.service ...
Stopping service mgmt-framework.service ...
Stopping service mgmt-framework.timer ...
Stopping service nat.service ...
Stopping service pmon.service ...
Stopping service portinitdone.service ...
Stopping service procdockerstatsd.service ...
Stopping service radv.service ...
Stopping service rsyslog-config.service ...
Stopping service sflow.service ...
Stopping service stp.service ...
Stopping service swss.service ...
Stopping service syncd.service ...
Stopping service tam.service ...
Stopping service teamd.service ...
Stopping service telemetry.timer ...
Stopping service udld.service ...
Stopping service vrrp.service ...
Services are stopped

Running command: queuestat -D
Running command: /usr/local/bin/sonic-cfggen -j /etc/sonic/copp_config.json --write-to-db
Running command: /usr/local/bin/sonic-cfggen -j /etc/sonic/init_cfg.json --write-to-db
Running command: /usr/local/bin/sonic-cfggen -j /etc/sonic/config_db.json --write-to-db
upgrade: Migrating Conifg DB from version_4_0_1 to version_4_0_1
Running command: /usr/local/bin/sonic-cfggen -d -y /etc/sonic/sonic_version.yml -t /usr/share/sonic/templates/sonic-environment.j2,/etc/sonic/sonic-environment
Resetting failed status on bgp.service
Resetting failed status on caclmgrd.service
Resetting failed status on dhcp_relay.service
Resetting failed status on gbsyncd.service
Resetting failed status on hostcfgd.service
Resetting failed status on hostname-config.service
Resetting failed status on iccpd.service
Resetting failed status on interfaces-config.service
Resetting failed status on l2mcd.service
Resetting failed status on lacp_helper.service
Resetting failed status on lldp.service
Resetting failed status on macsec.service
Resetting failed status on mgmt-framework.service
Resetting failed status on mgmt-framework.timer
Resetting failed status on nat.service
Resetting failed status on ntp-config.service
Resetting failed status on pmon.service
Resetting failed status on portinitdone.service
Resetting failed status on procdockerstatsd.service
Resetting failed status on radv.service
Resetting failed status on rsyslog-config.service
Resetting failed status on sflow.service
Resetting failed status on stp.service
Resetting failed status on swss.service
Resetting failed status on syncd.service
Resetting failed status on tam.service
Resetting failed status on teamd.service
Resetting failed status on telemetry.timer
Resetting failed status on udld.service
Resetting failed status on vrrp.service
Restarting SONiC target ...
Starting service bgp.service ...
Starting service caclmgrd.service ...
Starting service dhcp_relay.service ...
Starting service gbsyncd.service ...
Starting service hostcfgd.service ...
Starting service iccpd.service ...
Starting service l2mcd.service ...
Starting service lldp.service ...
Starting service macsec.service ...
Starting service mgmt-framework.service ...
Starting service mgmt-framework.timer ...
Starting service nat.service ...
Starting service pmon.service ...
Starting service portinitdone.service ...
Starting service procdockerstatsd.service ...
Starting service radv.service ...
Starting service rsyslog-config.service ...
Starting service sflow.service ...
Starting service stp.service ...
Starting service swss.service ...
Starting service syncd.service ...
Starting service tam.service ...
Starting service teamd.service ...
Starting service telemetry.timer ...
Starting service udld.service ...
Starting service vrrp.service ...
Services are started

Reloading Monit configuration ...
Reinitializing monit daemon
Reload complete!
Verifying if core services have started
admin@sonic:~$
admin@sonic:~$ sudo config ztp disable -y
Running command: ztp disable -y
May 26 08:20:45.302973+00:00 2022 sonic WARNING sonic-ztp[2858]: Received terminate signal. Shutting down.
[  131.941412] e1000 0000:00:03.0 eth1: Reset adapter
[  132.053160] e1000 0000:00:03.0 eth1: Reset adapter
[  132.105350] e1000 0000:00:04.0 eth2: Reset adapter
[  132.165155] e1000 0000:00:03.0 eth1: Reset adapter
[  132.311991] e1000 0000:00:03.0 eth1: Reset adapter
[  132.317174] e1000 0000:00:05.0 eth3: Reset adapter
[  132.505185] e1000 0000:00:06.0 eth4: Reset adapter

May 26 08:20:56.865683 System is not ready - one or more services are not up
[  237.895018] e1000 0000:00:05.0 eth3: Reset adapter
Waiting for system online status before stopping ZTP. (This may take 30--180 seconds).
Removing ZTP configuration profile. Loading factory default configuration.
Platform and HWSKU Valid - /usr/share/sonic/device/x86_64-kvm_x86_64-r0/DellEMC-S5248f-P-25G-DPB
Running command: /usr/local/bin/db_migrator.py -o check_version -f /etc/sonic/config_db.json
Stopping SONiC target ...
Stopping service bgp.service ...
Stopping service caclmgrd.service ...
Stopping service dhcp_relay.service ...
Stopping service gbsyncd.service ...
Stopping service hostcfgd.service ...
Stopping service iccpd.service ...
Stopping service l2mcd.service ...
Stopping service lldp.service ...
Stopping service macsec.service ...
Stopping service mgmt-framework.service ...
Stopping service mgmt-framework.timer ...
Stopping service nat.service ...
Stopping service pmon.service ...
Stopping service portinitdone.service ...
Stopping service procdockerstatsd.service ...
Stopping service radv.service ...
Stopping service rsyslog-config.service ...
Stopping service sflow.service ...
Stopping service stp.service ...
Stopping service swss.service ...
Stopping service syncd.service ...
Stopping service tam.service ...
Stopping service teamd.service ...
Stopping service telemetry.timer ...
Stopping service udld.service ...
Stopping service vrrp.service ...
Services are stopped

Running command: queuestat -D
Running command: /usr/local/bin/sonic-cfggen -j /etc/sonic/copp_config.json --write-to-db
Running command: /usr/local/bin/sonic-cfggen -j /etc/sonic/init_cfg.json --write-to-db
Running command: /usr/local/bin/sonic-cfggen -j /etc/sonic/config_db.json --write-to-db
upgrade: Migrating Conifg DB from version_4_0_1 to version_4_0_1
Running command: /usr/local/bin/sonic-cfggen -d -y /etc/sonic/sonic_version.yml -t /usr/share/sonic/templates/sonic-environment.j2,/etc/sonic/sonic-environment
Resetting failed status on bgp.service
Resetting failed status on caclmgrd.service
Resetting failed status on dhcp_relay.service
Resetting failed status on gbsyncd.service
Resetting failed status on hostcfgd.service
Resetting failed status on hostname-config.service
Resetting failed status on iccpd.service
Resetting failed status on interfaces-config.service
Resetting failed status on l2mcd.service
Resetting failed status on lacp_helper.service
Resetting failed status on lldp.service
Resetting failed status on macsec.service
Resetting failed status on mgmt-framework.service
Resetting failed status on mgmt-framework.timer
Resetting failed status on nat.service
Resetting failed status on ntp-config.service
Resetting failed status on pmon.service
Resetting failed status on portinitdone.service
Resetting failed status on procdockerstatsd.service
Resetting failed status on radv.service
Resetting failed status on rsyslog-config.service
Resetting failed status on sflow.service
Resetting failed status on stp.service
Resetting failed status on swss.service
Resetting failed status on syncd.service
Resetting failed status on tam.service
Resetting failed status on teamd.service
Resetting failed status on telemetry.timer
Resetting failed status on udld.service
Resetting failed status on vrrp.service
Restarting SONiC target ...
Starting service bgp.service ...
Starting service caclmgrd.service ...
Starting service dhcp_relay.service ...
Starting service gbsyncd.service ...
Starting service hostcfgd.service ...
Starting service iccpd.service ...
Starting service l2mcd.service ...
Starting service lldp.service ...
Starting service macsec.service ...
Starting service mgmt-framework.service ...
Starting service mgmt-framework.timer ...
Starting service nat.service ...
Starting service pmon.service ...
Starting service portinitdone.service ...
Starting service procdockerstatsd.service ...
Starting service radv.service ...
Starting service rsyslog-config.service ...
Starting service sflow.service ...
Starting service stp.service ...
Starting service swss.service ...
Starting service syncd.service ...
Starting service tam.service ...
Starting service teamd.service ...
Starting service telemetry.timer ...
Starting service udld.service ...
Starting service vrrp.service ...
Services are started

Reloading Monit configuration ...
Reinitializing monit daemon
Reload complete!
Verifying if core services have started
admin@sonic:~$
瑕疵 2025-02-08 15:05:47

使用期望()要关注命令的输出,并等待其成功完成的提示。

from shlex import quote
prompt = 'admin@sonic:~[$]'

child = pexpect.spawn(f"virsh console {quote(hostname)} --force", timeout=200, maxread=4000)

# wait until we see the first prompt before sending that command
# that way we can't match that prompt later and think the process finished early
child.expect(prompt)

child.sendline("sudo config ztp disable -y")
child.send("\r")
print(" Waiting ZTP ")

# Wait for the prompt again; this one will only match the one from _after_
# the command finishes execution, because we already matched the one from before
child.expect(prompt)
print(" Finished waiting for ZTP ")

Use expect() to watch the output of your command and wait for the prompt that follows its successful completion.

from shlex import quote
prompt = 'admin@sonic:~[$]'

child = pexpect.spawn(f"virsh console {quote(hostname)} --force", timeout=200, maxread=4000)

# wait until we see the first prompt before sending that command
# that way we can't match that prompt later and think the process finished early
child.expect(prompt)

child.sendline("sudo config ztp disable -y")
child.send("\r")
print(" Waiting ZTP ")

# Wait for the prompt again; this one will only match the one from _after_
# the command finishes execution, because we already matched the one from before
child.expect(prompt)
print(" Finished waiting for ZTP ")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文