在循环中使用布尔值
我有一个使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在两个执行状态之间存储。在您的期间循环做类似的事情之前:
然后,检查上一个状态不是当前状态
You have to store between two executions the status. Before your while loop do something like:
And then, check that the previous status is not the current one