Determine LOCAL IP address

Hi All,

I am trying to determine my Local/Client side IP address from within a flow in Node Red...

For the sake of clarity, by using an Exec Node with the command "ifconfig", I can determine the "Server" IP address that my current instance is connected to, but this is not what I am looking for...

Reason: I wish to switch to a particular dashboard tab on a specific client whenever that particular client logs in... This is to make a little wall panel that will be running an info screen by default, without a user having to prod around whenever the panel is turned on...

Regds
Ed

If I understand correctly you need the ip adress of the client connected to your nodered instance like dahboard.
You can use "UI Control" node to see an event when a client connects to your instance and ip adress of the client, hope this helps

Yes, UIControl will give me the address of whoever is accessing the server at any given access instant... But I need the IP address of the machine I am accessing the server from, locally....not the ip address of a machine that is accessing the NR server elsewhere on the network...

What exactly do you mean by that, and how will you know that the client has logged in?

If all you want is that the browser on the panel goes to the right tab when is is started then you just need to use the IP address of the device running node-red, with the appropriate tab url. Set that as the Home page in the browser. Configure your network so that is a fixed address and use that. Or if your network allocates names to connected devices then use that name.

Let me be more specific:

I go to a random machine on my network.
My local IP address of the client I am at is 192.168.0.123
I open browser and go to my node red dash.
On accessing the dash, Node red must recognise that I am accessing the dashboard from ip 123 and instead of going to the first tab on the list, it must go to a specific tab later down the list....
Once there, I can navigate to whatever tab etc I want...

Edit: Due to the vagaries of an old bit of hardware, coupled to a not so compatible browser, the specific link to a tab in the address line does not work... It always, on this bit of hardware, takes me to the first tab on the list. ui control does work, however, so using that node I can flip across to another tab programatically....

That is the client address, so as suggested by @umtksa you should be able to get it from msg.socketip in the ui control node.

I can, but how can I be certain that the socketip as referenced by uicontrol node is the ip address of the local client I am at? All I want is the ip address of the machine I am sitting at as allocated by dhcp/fixed ip as the local machine is set up...

Because otherwise there is a bug in the ui_control node. It gives you the ip of the machine that triggered the node to generate the message. It also gives you a socket id that you can use to address that particular instance of the dashboard.

ok... back to basics...

Simple question: How do I determine the local ip address of the client I am sitting at?

You could open a command window and run ip addr or whatever the equivalent windows command is. That may give you multiple addresses, because you may have multiple network interfaces, which will have different ip addresses. Also you will likely get both ipv4 and ipv6 addresses.

And to do that from within node red?

If you open the browser and navigate to a node-red dashboard, where the node-red flow has a ui_control node configured, then the ui_control node will emit a message containing a socketip (which is your ip) and a socketid. If the node-red flow extracts the ip and sends it to a text node then the text node will display your ip. If mulitple users (or browser windows) may be connected at the same time then include msg.socketid in the message sent to the text node and only your dashboard will be updated.

1 Like

If you can use uibuilder, any client connection reports its IP address as it connects and you can report it in every received msg if you like.

Example connect msg:

{
    "uibuilderCtrl": "client connect",
    "from": "server",
    "_socketId": "0GT7GXzuCXkjQrErAAAR",
    "version": "6.5.0-esm.min",
    "ip": "::ffff:127.0.0.1",
    "clientId": "eD15kleZnkV8To1GMEgsd",
    "tabId": "t198026",
    "url": "ghost",
    "pageName": "index.html",
    "urlParams": {},
    "connections": 0,
    "lastNavType": "reload",
    "tls": false,
    "connectedTimestamp": "2023-08-12T16:11:57.354Z",
    "referer": "http://127.0.0.1:3001/ghost/",
    "recovered": false,
    "_msgid": "15e8582ca3571d82"
}

You also get a client UUID which remains constant for the browser-profile/device until the browser is closed down completely.

The IP has reported localhost here because the browser and the server are on the same device.

Unfortunately that bit isn't working for me. It still sends it to all browser sessions connected. I believe it does work for switching tabs though, which I gather is what you want to do.

