Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.4: A shell script to do DNS lookups in the Terminal UNIX
Tiger only hintSince I'm using Mac OS X 10.4, controlling DNS lookups via the /etc/resolv.conf file (as explained in this hint) didn't work. Instead, I created this shell script, which I named setdns, to set the DNS temporarily. Remember to make the script executable (chmod 755 setdns).

Hope this helps someone else; it worked for my openvpn setup.

[robg adds: I haven't tested this one, and there's another hint which discusses non-resolv.conf methods of handling DNS that may be worth reading as well...]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[11,978 views]  

10.4: A shell script to do DNS lookups in the Terminal | 7 comments | Create New Account
Click here to return to the '10.4: A shell script to do DNS lookups in the Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: A shell script to do DNS lookups in the Terminal
Authored by: otterboy on Sep 20, '05 01:53:48PM

Or couldn't you just use the shipped utility dig?

i.e. :

bash$ dig @nameserver domain.com



[ Reply to This | # ]
10.4: A shell script to do DNS lookups in the Terminal
Authored by: prk on Sep 20, '05 04:07:19PM

or even the host utility?

host something.com ns1.somewhere.com



[ Reply to This | # ]
Wrong Tip title
Authored by: emzy on Sep 20, '05 06:13:27PM

The title is wrong, it's about setting the Nameserver setting (resolver).
Not to make a Host lookup.

Sometimes its nice to simply edit it e.g. if you using openvpn.

Maybe somone can set the Title to: Setting the Namserver on Shell

CU Emzy



[ Reply to This | # ]
10.4: A shell script to do DNS lookups in the Terminal
Authored by: TigerKR on Sep 21, '05 12:41:48AM

If you want, you can add a line to your hosts file, that later you can delete.

$ sudo echo "127.0.0.1 test.this.webserver.com" >> /etc/hosts

You should be able to see the affects of this change immediately (or you may need to close and reopen whatever internet enabled application you're using - your milage may vary).

For instance, if you're testing some web site that you're developing, you can start web sharing (apache) and then put your web development files in the apache default directory. And if you use absolute links (as opposed to relative links), they'll resolve to your machine (as long as you've set them to with the hosts file).

As always, when you're editing an important file, I recommend that you make a backup before editing the file with a command such as:

$ sudo cp /etc/hosts /etc/hosts.apple_default

If you need to go back to the original file simply execute:

$sudo cp /etc/hosts.apple_default /etc/hosts

Here's a bonus tip. If you get your IP address via DHCP and you don't want your ISP assigned hostname to show up in the terminal, you can add a line to your hosts file with your IP and whatever host name you want. Example:

$ sudo echo "10.0.0.13 rockinbox" >> /etc/hosts

And as long as 10.0.0.13 is your actual ISP assigned IP address, then your terminal will show up as:

rokinbox:~ username$

This will survive restarts, but not necessarily system updates, as the hosts file may be reformatted during a system update.



[ Reply to This | # ]
10.4: A shell script to do DNS lookups in the Terminal
Authored by: jmincey on Sep 21, '05 09:11:54AM

I'm afraid you've lost me on this. If one is using DHCP and uses no on-site router and instead gets his IP address from his ISP, then it is very unlikely to be of the 10-dot addressing format. Also, this IP address will most certainly NOT survive a restart of the computer except under very specific circumstances. Many users, depending on their connection type with their ISP, will not be assigned the same IP address.

---
Jeff Mincey



[ Reply to This | # ]
10.4: A shell script to do DNS lookups in the Terminal
Authored by: TigerKR on Sep 22, '05 01:31:58AM

>I'm afraid you've lost me on this. If one is using DHCP and uses no on-site
>router and instead gets his IP address from his ISP, then it is very unlikely to
>be of the 10-dot addressing format. Also, this IP address will most certainly
>NOT survive a restart of the computer except under very specific
>circumstances. Many users, depending on their connection type with their ISP,
>will not be assigned the same IP address.

You are correct, the 10.x.x.x range is for internal networks, however, I was using that as an example because I don't have the patience to type out every possible IP address combination. ; - )

Technically, you are also correct that most DHCP IP addresses are not guaranteed, however, many large broadband ISPs match IP addresses to MAC addresses, so your IP could change, but in reality, it never changes. For instance, my IP address on Comcast Broadband never changes, unless I use a different computer or a different network interface (ie: a second ethernet card).

In any event, my hint will not damage your computer or network connection in any way, because dns lookups start with the domain/host name first, not the IP address first. In the event that you were assigned a different IP address, my hint would simply not work, and your terminal host-name would revert to its ISP assigned host-name.

If someone else on your sub-net (another client of your ISP) got your old IP address, the worst case scenario would be that if you tried to use your /etc/hosts host-name to connect to yourself (it would never work outside of your computer because your /etc/hosts file does not exist on any other computer, nor is it published to any other computers), then you might be connected to that other computer on your sub-net, but its unlikely that anything would happen, unless your neighbor used the same software, services, usernames, and passwords a you use on your computer. ; - )

Thank you for posting your comments though, because I really should have been clearer and more explicit.

Cheers.



[ Reply to This | # ]
10.4: A shell script to do DNS lookups in the Terminal
Authored by: ar on Sep 25, '05 01:20:34AM

Depending on your VPN setup you may want to make use of MacOS X's little-known ability to use per-domain specific nameservers.

For example, if you have an OpenVPN tunnel to example.com, you can tell MacOS X to use a nameserver at the other end of the tunnel, but ONLY for example.com domain names. (This won't work if you want to resolve example.com domain names without bringing up the OpenVPN tunnel)

The secret is to create /etc/resolver/example.com. See resolver(5) for the details.



[ Reply to This | # ]