Script: Using (host)name instead of IP address

Moderator: leecollings

Post Reply
BarryT
Posts: 411
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Script: Using (host)name instead of IP address

Post 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 309 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();
    }
})();
User avatar
waltervl
Posts: 7020
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2026.3
Location: NL
Contact:

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

Post 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 285 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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply