# How to get IP from bash command

**URL:** <https://discourse.nodered.org/t/how-to-get-ip-from-bash-command/31792>\
**Category:** General\
**Created:** [20 August 2020 16:53 UTC](https://discourse.nodered.org/t/how-to-get-ip-from-bash-command/31792 "2020-08-20T16:53:27Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![hugocoolens](https://avatars.discourse-cdn.com/v4/letter/h/e95f7d/32.png) [@hugocoolens](https://discourse.nodered.org/u/hugocoolens)\
**Post date:** [20 August 2020 16:53 UTC](https://discourse.nodered.org/t/how-to-get-ip-from-bash-command/31792/1 "2020-08-20T16:53:28Z")

</div>

I'm trying to get the ip-adres of the raspberry pi which my node-red server is running on (it has Rhasspy running on it too, so I'll be able to ask for it, but that is not the issue).  
I use this bash-command:  
/sbin/ip -4 -o addr show dev eth0| /usr/bin/awk '{split($4,a,"/");print a[1]}'

which runs fine in the linux-terminal  
However when I put it in an exec-node in node-red, it no longer works.  
This is my flow:

```auto
ty[{"id":"aa7ebe61.f0a9f","type":"tab","label":"WhatsMyIp","disabled":false,"info":""},{"id":"507071fb.dfcf78","type":"debug","z":"aa7ebe61.f0a9f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":120,"wires":[]},{"id":"4c8ca89e.31f138","type":"exec","z":"aa7ebe61.f0a9f","command":"/sbin/ip -4 -o addr show dev eth0| /usr/bin/awk '{split($4,a,\"/\");print a[1]}'","addpay":true,"append":"","useSpawn":"false","timer":"20","oldrc":false,"name":"GetIp","x":310,"y":160,"wires":[["507071fb.dfcf78"],["988934e3.be1e48"],["a82d10e4.9c853"]]},{"id":"988934e3.be1e48","type":"debug","z":"aa7ebe61.f0a9f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":160,"wires":[]},{"id":"a82d10e4.9c853","type":"debug","z":"aa7ebe61.f0a9f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":200,"wires":[]},{"id":"db06641a.216ec8","type":"inject","z":"aa7ebe61.f0a9f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":260,"wires":[["4c8ca89e.31f138"]]}]pe or paste code here

```

I resorted to this approach because the node-red-contrib-get-my-ip fails to install.

kind regards,  
Hugo

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [20 August 2020 17:01 UTC](https://discourse.nodered.org/t/how-to-get-ip-from-bash-command/31792/2 "2020-08-20T17:01:43Z")

</div>

you need to untick the append msg.payload option...

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [20 August 2020 17:01 UTC](https://discourse.nodered.org/t/how-to-get-ip-from-bash-command/31792/3 "2020-08-20T17:01:46Z")

</div>

Couldn’t you also just use `hostname -I` in the exec node instead?

Johannes

---

<div class="post-metadata">

**Author:** ![LosinIt](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@LosinIt](https://discourse.nodered.org/u/LosinIt)\
**Post date:** [20 August 2020 17:08 UTC](https://discourse.nodered.org/t/how-to-get-ip-from-bash-command/31792/4 "2020-08-20T17:08:05Z")

</div>

> [@hugocoolens](#):
>
> node-red-contrib-get-my-ip fails to install.

I tried it and it installs fine with the Manage palette menu option. Works well, too, but I deleted it because the doc is non-existent and I don't trust it.

---

<div class="post-metadata">

**Author:** ![LosinIt](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@LosinIt](https://discourse.nodered.org/u/LosinIt)\
**Post date:** [20 August 2020 17:10 UTC](https://discourse.nodered.org/t/how-to-get-ip-from-bash-command/31792/5 "2020-08-20T17:10:06Z")

</div>

> [@JGKK](#):
>
> hostname -I

That has to be a lowercase "-i" on my systems, which are Arch Linux. In my case it just returns "127.0.0.1 127.0.0.1" which is probably not what @hugocoolens wants.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [20 August 2020 17:16 UTC](https://discourse.nodered.org/t/how-to-get-ip-from-bash-command/31792/6 "2020-08-20T17:16:00Z")

</div>

Works on raspberry pi os and is actually also in [their documentation](https://www.raspberrypi.org/documentation/remote-access/ip-address.md).  
The first one returned is the ip for me on all my pis.

---

<div class="post-metadata">

**Author:** ![hugocoolens](https://avatars.discourse-cdn.com/v4/letter/h/e95f7d/32.png) [@hugocoolens](https://discourse.nodered.org/u/hugocoolens)\
**Post date:** [20 August 2020 17:49 UTC](https://discourse.nodered.org/t/how-to-get-ip-from-bash-command/31792/7 "2020-08-20T17:49:06Z")

</div>

> > you need to untick the append msg.payload option...

Thanks for this suggestion. Now I get an ip-adress, the strange thing is however, it is not the same one as the one I get when giving the same command in a linux-shell on the raspberry. Could this behaviour be caused by the fact I am running node-red in a docker-container?

kind regards,  
hugo

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [20 August 2020 17:51 UTC](https://discourse.nodered.org/t/how-to-get-ip-from-bash-command/31792/8 "2020-08-20T17:51:25Z")

</div>

Yes you will get the internal ip of the container

---

<div class="post-metadata">

**Author:** ![hugocoolens](https://avatars.discourse-cdn.com/v4/letter/h/e95f7d/32.png) [@hugocoolens](https://discourse.nodered.org/u/hugocoolens)\
**Post date:** [20 August 2020 17:52 UTC](https://discourse.nodered.org/t/how-to-get-ip-from-bash-command/31792/9 "2020-08-20T17:52:32Z")

</div>

hostname -I (uppercase) gives me 5 !!! ip-addresses.  
Any idea where those are coming from?

kind regards,  
hugo

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [20 August 2020 18:18 UTC](https://discourse.nodered.org/t/how-to-get-ip-from-bash-command/31792/10 "2020-08-20T18:18:09Z")

</div>

From the networking layer :-). Docker sets up several. Plus the host, etc.

---

<div class="post-metadata">

**Author:** ![hugocoolens](https://avatars.discourse-cdn.com/v4/letter/h/e95f7d/32.png) [@hugocoolens](https://discourse.nodered.org/u/hugocoolens)\
**Post date:** [20 August 2020 18:23 UTC](https://discourse.nodered.org/t/how-to-get-ip-from-bash-command/31792/11 "2020-08-20T18:23:42Z")

</div>

> Yes you will get the internal ip of the container

Any idea how to get the one from the raspberry itself using this command?

kind regards,  
Hugo

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [20 August 2020 18:29 UTC](https://discourse.nodered.org/t/how-to-get-ip-from-bash-command/31792/12 "2020-08-20T18:29:49Z")

</div>

The whole point of containers is to be isolated from their hosts. Simplest is don’t run in a container. But if you are browsing to node-red presumably you know it anyway ?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [19 October 2020 18:29 UTC](https://discourse.nodered.org/t/how-to-get-ip-from-bash-command/31792/13 "2020-10-19T18:29:56Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
