Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get client IP using Mosquitto MQTT

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?

like image 524
pekasus Avatar asked Nov 04 '25 12:11

pekasus


1 Answers

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.
like image 57
hardillb Avatar answered Nov 06 '25 04:11

hardillb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!