如何让mosquitto_sub在订阅#时打印主题和消息

问题描述 投票:0回答:2

以下命令显示发布到与正则表达式匹配的主题的所有消息,但不显示确切主题本身。

mosquitto_sub -h localhost -u user -P pass -t 'devices/#'
{"value":"50"}
{"value":"45"}

例如,上面的 json 消息已发布到主题 devices/1234/transducer/46364/ 但我无法找到任何方法来使用 mosquitto_sub 打印该主题。

mqtt mosquitto
2个回答
36
投票

使用

-v
选项

mosquitto_sub -h localhost -u user -P pass -v -t 'devices/#'

来自手册页:

   -v, --verbose
       Print received messages verbosely. With this argument, messages
       will be printed as "topic payload". When this argument is not
       given, the messages are printed as "payload".

0
投票

老问题,但今天刚到这里,不想要 -v 的“冗长”。

这也可以使用 -F 选项来解决。

mosquitto_sub -h localhost -u user -P pass -t 'devices/#' -F "%t>%p"

输出示例:

设备/1/状态>离线

来自手册页:

    -F 
        Specify output printing format. This option allows you to choose 
        what information from each message is printed to the screen.
        See the Output Format section[1] below for full details.

在使用 JSON 时考虑使用这些助手:

%j JSON output of message parameters and timestamp, with a quoted and
escaped payload.

{"tst":"2020-05-06T22:12:00.000000+0100","topic":"问候语","qos":0,"retain":0,"payload":"hello world"}

%J JSON output of message parameters and timestamp, with a non-quoted 
and non-escaped payload - this means the payload must itself be 
valid JSON.
If the payload is not valid JSON, then the error message 
"Error: Message payload is not valid JSON on topic <topic>"
will be printed to stderr.

{“tst”:“2020-05-06T22:12:00.000000+0100”,“主题”:“foo”,“qos”:0,“保留”:0,“有效负载”:{“温度”:27.0 ,“湿度”:57}}。 >

© www.soinside.com 2019 - 2024. All rights reserved.