在循环中使用布尔值

发布于 2025-01-25 07:01:12 字数 867 浏览 2 评论 0原文

我有一个使用NTFY向我的手机发送通知的脚本。

它为设备ping ping,如果ping响应错误,它将向我的手机发送通知。

for ip in ap_ips:
    try:
        ping3.ping(ip)
        requests.post(ntfy_adress,
                      data=("Das Gerät mit der IP-Adresse " + ip + " ist online.").encode(
                          encoding='utf-8'),
                      headers={
                          "Title": ("Unifi AP ist Online!"),
                      })
    except:
        requests.post(ntfy_adress,
                      data=("Das Gerät mit der IP-Adresse " + ip + " ist nicht mehr erreichbar.").encode(encoding='utf-8'),
                      headers={
                          "Title": ("Unifi AP ist Offline!"),
                          "Tags": "warning"
                      })

该脚本应该在无限循环中运行。

如果ping结果比以前,我如何才能获得脚本仅发送通知?

I have a Script that sends a Notification to my Phone using ntfy.

It pings a Device and if the Ping responds with an Error, it sends a Notification to my Phone.

for ip in ap_ips:
    try:
        ping3.ping(ip)
        requests.post(ntfy_adress,
                      data=("Das Gerät mit der IP-Adresse " + ip + " ist online.").encode(
                          encoding='utf-8'),
                      headers={
                          "Title": ("Unifi AP ist Online!"),
                      })
    except:
        requests.post(ntfy_adress,
                      data=("Das Gerät mit der IP-Adresse " + ip + " ist nicht mehr erreichbar.").encode(encoding='utf-8'),
                      headers={
                          "Title": ("Unifi AP ist Offline!"),
                          "Tags": "warning"
                      })

The Script is supposed to be running in a infinite Loop.

How do i get the Script to only send a Notification if the Ping Result is diffrent than before?

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

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

发布评论

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

评论(1

梦太阳 2025-02-01 07:01:12

您必须在两个执行状态之间存储。在您的期间循环做类似的事情之前:

status = {}

然后,检查上一个状态不是当前状态

for ip in ap_ips:
    try:
        ping3.ping(ip)

        if status.get(ip) != "OK":
            requests.post(
                ntfy_adress,
                data=("Das Gerät mit der IP-Adresse " + ip + " ist online.").encode(
                    encoding="utf-8"
                ),
                headers={
                    "Title": ("Unifi AP ist Online!"),
                },
            )
        status[ip] = "OK"

    except:  # You should set explicitly which error type you expect here if ping fails.
        if status.get(ip) != "KO":
            requests.post(
                ntfy_adress,
                data=(
                    "Das Gerät mit der IP-Adresse " + ip + " ist nicht mehr erreichbar."
                ).encode(encoding="utf-8"),
                headers={"Title": ("Unifi AP ist Offline!"), "Tags": "warning"},
            )
        status[ip] = "KO"

You have to store between two executions the status. Before your while loop do something like:

status = {}

And then, check that the previous status is not the current one

for ip in ap_ips:
    try:
        ping3.ping(ip)

        if status.get(ip) != "OK":
            requests.post(
                ntfy_adress,
                data=("Das Gerät mit der IP-Adresse " + ip + " ist online.").encode(
                    encoding="utf-8"
                ),
                headers={
                    "Title": ("Unifi AP ist Online!"),
                },
            )
        status[ip] = "OK"

    except:  # You should set explicitly which error type you expect here if ping fails.
        if status.get(ip) != "KO":
            requests.post(
                ntfy_adress,
                data=(
                    "Das Gerät mit der IP-Adresse " + ip + " ist nicht mehr erreichbar."
                ).encode(encoding="utf-8"),
                headers={"Title": ("Unifi AP ist Offline!"), "Tags": "warning"},
            )
        status[ip] = "KO"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文