我见过用于清除一次性消息的
mosquitto_pub -h [server] -r -n -t [XYZ]
语法。我的问题是设备开发人员发布了很多垃圾消息。
我有一个 Java/Paho 代码库,我想修改它以根据需要自动执行此操作,但我似乎无法发布零字节消息。我试过了
client.publish(topic,null);
...但这似乎没有用。
关于如何批量删除所有内容有什么建议吗?
这里是如何使用 shell 脚本正确地做到这一点。
#!/bin/sh
echo "cleaning " $1 " :: usage: cleanmqtt <host>"
mosquitto_sub -h $1 -t "#" -v --retained-only | while read line; do mosquitto_pub -h $1 -t "${line% *}" -r -n; done
只需将它放在一个名为之类的文件中
finally_a_working_way_to_remove_all_those_annoying_messages.sh
然后运行
sh finally_a_working_way_to_remove_all_those_annoying_messages.sh localhost
这个解决方案很粗糙。您无法指定要删除的内容或任何内容。 在您可以假设它已收到所有消息后,您可能必须使用 ctrl-c 中止。
使用 paho 客户端代码有 2 个选项,具体取决于您使用的 2
publish
方法中的哪一个。
MqttMessage msg = new MqttMessage(new byte[0]);
msg.setRetained(true);
client.publish(topic, msg);
或
client.publish(topic, new byte[0],0,true);
另一种选择是停止 mosquitto 并删除持久性文件并重新启动
Mosquitto客户端提供--remove-retained选项:
mosquitto_sub -h $host --remove-retained -t '#' -E
Tunning -t 可以处理要清除的特定主题。
因为我没有足够的积分来评论,跑步
#!/bin/sh
echo "cleaning " $1 " :: usage: cleanmqtt <host>"
mosquitto_sub -h $1 -t "#" -v | while read line; do mosquitto_pub -h $1 -t "${line% *}" -r -n; done
可能会由于发布/订阅延迟而导致无限循环。 将
--retained-only
添加到 mosquitto_sub
似乎消除了无限循环。
这应该有效:
client.publish(topic, new byte[]{}, 0, true);
另外,您可能对来自 Eclipse Paho Python 的这个脚本感兴趣,用于清除给定的主题层次结构。您可能想在 Java 中实现类似的行为,但看起来您可能正在寻找一次性解决方案,所以也许只使用 Python 脚本 :)
在 Windows 上有这个问题,所以在这里使用 powershell。命令主机需要安装Mosquitto
我不想清除所有保留的消息。例如,只有主题中包含“octo”的那些。让我们看看那里有什么:
mosquitto_sub.exe -h <mqtt host> -v -u <mqtt user> -P <mqtt password> -t '#' --retained-only|
Select-String octo
(根据需要替换 mqtt 主机、用户、密码)
在这里使用相同的搜索字符串(在本例中为“octo”):
mosquitto_sub.exe -h <mqtt host> -v -u <mqtt user> -P <mqtt password> -t '#' --retained-only|
Select-String octo |
Out-String -Stream|ForEach-Object -Process {$_.Split(" ")[0]}|
%{mosquitto_pub.exe -h <mqtt host> -u <mqtt user> -P <mqtt password> -t "$_" -r -n}
再次,替换所有出现的 mqtt 主机、用户、密码。
如果还有任何东西,只需再次检查步骤 1 :)
如果您使用的是 Mosquitto MQTT Broker,请使用 Mosquitto 提供的官方方法禁用“保留消息”
首先找到mosquitto.conf文件
(在我的 Ubuntu/EC2 实例中,它存储在 /etc/mosquitto 目录中,我假设您的 mosquitto.conf 文件路径是 /etc/mosquitto/mosquitto.conf)
用你最喜欢的文本编辑器编辑,我的是 nano。
sudo nano /etc/mosquitto/mosquitto.conf
并在该文件中用“persistence false”替换“persistence true”
persistence false
现在保存文件(如果使用nano按ctrl+o然后回车保存,ctrl+x退出)
现在使用以下命令重启 mosquitto
sudo service mosquitto stop
sudo service mosquitto start
注意: 如果您的情况下不存在此配置路径,请使用此命令查找配置文件-
sudo find / -name mosquitto.conf
如果你在远程计算机上,你可以这样做。
mosquitto_sub -h HOST -t 'TOPIC' --remove-retained --retained-only -u USER -P PASSWORD
对我有用!
我没有足够的代表来添加简单的评论。 bash 脚本很棒!我遇到的极端情况是存在多个空格的情况,例如日期戳:
bob/was/here/time 2021-08-31 01:52:51
${line% *} 仅删除时间,同时将日期包含在主题中。添加第二个 % 到 ${line%% *} 使替换变得贪婪,确保只返回主题。
来自
mosquitto_sub
手册页
示例 1. 删除服务器上所有保留的消息,假设我们有权这样做,然后退出:
mosquitto_sub -t '#' --remove-retained --retained-only
作为函数:
clmq(){ mosquitto_sub -t '#' --remove-retained --retained-only -d -W ${4:-1} -h ${1:-localhost} -u ${2} -P ${3}; }
用法示例:
clmq myhostoripaddr myusername mypassword mytimeout
请注意,选项
-W 1
会导致mosquitto_sub
在连接到代理后超时1秒(这样您就不必按Ctrl+C
来终止mosquitto_sub
进程)。
如果您有很多保留消息要清除,您可能需要删除
-W 1
选项并手动杀死 mosquitto_sub
(一旦它完成向之前有保留消息的所有主题发布空消息)。