# [SOLVED] Howto different "resolv.conf" for specific script.

## luispa

Hi, 

I'm in a test environment where I need a bash script (Program-A) use the standard resolving mechanism /etc/resolv.conf file, and would like to have a different script (Program-B) use a different dns servers (like forcing it to use a different resolv.conf file). 

Beyond setting up a chroot environment or virtual box, do you know of any tip that may help?

Thanks in advance, 

[/glep]Luis[glep=]Last edited by luispa on Sat Mar 22, 2008 10:17 am; edited 1 time in total

----------

## toralf

man resolv.conf:

```
      The options keyword of a system's resolv.conf file can be amended on a per-process basis by setting  the

       environment  variable RES_OPTIONS to a space-separated list of resolver options as explained above under

       options.

```

----------

## luispa

 *toralf wrote:*   

> man resolv.conf:
> 
> ```
>       The options keyword of a system's resolv.conf file can be amended on a per-process basis by setting  the
> 
> ...

 

toralf,

thank you for your response, unfortunately it doesn't help. Maybe I explain incorrectly. What I need to have is whole system use current /etc/hosts + /etc/resolv.conf, and one process (bash-script) use a different /etc/hosts (i.e. /etc/hosts.alternative) + same /etc/resolv.

Example:

/etc/hosts

 129.129.129.129  mybox.mydomain.com   <= This IP is not the same as if it would be resolved by my nameserver.

/etc/resolv.conf

 domain mydomain.com

 nameserver 123.231.123.231

SYSTEM:  Will work as normal:

 $ resolveip mybox.mydomain.com

 IP address of mybox.mydomain.com is 129.129.129.129

myscript.sh: Here is were I need the EXCEPTION to "ignore the entry under /etc/hosts".

 resolveip mybox.mydomain.com

 should return the real IP returned by the dns server: 

 > IP address of mybox.mydomain.com is 123.231.0.1

I've found a way of doing it creating a chrooted environment, but it's costing me "712M" of wasted space.

I've been looking at nsswitch.conf options, but can find a way to do it.

Any other idea would be of help. 

Thanks again

Luis

----------

## richard.scott

Try this:

```
#!/bin/bash

RES_NAMESERVERS="127.0.0.1"

ping hostname
```

I found it here:

http://www.nihongo.org/snowhare/utilities/perldoc2tree/example/Net/DNS/Resolver.html#RES_NAMESERVERS

Be aware tho that some commands will always use the /etc/resolv.conf file. For example if you use dig you would need to do the following:

```
dig @127.0.0.1 hostname
```

Hope this helps.

----------

## luispa

 *richard.scott wrote:*   

> Try this:
> 
> ```
> #!/bin/bash
> 
> ...

 

Thanks richard.scott,

Unfortunately it doesn't work as "/etc/hosts" has more priority (as per nsswitch.conf). Still goes to /etc/hosts first. 

I would like to have something like USEDIFFERENTHOSTSFILE=/xx/xxx  or  USEDIFFERENTNSSWITCH=/xxx/xxxx     :Smile: 

Thanks again

Luis

----------

## richard.scott

If you have nsswitch.conf setup to point to the hosts file first then there will be no way of referencing a different /etc/hosts file for each script.

Why not just hard code the entry's into a new file like /etc/hosts.test

example /etc/hosts.test content:

```
HOSTA="192.168.0.1"

HOSTB="192.168.0.2"
```

and then in the test script use this:

```
#!/bin/bash

source /etc/hosts.test

ping ${HOSTA}
```

That way each script could have different settings.

or if you like, in each script use the following to work out its name:

```
SCRIPT_NAME="$(basename $0)"
```

Then use this to load a specific file for that script:

```
source /etc/hosts.${SCRIPT_NAME}
```

That way for a script called "test" it would load /etc/hosts.test from the above code.

----------

## luispa

Thanks richard,

Unfortunately that tip doesn't work either. The reason: I'm running a complex network probe script that is testing 2 x load balance boxes, which expects to receive querys with the full name and the url, as in "test (protocol://myhost.mydomain.com" (protocols vary http, https, ftp). The script is getting the host names from a list of 45 being tested through the load balance box. I'm using wget to test, and it uses the dns names.

First load balance box is used when resolving through /etc/hosts (so hardcoded IP's), but still need to use the dns name in the wget query.

Second load balance box will be used when "ignoring /etc/hosts" and getting dns served IP's, again need to use the dns name in the wget query.

For first one I just modified /etc/hosts and run the script_1. For the second case I setup a chroot environment with an empty /etc/hosts an run script_2.

What I wanted is to void chrooting to save space. 

As you said i seems impossible to have like a /etc/hosts.test tied to a bash session for example. So everything run from there will use it. Something like NS_USE_HOSTS=/etc/hosts.test   :Smile: 

Thanks again for your help. 

Luis

----------

## richard.scott

ah, I see.

Have you seen the Nagios project? 

http://www.nagios.org/

They have some plugins that may help here:

```
emerge -pv net-analyzer/nagios-plugins
```

They have a plugin called check_http that works as follows:

```
/usr/nagios/libexec/check_http -H www.google.co.uk -I 66.249.93.99 --string "google"
```

the plugin returns an error code that is available via the "$?" bash variable so doing this would record it:

```
#!/bin/bash

/usr/nagios/libexec/check_http -H www.google.co.uk -I 66.249.93.99 --string "google"

RESULT=$?

echo "Result: ${RESULT}"

```

You can check for a different virtual host by changing the "-H" variable, or the IP checked by changing the -I

Also, you can change what text to expect in the response by changing the "--string" variable.

This may help you write a custom script to check your load balancers are ok.

----------

## luispa

Very interesting... 

I'll take a look and study the scripts. The option to pass the IP address is what I need. 

Thanks a lot. 

I mark the thread as solved. 

Luis

----------

## richard.scott

ok. PM me if you need any help.

Rich

----------

## luispa

Thanks again Richard,

Here is the howto:

Objective: From the same program (script) tests HTTP through two different Servers using the same URL. Each server has its own IP but same "dns name", so there is a need of "bypassing" both /etc/hosts and DNS resolver.

Howto: Use NAGIOS plugins which are able to bypass dns and hosts.

```

[..]

 while [ 1 ] 

 do 

     :

     # Check service through IP #1 

     /usr/nagios/libexec/check_http -v -t 3 -p 80 -H www.myweb.com -I 111.111.111.1 -f follow --string "Expected String"

     :

     # Check same service through IP #2

     /usr/nagios/libexec/check_http -v -t 3 -p 80 -H www.myweb.com -I 222.222.222.2 -f follow --string "Expected String"

    :

[..]

```

Regards,

Luis

----------

