pekasus
pekasus

Reputation: 646

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?

Upvotes: 4

Views: 20671

Answers (2)

Shankara Narayana
Shankara Narayana

Reputation: 765

I just set the IP as the client ID. Be careful with this though. If anyone has taken up this client ID already, you will get disconnected with reason code 1.

Alternatively, you can use the user data field.

Upvotes: 0

hardillb
hardillb

Reputation: 59816

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.

Upvotes: 7

Related Questions