赞
踩
mosquitto是一个开源broker,并且支持MQTTv3.1,支持客户端的验证。mosquitto包括服务端和客户端。
执行mosquitto_pub --help
- mosquitto_pub --help
- mosquitto_pub is a simple mqtt client that will publish a message on a single topic and exit.
- mosquitto_pub version 1.4.8 running on libmosquitto 1.4.8.
-
- Usage: mosquitto_pub [-h host] [-k keepalive] [-p port] [-q qos] [-r] {-f file | -l | -n | -m message} -t topic
- [-A bind_address] [-S]
- [-i id] [-I id_prefix]
- [-d] [--quiet]
- [-M max_inflight]
- [-u username [-P password]]
- [--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]]
- [{--cafile file | --capath dir} [--cert file] [--key file]
- [--ciphers ciphers] [--insecure]]
- [--psk hex-key --psk-identity identity [--ciphers ciphers]]
- [--proxy socks-url]
- mosquitto_pub --help
-
- -A : bind the outgoing socket to this host/ip address. Use to control which interface
- the client communicates over.
- -d : enable debug messages.
- -f : send the contents of a file as the message.
- -h : mqtt host to connect to. Defaults to localhost.
- -i : id to use for this client. Defaults to mosquitto_pub_ appended with the process id.
- -I : define the client id as id_prefix appended with the process id. Useful for when the
- broker is using the clientid_prefixes option.
- -k : keep alive in seconds for this client. Defaults to 60.
- -l : read messages from stdin, sending a separate message for each line.
- -m : message payload to send.
- -M : the maximum inflight messages for QoS 1/2..
- -n : send a null (zero length) message.
- -p : network port to connect to. Defaults to 1883.
- -P : provide a password (requires MQTT 3.1 broker)
- -q : quality of service level to use for all messages. Defaults to 0.
- -r : message should be retained.
- -s : read message from stdin, sending the entire input as a message.
- -S : use SRV lookups to determine which host to connect to.
- -t : mqtt topic to publish to.
- -u : provide a username (requires MQTT 3.1 broker)
- -V : specify the version of the MQTT protocol to use when connecting.
- Can be mqttv31 or mqttv311. Defaults to mqttv31.
- --help : display this message.
- --quiet : don't print error messages.
- --will-payload : payload for the client Will, which is sent by the broker in case of
- unexpected disconnection. If not given and will-topic is set, a zero
- length message will be sent.
- --will-qos : QoS level for the client Will.
- --will-retain : if given, make the client Will retained.
- --will-topic : the topic on which to publish the client Will.
- --cafile : path to a file containing trusted CA certificates to enable encrypted
- communication.
- --capath : path to a directory containing trusted CA certificates to enable encrypted
- communication.
- --cert : client certificate for authentication, if required by server.
- --key : client private key for authentication, if required by server.
- --ciphers : openssl compatible list of TLS ciphers to support.
- --tls-version : TLS protocol version, can be one of tlsv1.2 tlsv1.1 or tlsv1.
- Defaults to tlsv1.2 if available.
- --insecure : do not check that the server certificate hostname matches the remote
- hostname. Using this option means that you cannot be sure that the
- remote host is the server you wish to connect to and so is insecure.
- Do not use this option in a production environment.
- --psk : pre-shared-key in hexadecimal (no leading 0x) to enable TLS-PSK mode.
- --psk-identity : client identity string for TLS-PSK mode.
- --proxy : SOCKS5 proxy URL of the form:
- socks5h://[username[:password]@]hostname[:port]
- Only "none" and "username" authentication is supported.
-
- See http://mosquitto.org/ for more information.
选项:
-d, –debug :开启debug选项
-f, –file:把一个文件的内容做为消息的内容发送。经测试,支持txt文件,不支持doc等其他形式文件。
-h, –host:说明所连接到的域名,默认是localhost
-i, –id:客户端的ID号,如果没有指定,默认是mosquitto_pub_加上客户端的进程id,不能和–id_prefix同时使用。
-I, –id-prefix :指定客户端ID的前缀,与客户端的进程ID连接组成客户端的ID,不能和–id同时使用。
-l, –stdin-line:从总段读取输入发送消息,一行为一条消息,空白行不会被发送。
-m, –message:从命令行发送一条消息,-m后面跟发送的消息内容。
-n, –null-message:发送一条空消息。
-p, –port:连接的端口号,默认是1883.
-P, –pw:指定密码用于代理认证,使用此选项时必须有有效的用户名。
-q, –qos:指定消息的服务质量,可以为0,1,2,默认是0.
–quiet:如果指定该选项,则不会有任何错误被打印,当然,这排除了无效的用户输入所引起的错误消息。
-r, –retain:如果指定该选项,该条消息将被保留做为最后一条收到的消息。下一个订阅消息者将能至少收到该条消息。
-s, –stdin-file:从标准输入接收传输的消息内容,所有输入做为一条消息发送。
-t, –topic:指定消息所发布到哪个主题。
-u, –username:指定用户名用于代理认证。
–will-payload:如果指定该选项,则万一客户端意外和代理服务器断开,则该消息将被保留在服务端并发送出去,该选项必须同时用–will-topic指定主题。
–will-qos:指定Will的服务质量,默认是0.必须和选项 –will-topic同时使用.
–will-retain:如果指定该选项,则万一客户端意外断开,已被发送的消息将被当做retained消息。必须和选项 –will-topic同时使用.
–will-topic:指定客户端意外断开时,Will消息发送到的主题。
mosquitto_sub --help
- mosquitto_sub --help
- mosquitto_sub is a simple mqtt client that will subscribe to a single topic and print all messages it receives.
- mosquitto_sub version 1.4.8 running on libmosquitto 1.4.8.
-
- Usage: mosquitto_sub [-c] [-h host] [-k keepalive] [-p port] [-q qos] [-R] -t topic ...
- [-C msg_count] [-T filter_out]
- [-A bind_address] [-S]
- [-i id] [-I id_prefix]
- [-d] [-N] [--quiet] [-v]
- [-u username [-P password]]
- [--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]]
- [{--cafile file | --capath dir} [--cert file] [--key file]
- [--ciphers ciphers] [--insecure]]
- [--psk hex-key --psk-identity identity [--ciphers ciphers]]
- [--proxy socks-url]
- mosquitto_sub --help
- -A : bind the outgoing socket to this host/ip address. Use to control which interface
- the client communicates over.
- -c : disable 'clean session' (store subscription and pending messages when client disconnects).
- -C : disconnect and exit after receiving the 'msg_count' messages.
- -d : enable debug messages.
- -h : mqtt host to connect to. Defaults to localhost.
- -i : id to use for this client. Defaults to mosquitto_sub_ appended with the process id.
- -I : define the client id as id_prefix appended with the process id. Useful for when the
- broker is using the clientid_prefixes option.
- -k : keep alive in seconds for this client. Defaults to 60.
- -N : do not add an end of line character when printing the payload.
- -p : network port to connect to. Defaults to 1883.
- -P : provide a password (requires MQTT 3.1 broker)
- -q : quality of service level to use for the subscription. Defaults to 0.
- -R : do not print stale messages (those with retain set).
- -S : use SRV lookups to determine which host to connect to.
- -t : mqtt topic to subscribe to. May be repeated multiple times.
- -T : topic string to filter out of results. May be repeated.
- -u : provide a username (requires MQTT 3.1 broker)
- -v : print published messages verbosely.
- -V : specify the version of the MQTT protocol to use when connecting.
- Can be mqttv31 or mqttv311. Defaults to mqttv31.
- --help : display this message.
- --quiet : don't print error messages.
- --will-payload : payload for the client Will, which is sent by the broker in case of
- unexpected disconnection. If not given and will-topic is set, a zero
- length message will be sent.
- --will-qos : QoS level for the client Will.
- --will-retain : if given, make the client Will retained.
- --will-topic : the topic on which to publish the client Will.
- --cafile : path to a file containing trusted CA certificates to enable encrypted
- certificate based communication.
- --capath : path to a directory containing trusted CA certificates to enable encrypted
- communication.
- --cert : client certificate for authentication, if required by server.
- --key : client private key for authentication, if required by server.
- --ciphers : openssl compatible list of TLS ciphers to support.
- --tls-version : TLS protocol version, can be one of tlsv1.2 tlsv1.1 or tlsv1.
- Defaults to tlsv1.2 if available.
- --insecure : do not check that the server certificate hostname matches the remote
- hostname. Using this option means that you cannot be sure that the
- remote host is the server you wish to connect to and so is insecure.
- Do not use this option in a production environment.
- --psk : pre-shared-key in hexadecimal (no leading 0x) to enable TLS-PSK mode.
- --psk-identity : client identity string for TLS-PSK mode.
- --proxy : SOCKS5 proxy URL of the form:
- socks5h://[username[:password]@]hostname[:port]
- Only "none" and "username" authentication is supported.
- See http://mosquitto.org/ for more information.
mosquitto_sub 订阅到主题,接收到消息时打印
选项:
-c, –disable-clean-session:禁止’clean session’选项,即如果客户端断开连接,这个订阅仍然保留来接收随后到的QoS为1和2的消息,当改客户端重新连接之后,它将接收到已排在队列中的消息。建议使用此选项时,客户端id选项设为–id
-d, –debug:开启debug选项
-h, –host:说明所连接到的域名,默认是localhost
-i, –id:客户端的ID号,如果没有指定,默认是mosquitto_pub_加上客户端的进程id,不能和–id_prefix同时使用。
-I, –id-prefix:指定客户端ID的前缀,与客户端的进程ID连接组成客户端的ID,不能喝–id同时使用。
-k, –keepalive:给代理发送PING命令(目的在于告知代理该客户端连接保持且在正常工作)的间隔时间,默认是60s
-p, –port:说明客户端连接到的端口,默认是1883
-P, –pw:指定密码用于代理认证,使用此选项时必须有有效的用户名。
-q, –qos:指定消息的服务质量,可以为0,1,2,默认是0.
–quiet:如果指定该选项,则不会有任何错误被打印,当然,这排除了无效的用户输入所引起的错误消息。
-t, –topic:指定订阅的消息主题,允许同时订阅到多个主题
-u, –username:指定用户名用于代理认证。
-v, –verbose:冗长地打印收到的消息。若指定该选项,打印消息时前面会打印主题名——“主题 消息内容”,否则,只打印消息内容
–will-payload:如果指定该选项,则万一客户端意外和代理服务器断开,则该消息将被保留在服务端并发送出去,该选项必须同时用–will-topic指定主题。
–will-qos:指定Will的服务质量,默认是0.必须和选项 –will-topic同时使用.
–will-retain:如果指定该选项,则万一客户端意外断开,已被发送的消息将被当做retained消息。必须和选项 –will-topic同时使用.
–will-topic:指定客户端意外断开时,Will消息发送到的主题。
1.连接请求(CONNECT)
当一个从客户端到服务器的TCP/IP套接字连接被建立时,必须用一个连接流来创建一个协议级别的会话。
2.连接请求确认(CONNECTACK)
连接请求确认报文(CONNECTACK)是服务器发给客户端,用以确认客户端的连接请求
3.发布报文(PUBLISH)
客户端发布报文到服务器端,用来提供给有着不同需求的订阅者们。每个发布的报文都有一个主题,这是一个分层的命名空间,他定义了报文来源分类,方便订阅者订阅他们需要的主题。订阅者们可以注册自己的需要的报文类别。
4.发布确认报文(PUBACK)
发布确认报文(PUBACK)是对服务质量级别为1的发布报文的应答。他可以是服务器对发布报文的客户端的报文确认,也可以是报文订阅者对发布报文的服务器的应答。
5.发布确认报文(PUBREC)
PUBREC报文是对服务质量级别为2的发布报文的应答。这是服务质量级别为2的协议流的第二个报文。PUBREC是由服务器端对发布报文的客户端的应答,或者是报文订阅者对发布报文的服务器的应答。
6.发布确认报文(PUBREL)
PUBREL是报文发布者对来自服务器的PUBREC报文的确认,或者是服务器对来自报文订阅者的PUBREC报文的确认。它是服务质量级别为2的协议流的第三个报文。
7.确定发布完成(PUBCOMP)
PUBCOMP报文是服务器对报文发布者的PUBREL报文的应答,或者是报文订阅者对服务器的PUBREL报文的应答。它是服务质量级别为2的协议流的第四个也是最后一个报文。
8.订阅命名的主题(SUBSCRIBE)
订阅报文(SUBSCRIBE)允许一个客户端在服务器上注册一个或多个感兴趣的主题名字。发布给这些主题的报文作为发布报文从服务器端交付给客户端。订阅报文也描述了订阅者想要收到的发布报文的服务质量等级。
9.订阅报文确认(SUBACK)
当服务器收到客户端发来的订阅报文时,将发送订阅报文的确认报文给客户端。一个这样的确认报文包含一列被授予的服务质量等级。被授予的服务质量等级次序和对应的订阅报文中的主题名称的次序相符。
10.退订命名的主题(UNSUBSCRIBE)
退订主题的报文是从客户端发往服务器端,用以退订命名的主题。
11.退订确认(UNSUBACK)
退订确认报文是从服务器发往客户端,用以确认客户端发来的退订请求报文。
12.Ping请求(PINGREQ)
Ping请求报文是从连接的客户端发往服务器端,用来询问服务器端是否还存在。
13.Ping应答(PINGRESP)
Ping应答报文是从服务器端发往Ping请求的客户端,对客户端的Ping请求进行确认。
14.断开通知(DISCONNECT)
断开通知报文是从客户端发往服务器端用来指明将要关闭它的TCP/IP连接,他允许彻底地断开,而非只是下线。如果客户端已经和干净会话标志集联系,那么所有先前关于客户端维护的信息将被丢弃。一个服务器在收到断开报文之后,不能依赖客户端关闭TCP/IP连接。
————————————————
版权声明:本文为CSDN博主「liefyuan」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_28877125/article/details/78330809
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。