Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to grab MQTT protocol packets for debugging and analysis

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to grab MQTT protocol packets for debugging and analysis". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to grab MQTT protocol packets for debugging and analysis".

You may encounter some problems when using the MQTT protocol to develop Internet of things-related projects or to use them as message queues:

For example, why did the subscription fail? Why is the connection broken and so on?

There is an attribute of code in MQTT5.0, which can be used to analyze the cause of the error through code, but there is no attribute of code in MQTT3.x, so if you analyze a problem, you can only use the package grab tool to analyze it.

You can use tcpdump to grab packets. The command is as follows:

Tcpdump-I en0 port 1883-w mqtt.pcap

Here, you need to modify the network card and the corresponding port number, and use ifconfig to view the network card information.

Lo0: flags=8049 mtu 16384

Options=1203

Inet 127.0.0.1 netmask 0xff000000

Inet6:: 1 prefixlen 128

Inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1

Nd6 options=201

En0: flags=8863 mtu 1500

Options=400

Ether f0:18:98:ae:9e:df

Inet6 fe80::1053:12ff:524e:c2a2%en0 prefixlen 64 secured scopeid 0x6

Inet 192.168.2.139 netmask 0xffffff00 broadcast 192.168.2.255

Nd6 options=201

Media: autoselect

Status: active

Server Broker can use en0 if it uses public network communication, while lo0 can use local internal network communication.

Port 1883 can be set according to the actual port. Generally, the default is 1883. It is recommended to use a regular port:

1883: MQTT, unencrypted 8883: MQTT, encrypted 8884: MQTT, encrypted, client certificate required 8887: MQTT, encrypted, server certificate deliberately expired 8080: MQTT over WebSockets, unencrypted 8081: MQTT over WebSockets, encrypted

After executing the tcpdump, see the following output, and you can initiate the request

$tcpdump-I en0 port 1883-w mqtt.pcap

Tcpdump: listening on en0, link-type EN10MB (Ethernet), capture size 262144 bytes

After the problematic request, end with CTRL+C and see the following output:

$tcpdump-I en0 port 1883-w mqtt.pcap

Tcpdump: listening on en0, link-type EN10MB (Ethernet), capture size 262144 bytes

^ C14 packets captured

70 packets received by filter

0 packets dropped by kernel

Indicates that 14 packets have been captured and can be analyzed. Wireshark can be used to open the generated mqtt.pcap file.

Of course, you can also directly use Wireshark for packet capture analysis.

MQTT 5.0contains code return codes, which can be converted into human-readable information using Simps\ MQTT\ Hex\ ReasonCode in simps/mqtt, such as:

Use Simps\ MQTT\ Client

Use Simps\ MQTT\ Hex\ ReasonCode

Use Simps\ MQTT\ Protocol\ V5

Use Simps\ MQTT\ Config\ ClientConfig

Use function Swoole\ Coroutine\ run

Run (function () {

Config = (new ClientConfig ())-> setClientId (Client::genClientID ())

-> setKeepAlive (10)

-> setDelay (3000) / / 3s

-> setMaxAttempts (5)

-> setProtocolLevel (V5::MQTT_PROTOCOL_LEVEL_5_0)

-> setSwooleConfig ([

'open_mqtt_protocol' = > true

'package_max_length' = > 2 * 1024 * 1024

'connect_timeout' = > 5. 0

])

$client = new Client ('broker.emqx.io', 1883, $config)

$res = $client- > connect ()

Var_dump ('Connect:'. ReasonCode::getReasonPhrase ($res ['code']))

$res = $client- > publish ('simps-mqtt/test',' hello', 3)

Var_dump ($res)

Var_dump ('Publish:'. ReasonCode::getReasonPhrase ($res ['code']))

});

Connect succeeds normally and returns Success, while publish is disconnected because it publishes the wrong qos level 3. The error message is: QoS not supported.

Thank you for reading, the above is the content of "how to grab MQTT protocol packets for debugging and analysis". After the study of this article, I believe you have a deeper understanding of how to grab MQTT protocol packets for debugging and analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report