How to get client IP using Mosquitto MQTT

pekasus picture pekasus · Oct 13, 2016 · Viewed 10.4k times · Source

I would like to log the IP address of clients using Mosquitto MQTT. I am using Paho - Python on the server side and Arduino - PubSubClient on the client side.

In PHP, it is quite simple, I just use $_SERVER['REMOTE_ADDR']. Is there something similar for Mosquitto?

Answer

hardillb picture hardillb · Oct 13, 2016

Mosquitto records when new clients connect if logging is enabled:

Oct 13 15:09:32 bagend mosquitto[1361]: 1476367772: New connection from 127.0.0.1 on port 1883.
Oct 13 15:09:32 bagend mosquitto[1361]: 1476367772: New client connected from 127.0.0.1 as mosqsub/18943-bagend (c1, k60).

This shows a new client connecting from localhost and includes the client id mosqsub/18943-bagend

There is no way to get it from another MQTT client as every client has no knowledge of any other clients connected to the broker.

EDIT: The mosquitto.conf man page implies you may be able to log to a MQTT topic using the following in the config file:

log_dest topic

This logs to a topic of $SYS/broker/log/<log level>

so you get something like this when subscribing with mosquitto_sub -v -t '$SYS/broker/log/#'

$SYS/broker/log/N 1476378785: New connection from 127.0.0.1 on port 1885.
$SYS/broker/log/N 1476378785: New client connected from 127.0.0.1 as mosqpub/4654-tiefighter (c1, k60).
$SYS/broker/log/N 1476378785: Client mosqpub/4654-tiefighter disconnected.