gpt4 book ai didi

python - 子进程.CalledProcessError : returned non-zero exit status 1 for non-pingable destination

转载 作者:太空狗 更新时间:2023-10-29 12:23:09 27 4
gpt4 key购买 nike

我正在编写一个 python 脚本来计算 数据包丢失 通过使用 ping IP 地址linux 中的 subprocess 模块。 CSV 文件中保存了多个 IP 地址。当只给出可 ping 目的地时,它运行良好。
但是当 CSV 文件中给出不可 ping 的 IP 时抛出错误,然后脚本退出而不检查该 CSV 中的其他 IP 地址> 文件。因此,我无法捕获不可 ping 目标的数据包丢失,这是脚本的主要目的。

请提出前进的方向。

subprocess.check_output(['ping','-c 4',hostname], shell=False, 
universal_newlines=True).splitlines()

subprocess.CalledProcessError: Command '['ping', '-c 4', '192.168.134.100']' returned non-zero exit status 1

最佳答案

只是如果你的 ping 有 100% 的丢包,目的地不可达或任何其他问题,subprocess 会返回一个错误。你可以做的是:

try:
# subprocess code here
except:
# some code here if the destination is not pingable, e.g. print("Destination unreachable..") or something else
pass # You need pass so the script will continue on even after the error

关于python - 子进程.CalledProcessError : returned non-zero exit status 1 for non-pingable destination,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51043456/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com