Posted on 03-23-2012 03:17 PM
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
Posted on 03-23-2012 03:43 PM
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.
Posted on 03-23-2012 04:07 PM
Thanks - I'll be able to hook that in...
Posted on 03-24-2012 11:04 AM
This doesn't list the domain you are on, but lists the domains in the search list.
Try
hostname | cut -d "." -f 2-
Sean
Posted on 03-26-2012 11:51 AM
@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.
Posted on 03-26-2012 04:03 PM
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.
Posted on 03-28-2012 06:07 PM
@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
Posted on 03-29-2012 04:13 PM
@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!!!
Posted on 03-30-2012 03:29 PM
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
Posted on 03-31-2012 01:43 PM
@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 )?
Posted on 04-04-2012 03:33 AM
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