Resource Kit, DNS and Search Domain scripts

RobertHammen
Valued Contributor II

I haven't looked at the Resource Kit scripts recently, however I do know you can specify multiple DNS servers in the networksetup command line (at least in 10.6):
On Feb 23, 2011, at 3:04 PM, Daniel Behan wrote:

networksetup -setdnsservers (interface) 172.16.25.1 8.8.8.8 8.8.4.4

et. al.

7 REPLIES 7

jkruse
New Contributor

I'm looking for how to do this too.

Chris
Valued Contributor

for search domains it would be

networksetup -setsearchdomains (interface) firstdomain.mycompany.net seconddomain.mycompany.net thirddomain.mycompany.net

for 10.6 and 10.7

twrangham
New Contributor III

Ok, to expand on this. Is there a way to set multiple search domains for multiple or ALL network services? I want to set 3 searchDomains for any and all Network adapters on a Mac. Either by Managed Preference or script, although the latter seems more likely. Thanks!

rmanly
Contributor III

Something like this could do it.

#!/bin/bash

while read iface; do
    networksetup -setsearchdomains "${iface}" foo.bar.net foo.baz.net
done < <(networksetup -listallnetworkservices | grep -v '*')

EDIT to the EDIT: there was an error here in the edit for years…not a problem with the script but about running this for Inactives in that it didn't strip the leading * from the name and would thus fail on them. I won't bother dealing with that now. This works for all active interfaces

EDIT3: Also, I added the bash shebang for clarification because process substitution '< <()' will fail if you use sh aka bash in sh mode.

EDIT4: I also added the second domain, again, just for clarification…

Lhsachs
Contributor II

Here is a script I used for setting up dynamic dns at my site. Note that I have both 10.6 and 10.7 and had to deal with the differences..Hope it gives you some ideas of what you can do.....

#!/bin/sh
OS=`sw_vers | grep ProductVersion | cut -c 17-20`
echo $OS
###find via hardware##
vMac=`system_profiler SPHardwareDataType | awk '/Model Name:/ {print $3$4}'`
echo $vMac

if [ $vMac = "MacPro" ]; then
    echo "Mac Pro - setting up ethernet"
    networksetup -setsearchdomains "Ethernet 1" mycompany.com nvprod.mycompany.com client.mycompany.com nvclient.mycompany.com
        networksetup -setsearchdomains "Ethernet 2" mycompany.com nvprod.mycompany.com client.mycompany.com nvclient.mycompany.com
else
       if [ $vMac = "MacBookAir" ];
       then
       echo "MacBook  Air"
               if [ "${OS}" = "10.7" ]; then
            echo "10.7"
            networksetup -setsearchdomains "Wi-Fi" mycompany.com nvprod.mycompany.com client.mycompany.com nvclient.mycompany.com
                else
                echo "10.6"
                networksetup -setsearchdomains "Airport" mycompany.com nvprod.mycompany.com client.mycompany.com nvclient.mycompany.com
                fi

    else
        echo "Not Mac Pro or MacBook Air"
        echo "must be MacBook Pro, Mac Mini or iMac"
        echo "do the standard stuff"
    if [ "${OS}" = "10.7" ]; then
    echo "10.7"
    networksetup -setsearchdomains "Wi-Fi" mycompany.com nvprod.mycompany.com client.mycompany.com nvclient.mycompany.com
    else    
    echo "10.6"
        networksetup -setsearchdomains "Airport" mycompany.com nvprod.mycompany.com client.mycompany.com nvclient.mycompany.com
    fi

    echo "setting up ethernet"
    networksetup -setsearchdomains "Ethernet" mycompany.com nvprod.mycompany.com client.mycompany.com nvclient.mycompany.com
    echo "all done"

    fi


    echo "all done"
fi

rmanly
Contributor III

@lenny take a look at the output from networksetup -listallnetworkservices it will save you the headache of doing all those checks for Airport v Wi-Fi etc. And will even grab both Ethernet interfaces on the towers.

franton
Valued Contributor III

I had a similar issue not too long ago. As a result, I wrote (and posted) the script here:
https://jamfnation.jamfsoftware.com/discussion.html?id=5701