[{"id":"4997df89231bd0db","type":"ui_ui_control","z":"bdd7be38.d3b55","name":"","events":"all","x":400,"y":2020,"wires":[["32c211e1cf5feedc","e1442b05ca033b3e"]]},{"id":"32c211e1cf5feedc","type":"debug","z":"bdd7be38.d3b55","name":"debug 98","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":620,"y":2080,"wires":[]},{"id":"3a0a4688b07f515b","type":"ui_text","z":"bdd7be38.d3b55","group":"a8e4a3.d31edb6","order":2,"width":0,"height":0,"name":"","label":"ip","format":"{{msg.payload}}","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":750,"y":1980,"wires":[]},{"id":"e1442b05ca033b3e","type":"change","z":"bdd7be38.d3b55","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"socketip","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":1980,"wires":[["3a0a4688b07f515b"]]},{"id":"a8e4a3.d31edb6","type":"ui_group","name":"Chart","tab":"8032fe72.980b98","order":3,"disp":false,"width":"12","collapse":false,"className":""},{"id":"8032fe72.980b98","type":"ui_tab","name":"PnH","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Hi,

Try this in an Exec node:

netstat -tn 2>/dev/null | grep :1880 | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head

See: https://mkyong.com/linux/list-all-ip-addresses-connected-to-your-server/

Perhaps this will do, what you want.

Regards

@eddee54455

This flow should do what you need, just change the tab setup in the change node as required.

The change tab message will be sent to the browser session on 192.168.0.123 only.

[
    {
        "id": "de3eb7b867209e03",
        "type": "ui_ui_control",
        "z": "be14baae.1728",
        "name": "",
        "events": "connect",
        "x": 750,
        "y": 3285,
        "wires": [
            [
                "3b69d46dd8fca431"
            ]
        ]
    },
    {
        "id": "840965616334fbf2",
        "type": "change",
        "z": "be14baae.1728",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "2",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1085,
        "y": 3285,
        "wires": [
            [
                "de3eb7b867209e03"
            ]
        ]
    },
    {
        "id": "3b69d46dd8fca431",
        "type": "switch",
        "z": "be14baae.1728",
        "name": "",
        "property": "socketip",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "192.168.0.123",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 920,
        "y": 3285,
        "wires": [
            [
                "840965616334fbf2"
            ]
        ]
    }
]

Thanks to all!

This was the solution I came up with late last night:

[
    {
        "id": "f3daf1e039287aec",
        "type": "ui_ui_control",
        "z": "e721b05f18e1dd1d",
        "g": "0dc2fac96ee774f9",
        "name": "",
        "events": "connect",
        "x": 330,
        "y": 50,
        "wires": [
            [
                "a78a7395f2b99865"
            ]
        ]
    },
    {
        "id": "a78a7395f2b99865",
        "type": "function",
        "z": "e721b05f18e1dd1d",
        "g": "0dc2fac96ee774f9",
        "name": "Chk for Panel IP",
        "func": "let InfoPanel1 = global.get(\"InfoPanel1IP\", \"file\") \nlet InfoPanel2 = global.get(\"InfoPanel2IP\", \"file\")\nlet InfoPanel3 = global.get(\"InfoPanel3IP\", \"file\")\nlet InfoPanel4 = global.get(\"InfoPanel4IP\", \"file\")\nlet InfoPanel5 = global.get(\"InfoPanel5IP\", \"file\")\n\nif (msg.socketip == InfoPanel1 || msg.socketip == InfoPanel2 || msg.socketip == InfoPanel3\n    || msg.socketip == InfoPanel4 || msg.socketip == InfoPanel5){\n    msg.payload = { \"tab\": \"KT Status\" }\n    return msg\n}\nreturn",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 490,
        "y": 50,
        "wires": [
            [
                "efbc6cda3b7e5687"
            ]
        ]
    },
    {
        "id": "efbc6cda3b7e5687",
        "type": "delay",
        "z": "e721b05f18e1dd1d",
        "g": "0dc2fac96ee774f9",
        "name": "",
        "pauseType": "delay",
        "timeout": "6",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 650,
        "y": 50,
        "wires": [
            [
                "f3daf1e039287aec"
            ]
        ]
    }
]

very similar to @smcgann99 solution...

It works, but is a little iffy on older hardware with slow wifi connections... The reason I think it is dodgy is that the time taken for the login / execution begin / switch to tab, allows the switch to new tab to execute before all tabs are "loaded"....Whereupon it sits at the default start tab, having "missed" the switch tab instruction... Inserting a 6 second delay slows the switch tab command to "play catch up" and activate once the rest of the tabs are lined up....

I think a better way to do it would be for the flow to actually determine the local IP of the client it is running on and do the switching/processes required based on the local ip address of the client, without relying on server connect signal etc - A downfall, I think, of the current method, will be that should the client disconnect and reconnect due to a dropped wifi signal, the switch command will throw the screen out of where it is and back to the "base" tab allocated....

Regds
Ed

The flow is not running on the client, it is running on the server. Until the client connects, node red knows nothing about the client.

Sorry....Slip of the tongue....I am aware its running on the server...I should have said: determine the local IP of the client that is accessing it via a client side interrogation, not based on the server communications in progress...

Do you mean that you want to run some code in the browser that determines the ip address, and then when it connects with node-red it sends the ip to node red?