引言
以前在物联网中我主要关注MQTT协议,最近因为工作需要,对CoAP协议也做了一些工作。今天先记录一下CoAP客户端调试工具CoAP Shell。CoAP - Shell(CoAP 命令行工具)是一个用于与基于 CoAP的设备或服务进行交互的命令行工具,它为开发者和测试人员提供了一种方便快捷的方式来测试和调试 CoAP 端点。
软件安装
CoAP Shell的官方地址是https://github.com/sanshengshui/coap-shell?tab=readme-ov-file。如果想直接获得可以运行Jar包,可以访问抓住CoAP协议的“心” - 三升水 - 博客园获得编译好的文件。
软件的使用
下载后直接使用下面的命令行运行:
java -jar coap-shell.jar
软件启动后界面如下:
输入Help可以得到命令列表。
AVAILABLE COMMANDS
Built-In Commands
clear: Clear the shell screen.
exit, quit: Exit the shell.
help: Display help about available commands.
history: Display or save the history of previously run commands
script: Read and execute commands from a file.
stacktrace: Display the full stacktrace of the last error.
CoAP Commands
* delete: Delete CoAP Resource
* discover: List available resources
* get: Request data from CoAP Resource
mime types: List supported MIME types
* observe: Start observing data from a CoAP Resource
* observe show messages: List observed responses
* observe stop: Stop the observe task
* post: Create/Update data in CoAP Resource
* put: Update data in CoAP Resource
CoAP Server Connectivity
connect: Connect to CoAP server
* message acknowledgement: Enables/Disables message acknowledgement
* ping: Check CoAP resources availability
* shutdown: Disconnect from the CoAP server
Ikea Gateway Commands
* ikea device list: List all devices registered to the IKEA TRÅDFRI Gateway
* ikea device name: Get or change a device name
ikea gateway key: Generate IDENTITY and PRE_SHARED_KEY for IKEA TRÅDFRI Gateway
* ikea turn off: switch light/outlet OFF
* ikea turn on: switch light/outlet ON
Commands marked with (*) are currently unavailable.
Type `help <command>` to learn more.
可以用coap://coap.me作为测试服务器,输入下列命令连接服务器:
connect coap://coap.me
然后使用discover命令查询服务器上的所有资源。
比如可以使用get /hello 命令进行GET操作。
Wireshark抓包分析
Wireshark内置了对CoAP协议的支持,我们可以使用它进行抓包分析。
从上面的抓包可以看出,discover命令实际上是查询了CoAP协议约定的.well-known/core 资源。因为返回的数据太多了,服务器分多个包返回了全部资源。
结语
使用CoAP-Shell工具结合Wireshark可以方便地分析CoAP的通信过程。