Grab domain name 10.7

Lhsachs
Contributor II

Until 10.6 you can grab the domain name you are on using cat /etc/resolv.conf It returns the domain you are on, search domains and nameservers. I am working on a script that I want to ONLY run if the user is on their home domain...

Under 10.7 cat /etc/resolv.conf doesn't return the domain you are on. Only gives you search domains and nameservers.

Does anyone know a call that will bring you the domain name you are on under 10.7?

Thanks in advance.
Lenny

10 REPLIES 10

acidprime
New Contributor III

Something like this would grab it from the domain_name option in the DHCP packet on the first active network adapter

#!/bin/bash
#set -x
declare -x ipconfig="/usr/sbin/ipconfig"
for (( N = 0 ; N <=3; N++ )) ; do
  declare DOMAIN_NAME="$($ipconfig getoption en$N domain_name 2>/dev/null)"
  [ "${#DOMAIN_NAME}" -gt 0 ] && break
done
printf "<result>%s</result>
" "$DOMAIN_NAME"

Also this should work on all Operating system versions as this is a really old command.

https://gist.github.com/2175933

Lhsachs
Contributor II

Thanks - I'll be able to hook that in...

sean
Valued Contributor

This doesn't list the domain you are on, but lists the domains in the search list.

Try

hostname | cut -d "." -f 2-

Sean

acidprime
New Contributor III

@sean well technically I guess it depends on how you define "on" as your right but /etc/resolv.conf and the information contained in the DHCP packet are the search domain.

However just a heads up "hostname" can come from multiple places, i.e. PTR record, DHCP hostname option, cached hostname store in /Library/Preferences/SystemConfiguration/preferences.plist, so technically its relative to your site configuration.

For instance if you don't receive a reverse DNS record or a hostname from DHCP it will use your cached hostname if any, thus returning your old hostname on a new network.

sean
Valued Contributor

What I'm getting at is that the methods suggested output the list of search domains, we have 4 in our company search list, it isn't necessarily just one. So the answers from resolv.conf or ipconfig will list all of these and hence could list domains that you are not connected to. If the domains are added manually and not through dhcp, then they will show up permanently. They really aren't necessarily a demonstration of the domain that you are currently connected to.

Hence why I suggested hostname. However, if you feel hostname could provide false answers, then there are other methods. You could do a host on the ip of the router address for example:

host [router ip address] | awk '{print $NF}' | cut -d "." -f 2-

I'm assuming your company router has a correct forward and reverse dns.

acidprime
New Contributor III

@sean, ah I see your confusion, domain_name is a single value option, domain_search is multiple. You are correct though that this is all coming from dhcp but not that it would ever be multivalued. Assume the original question is something like,

do something when we are on "our" domain, then then parsing this from DHCP should be fine as when your in office the packet has the info and when your not it does not. If the question is more , what current domain are you "on" , then its much trickier as even looking up the PTR record of the router would not be consistent as the network may not have one ( such as most RFC1918 private style wifi APs )

Cheers
Z

sean
Valued Contributor

@acidprime dude, not confusion, I'm reporting what I see! If I run

ipconfig getoption en0 domain_name

you'd like to think it only prints out the domain name that you are connected to, but for me it doesn't, it lists all of the items that would be seen in the search list! If you are saying that if you have multiple search domains and it only answers one, then your experience is different, but I see the equivalent to our full search list.

I realise I need to chat with our network guys about that and see if the dhcp conf on our servers is configured like that for a reason. I imagine, seeing as I believe it is just a label, to keep things simple they just provided a single title including all of our search domains, rather than tailoring each one to provide their own location/domain.

As such, it means this command is no more likely to be correct than any of the other options discussed.

For an internal only facing JSS, you could use:

jamf checkJSSConnection

If the JSS isn't available, then you aren't on the local network, unless the server is down, but that would be more concerning!!!

Lhsachs
Contributor II

Sean,

I needed to grab the domain as it became a piece of a launchd script that checks the domain you are on - if on corporate - it runs a dynamicdns registration script otherwise it just quits. The kicker is startup/login and any time any file in /Library/Preferences/SystemConfiguration/ changes.

Lenny

acidprime
New Contributor III

@sean Interesting, I thought that option (15) was typically single valued, the RFC does not seen to specify but I see a tech note from Microsoft that implies that its not.

I guess I had not noticed it before , thanks for setting me straight.

From a text parsing perspective whats that output look like ( I am admittedly too lazy to go change my isc dhcp conf at the moment )?

sean
Valued Contributor

Apparently, some *nix versions will use the 'domain-name' option to populate their search domain list! Go figure!!!

Even if that wasn't the case anymore, there is then the concern that this option has been used already in previous in-house scripts and changing it to be just the one domain you are on, instead of the entire search list, could break something!

As for the output, it is just a single line that matches whatever was typed in as the option in the dhcp common.conf file.

As it stands, there really isn't a 'correct' method, but there are better methods.

facter, puppets local binary, for example, makes a better guess by cross referencing several things in the hope to provide the correct answer. I can make it report incorrectly though.

If your local network has routers with dns names, then you could do a host on the router address.

You could try a ping against a crucial internal server (preferably servers that are in a round robin)

The list could obviously go on. It's down to the local admin to look at their setup and decide best method for them