# Name resolution

**URL:** https://discourse.nodered.org/t/name-resolution/53266
**Category:** General
**Created:** [5 November 2021 15:45 UTC](https://discourse.nodered.org/t/name-resolution/53266 "2021-11-05T15:45:48Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)
#### Post date: [5 November 2021 15:45 UTC](https://discourse.nodered.org/t/name-resolution/53266/1 "2021-11-05T15:45:48Z")

</div>

Hi,

I am busy with an action to remove all ip-adresses from my code and use hostnames that are registrated in /etc/hosts.

This works for lot of code and nodes , but not always..

Now the problem.

I have a function node that looks for the local ip and the ip of the active flow.

When the subnet matches it adresses the msg via mqtt and otherwise it sends the msg with i http command to another servr. This works for years.

But now i use host name for the localadress and dns name for http adress and i cant matched thse two anymore...  
Is there a way in NR to resolve  
a) a host as pecified in /etc/hosts  
b) dns name the main parts works ok!

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [5 November 2021 16:03 UTC](https://discourse.nodered.org/t/name-resolution/53266/2 "2021-11-05T16:03:49Z")

</div>

I don't fully understand what you are trying to do. Can you give a real example of what you are trying to match against what?

---

<div class="post-metadata">

### Author: ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)
#### Post date: [7 November 2021 11:24 UTC](https://discourse.nodered.org/t/name-resolution/53266/3 "2021-11-07T11:24:05Z")

</div>

Yes i can!

I have a working flow that send msg to my nest hub (images).  
You have to put these images first on a webserver.  
You have to tell the nest hub where to find the images. That works great with for example  
msg.ip\_websvr = [http://192.168.20.10/etc](http://192.168.20.10/etc) (see part of my msg (flow) below

But i want alle my used internal ip-adressen used by HOSTNAME which i declare in /etc/host in de docker or at the etc of the host, this works perfect voor 99% of my flows and other software.  
BUT, my nest hub does not except local hostnames because it cannot resove it  
msg.ip\_websvr = [http://domserver/etc](http://domserver/etc)

So i have to translate the hostname in msg.ip\_websvr before sending it to the nesthub !

```auto
 msg = 
    {
    ip:msg.ip_nest,
    volume: x,
    url: msg.ip_websvr + msg.webnaam,
    contentType: "image/jpg",
    //imageUrl 
    message: "",
    language: 'nl'
    };
    }

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [7 November 2021 18:59 UTC](https://discourse.nodered.org/t/name-resolution/53266/4 "2021-11-07T18:59:39Z")

</div>

You could parse the result of the `nslookup` command, using an exec node, or you could use the [nslookup](https://www.npmjs.com/package/nslookup) module.

Or you read and parse `/etc/hosts`

---

<div class="post-metadata">

### Author: ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)
#### Post date: [8 November 2021 15:27 UTC](https://discourse.nodered.org/t/name-resolution/53266/5 "2021-11-08T15:27:41Z")

</div>

Could be a solution as the nodered docker node supports grep in the docker..  
When i use this i get a line with the correct IP  
grep domserver /etc/hosts | grep -v "\_"

now get this value in NR and strip it...

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [8 November 2021 15:40 UTC](https://discourse.nodered.org/t/name-resolution/53266/6 "2021-11-08T15:40:56Z")

</div>

You could read the file in node red and parse it there, if that is easier.

---

<div class="post-metadata">

### Author: ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)
#### Post date: [8 November 2021 15:54 UTC](https://discourse.nodered.org/t/name-resolution/53266/7 "2021-11-08T15:54:21Z")

</div>

will try that too 🙂

this what i got for now: and it gives the right record.

1. Only have to strip the hostname from it...  
Searching javascript for it...  
outcome:  
192.168.20.48 domserver  
instead of 192.168.20.48

```auto
var searchitem = "domserver"
var cmd = "grep " + searchitem + " /etc/hosts "

if (searchitem.includes("_") === false)
 {
   cmd = cmd + " | grep -v " + '"_"'
 }

msg.payload= cmd
return msg;

```

---

<div class="post-metadata">

### Author: ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)
#### Post date: [8 November 2021 16:31 UTC](https://discourse.nodered.org/t/name-resolution/53266/8 "2021-11-08T16:31:22Z")

</div>

and the code to remove the tab

```auto
test = msg.payload
myArray = test.split('\t')
msg.a= myArray[0]
msg.b= myArray[1]
return msg;

```

---

<div class="post-metadata">

### Author: ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)
#### Post date: [9 November 2021 10:33 UTC](https://discourse.nodered.org/t/name-resolution/53266/9 "2021-11-09T10:33:47Z")

</div>

Problems solved!  
I now can resolve an ipadres from a string with an hostname in it...  
I had to use a smaal flow for it, and it would be more perfect if it was one procedure which i can reuse..

But, very hapy for now!

for those who can used it...  
And thanks for the help!

[json.txt](https://discourse.nodered.org/uploads/short-url/cNlA4Q5ULF9mxlAoS8hJ66q3xAf.txt) (2.2 KB)

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [9 November 2021 12:57 UTC](https://discourse.nodered.org/t/name-resolution/53266/10 "2021-11-09T12:57:13Z")

</div>

Personally, I would do the hosts process when node-red starts and then maybe periodically - not doing it when you want to process the address - unless your hosts file is changing a lot.

---

<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: [8 January 2022 12:58 UTC](https://discourse.nodered.org/t/name-resolution/53266/11 "2022-01-08T12:58:02Z")

</div>

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