Posted on 07-08-2013 01:49 PM
Hi Everyone,
I have been tasked with deploying a script to our mobile systems that will add several new search domains to the DNS config. I have a working script that "changes" the current search domain, but what I need to do is have it add a new entry, not edit the current one (if that makes sense). In my current script, I can plug in all of my new search domains and it will add them as comma separated, unfortunately though that's not going to work.
For example, our systems start out with a given search domain:
company.companyusa.corp with my current script, if I plugin the new search domains it will look something like company.corp, sea.company.corp, APAC.company.corp... est..
I need these to be on new lines:
Company.corp
sea.company.corp
APAC.company.corp
I've included the script i'm currently working on. Any insight or suggestions would be very much appreciated.
#!/bin/sh
# setSearchDomains.sh -- Set a search domain for a specified network interface
#
# SYNOPSIS
# sudo setSearchDomains.sh
# sudo setSearchDomains.sh <mountPoint> <computerName> <currentUsername> <networkInterface>
# <searchDomains>
#
# If the $networkInterface parameter is specified (parameter 4), this is the Network Interface for
# which the search domains will be set. The expected values for the $networkInterface parameter can
# be found by running the command:
#
# networksetup -listallnetworkservices
#
# If the $searchDomains parameter is specified (parameter 5), this is the search domain that will
# be set.
#
# If no parameter is specified for parameters 4 and 5, the hardcoded value in the script will be
# used.
#
# DESCRIPTION
# This script will set a search domain in the network settings for whichever network interface has
# been specified.
#
#
# DEFINE VARIABLES & READ IN PARAMETERS
#
####################################################################################################
# HARDCODED VALUES ARE SET HERE Example Interfaces are Ethernet 1, Ethernet 2, Thunderbolt Ethernet (rMBP), USB Ethernet etc basically whatever your Network Type shows in Sys Prefs
networkInterface="Ethernet"
**searchDomains="comapny.corp, americas.company.corp, europe.comapny.corp, sea.comapny.corp, asia.comapany.corp**"
# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 4 AND, IF SO, ASSIGN TO "networkInterface"
if [ "$4" != "" ] && [ "$networkInterface" == "" ];then
networkInterface=$4
fi
# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 5 AND, IF SO, ASSIGN TO "searchDomains"
if [ "$5" != "" ] && [ "$searchDomains" == "" ];then
searchDomains=$5
fi
####################################################################################################
#
# SCRIPT CONTENTS - DO NOT MODIFY BELOW THIS LINE
#
####################################################################################################
if [ "$networkInterface" == "" ]; then
echo "Error: No network interface has been specified."
exit 1
fi
if [ "$searchDomains" == "" ]; then
echo "Error: No search domains have been specified."
exit 1
fi
OS=`/usr/bin/defaults read /System/Library/CoreServices/SystemVersion ProductVersion | awk '{print substr($1,1,4)}'`
if [[ "$OS" < "10.5" ]]; then
echo "Setting search domains for OS $OS..."
/System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Support/networksetup -setsearchdomains "$networkInterface" "$searchDomains"
else
echo "Setting search domains for OS $OS..."
/usr/sbin/networksetup -setsearchdomains "$networkInterface" "$searchDomains"
fi
Posted on 07-08-2013 02:18 PM
Believe it or not, you don't need the commas between each search domain entry. Just a space will give you what you want.
So for your search domains variable in the script, just try this instead-
searchDomains="comapny.corp americas.company.corp europe.comapny.corp sea.comapny.corp asia.comapany.corp"
Posted on 07-08-2013 03:33 PM
If you want to add to your domain list you can set a variable using -getsearchdomains
You could then use that as part of your search domains that you applying:
#!/bin/sh
NewSearchDomains=(test1.internetaddress.com test2.internetaddress.com test3.internetaddress.com)
ExistingSearchDomains=(`networksetup -getsearchdomains $networkInterface `)
/usr/sbin/networksetup -setsearchdomains "$networkInterface" "${ExistingSearchDomains[@]} ${NewSearchDomains[@]}"
I put it into an array so that you could parse thought and do matching to ensure that you aren't duplicating any domains.
#!/bin/sh
NewSearchDomains=(test1.internetaddress.com test2.internetaddress.com test3.internetaddress.com)
ExistingSearchDomains=(`networksetup -getsearchdomains $networkInterface `)
for i in ${ExistingSearchDomains[@]}; do
for n in ${NewSearchDomains[@]}; do
if "$i" == "$n"; then
echo Not adding MATCH $n
else
echo Adding $n
ExistingSearchDomains=(`echo ${ExistingSearchDomains[@]}` $n)
fi
done
done
/usr/sbin/networksetup -setsearchdomains "$networkInterface" "${ExistingSearchDomains[*]}"
I think that works. You might need to debug some.
Of course there is also probably an easier way to do this.
Posted on 07-09-2013 04:12 AM
Sorry didn't read the question all the way through.
Posted on 07-09-2013 04:58 AM
I posted this a while back. It's very flexible as to network adaptor names.
Posted on 07-10-2013 09:00 AM
MM2270
I appreciate the response, but these each needed to be accounted for as an independent entity. If I space separate them on the same line I still run into the same problem with DNS.
Thank you all for the responses