Setting Search Domains Dynamically

franton
Valued Contributor III

Hi,

First post! I'm new to Casper (have about four months experience under my belt) and i've been working on an implementation project for 8.62.

One of the things i've encountered was our need to set search domains on each mac to ensure that things like AD authentication works reliably. The supplied JAMF script does the job but we quickly ended up in the situation with three copies of the script attached to multiple smart groups to take care of the varying names of the Ethernet network service on various macs.

For example, our iMacs all have a network service simply called "Ethernet". Our Mac Pros all have "Ethernet 1" and "Ethernet 2". The few rMBP's have "Thunderbolt Ethernet". MBA's have "USB Ethernet" and so on ...

To try and make scripts to cope with the increasing variation in name got unwieldy so I've implemented a new version to cope.

You can call this script as normal from a policy in JSS. You can pass it the network service name you wish to search for, and two search domains. The script will then search for all occurrences of the specified network service and apply the domains to it.

(the hard bit was taking care of the spaces present in the names. Bash likes to treat spaces as a newline character.)

Here's the script:

#!/bin/sh

# Set Search Domains
# Author: Richard Purves
# Version 1.0 : 15-10-2012 - Initial Version
# Version 1.1 : 16-10-2012 - Bugfixed Version
# Version 1.2 : 23-10-2012 - Use an array to pass spaces in network service name
# Version 1.3 : 24-10-2012 - Improved logging

# This script should detect the names of any present specified network ports and
# configure the search domains settings accordingly.

# Based loosely off the JAMF script that does the same thing for policy compatibility reasons.

# Set variables up here
# Casper reserves $1 to 3 for itself, so we have to use $4 onwards.
# So when calling this script, use the following fields of information:
# Field 4: Name of a Network Service
# Field 5: First search domain address. (eg. arts.local)
# Field 6: Second search domain address. (eg. arts.ac.uk)

searchNetwork="$4"
searchDomain1="$5"
searchDomain2="$6"

# Let's check to see if we've been passed the Search Domain details in field 5 & 6.

if [ "$searchNetwork" == "" ]; then echo "Error: No network service name in parameter 4 was specified." exit 1
fi

if [ "$searchDomain1" == "" ]; then echo "Error: No search domain in parameter 5 was specified." exit 1
fi

if [ "$searchDomain2" == "" ]; then echo "Error: No search domain in parameter 6 was specified." exit 1
fi

# We're going to be doing clever things with $IFS
# (internal field separator)
# So we need to save IFS so we can change it back later OLDIFS=$IFS
IFS=$' '

# Let's start setting the search domains

# Read the output of the networksetup command
# Grep that output through the specified service name
# Then read all of it into an array
NetServiceArray=($( networksetup -listallnetworkservices | grep $searchNetwork ))

# We'll stop being clever with $IFS and put it back the way it was
IFS=$OLDIFS

# What's the length of the array? We need it for the following loop
tLen=${#NetServiceArray[@]}

# This is the bit that actually does the work
# Loop around the array and process the contents
for (( i=0; i<${tLen}; i++ ));
do echo "Network Service name to be configured - " "${NetServiceArray[$i]}" echo "Specified Search Domains addresses - " $searchDomain1 " - " $searchDomain2 networksetup -setsearchdomains "${NetServiceArray[$i]}" $searchDomain1 $searchDomain2
done

# All done!
echo "Completed!"
exit 0

0 REPLIES 0