# How can I retrieve IP address from host?

**URL:** <https://discourse.nodered.org/t/how-can-i-retrieve-ip-address-from-host/36660>\
**Category:** General\
**Created:** [28 November 2020 09:04 UTC](https://discourse.nodered.org/t/how-can-i-retrieve-ip-address-from-host/36660 "2020-11-28T09:04:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kgalilio](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kgalilio/32/31512_2.png) [@kgalilio](https://discourse.nodered.org/u/kgalilio)\
**Post date:** [28 November 2020 09:04 UTC](https://discourse.nodered.org/t/how-can-i-retrieve-ip-address-from-host/36660/1 "2020-11-28T09:04:38Z")

</div>

Hi,  
I am trying to obtain an IP address from a host. The available Ping nodes does not seem to return the IP address, so I tried the EXEC node with the following command:

```auto
nslookup myhost.com

```

This worked, but the object returned is not well formatted, so I could not extract the IP address easily.  
The complete msg object looks like this:

```auto
{"_msgid":"ef79c141.ca169","payload":"Address:\t11.22.33.44:53\n\nNon-authoritative answer:\nNon-authoritative answer:\nName:\tmyhost.com\nAddress: 11.22.33.44\n\n"}

```

I appreciate if anyone could point me in the right direction. I just need the ip address from this whole message.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 November 2020 09:16 UTC](https://discourse.nodered.org/t/how-can-i-retrieve-ip-address-from-host/36660/2 "2020-11-28T09:16:28Z")

</div>

> [@kgalilio](#):
>
> point me in the right direction.

You could (in a function node) use a [regular expression](https://stackoverflow.com/questions/32689442/extract-ip-address-from-a-string-using-regex) or the string [split](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) method.

---

<div class="post-metadata">

**Author:** ![kgalilio](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kgalilio/32/31512_2.png) [@kgalilio](https://discourse.nodered.org/u/kgalilio)\
**Post date:** [28 November 2020 09:29 UTC](https://discourse.nodered.org/t/how-can-i-retrieve-ip-address-from-host/36660/3 "2020-11-28T09:29:37Z")

</div>

Thanks @Steve-Mcl, but I am trying to keep it simple.  
I tried the following EXEC command and it seems to work so far:

```auto
getent hosts myhost.com | awk '{ print $1 }'

```

The message payload contains the IP address only, which is what I wanted.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 November 2020 09:40 UTC](https://discourse.nodered.org/t/how-can-i-retrieve-ip-address-from-host/36660/4 "2020-11-28T09:40:10Z")

</div>

> [@kgalilio](#):
>
> but I am trying to keep it simple.

That wasn't your question though, you asked...

> [@kgalilio](#):
>
> I just need the ip address from this whole message

Hey ho, all well that ends well.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [28 November 2020 09:54 UTC](https://discourse.nodered.org/t/how-can-i-retrieve-ip-address-from-host/36660/5 "2020-11-28T09:54:05Z")

</div>

If you are making less than 1,000 calls per month, you could use a free [https://geo.ipify.org/docs](https://geo.ipify.org/docs) account.  
I've used them for years, and very reliable.

A GET call; `https://geo.ipify.org/api/v1?apiKey=at_EHuas86RK4ujhduhsdssffb0JF9&domain=bbc.co.uk`

returns a `payload.ip` of `"151.101.0.81"` plus lots of other information if you need/want it.

---

<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:** [12 December 2020 09:54 UTC](https://discourse.nodered.org/t/how-can-i-retrieve-ip-address-from-host/36660/6 "2020-12-12T09:54:11Z")

</div>

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