Python ping 脚本在第一次尝试时总是失败

标签 python linux networking device ping

当运行 python 脚本来 ping 我网络中的节点时,我遇到了失败问题,并且它在我的第一次尝试中总是失败......再次运行时......它有效。我的环境是 linux 机器,我只 ping 到 3 台设备。

为了测试它,我只创建了一个简单的脚本。第一次尝试它会失败,如果我一次又一次地运行脚本......一切正常......然后在我离开 15 分钟到 30 分钟后......我运行它会失败......然后再次运行......好的...我可以知道你们是否面临这种问题。

代码如下;

def verify_ping():
    ipadd = "192.168.1.1"
    response = os.system("ping -c 1 " + ipadd)

    if response == 0:
        ipstatus = "UP"
    else:
        ipstatus = "DOWN"

    return ipstatus

ping_status = verify_ping()

接下来是PING失败

PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
From 192.168.1.1 icmp_seq=1 Redirect Host(New nexthop: 192.168.1.100)

--- 192.168.1.1 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms

平好

PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=250 time=1.33 ms

--- 192.168.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms

与此同时,我继续对 IP 执行 ping 操作,到目前为止 IP 已启动且可访问。也许有人遇到了问题或者我需要添加到脚本中的东西。请进一步告知,感谢您的宝贵时间。

最佳答案

可能与ARP问题有关。这可能取决于您的环境的网络架构,以及您的网络设备的参数

(参见 https://learningnetwork.cisco.com/thread/85577)

关于Python ping 脚本在第一次尝试时总是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56337562/

相关文章:

python - 无法在 label-studio 中添加 Ml 后端

python - 从音频文件生成波形图像

Linux bash 脚本卡在 sleep 状态

machine-learning - 将多个 OSC 信号合并为一个

python - 在 python 中使用 lxml 和 xpath 获取空列表

python - 从 Python 中的响应中读取特定的 JSON 对象

python - 字典中使用的属性的 Setter

javascript - 未知插件 "transform-decorators-legacy"在windows中使用react storybook时报错

networking - 客户端-服务器模型有多少种实现方式

forms - API中application/x-www-form-urlencoded POST请求参数的字符编码(非Form)