Page 1 of 1

Script: Using (host)name instead of IP address

Posted: Monday 03 August 2026 17:28
by BarryT
Hi,

I wrote a small script that displays the name of a connected user (and the user who switched a device) instead of only showing the IP address in the Domoticz log.

Because we have many users on our local network who use the default local account, it was sometimes difficult to identify who actually connected to Domoticz or operated a device.

To use it, simply add the script to your custom.js file. After configuring the static IP addresses and their corresponding names, Domoticz will display the assigned name together with the IP address in the log.

It might be useful for others as well. 🙂
domo4.png
domo4.png (6.96 KiB) Viewed 641 times

Code: Select all

(function () {
    "use strict";

    const clientNames = {
      "127.0.0.1": "localhost",
      "192.168.1.3": "localdocker",
      "192.168.1.39": "BarryMuziekPC",
      "10.8.0.2": "myvpn",
      "10.8.0.3": "mygirlfriendvpn",
      "10.8.0.4": "othervpn",
      "192.168.1.90": "user1",
      "192.168.1.91": "otheruser2",
      "192.168.1.92": "otheruser3",
      "192.168.1.93": "otheruser4"
    };

    const sortedClients = Object.entries(clientNames)
        .sort((a, b) => b[0].length - a[0].length);
    function escapeRegex(value) {
        return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
    }

    function replaceClientIPs(root) {
        if (!root) return;
        const walker = document.createTreeWalker(
            root,
            NodeFilter.SHOW_TEXT
        );

        const textNodes = [];
        while (walker.nextNode()) {
            textNodes.push(walker.currentNode);
        }

        for (const node of textNodes) {
            let text = node.nodeValue;
            for (const [ip, name] of sortedClients) {
                const escapedIp = escapeRegex(ip);
                const replacement = `${name} (${ip})`;
                const mappedRegex = new RegExp(
                    `::ffff:${escapedIp}(?!\\d)`,
                    "g"
                );

                const ipv4Regex = new RegExp(
                    `(?<![\\d.])${escapedIp}(?![\\d.])`,
                    "g"
                );
                text = text.replace(mappedRegex, replacement);
                text = text.replace(ipv4Regex, replacement);
            }

            if (text !== node.nodeValue) {
                node.nodeValue = text;
            }
        }
    }

    function start() {
        replaceClientIPs(document.body);

        const observer = new MutationObserver((mutations) => {
            for (const mutation of mutations) {
                for (const addedNode of mutation.addedNodes) {
                    if (addedNode.nodeType === Node.TEXT_NODE) {
                        replaceClientIPs(addedNode.parentNode);
                    } else if (addedNode.nodeType === Node.ELEMENT_NODE) {
                        replaceClientIPs(addedNode);
                    }
                }
            }
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }

    if (document.readyState === "loading") {
        document.addEventListener("DOMContentLoaded", start);
    } else {
        start();
    }
})();

Re: Script: Using (host)name instead of IP address

Posted: Monday 03 August 2026 19:59
by waltervl
Isnt the user shown in the switch log (not the main log) when you click on the log butoon on a switch?
Then you do not need to change the js file.
Screenshot_20260803-200215_Chrome.png
Screenshot_20260803-200215_Chrome.png (249.47 KiB) Viewed 617 times

And when you think it is a useful change for everyone please submit a PR on the GitHub repository. It will then become implemented for everyone.