Posted on 05-02-2013 08:01 AM
I'm trying to tidy up my search domain script by only grabbing the available network interfaces. If I use my existing one from my first boot script I receive errors due the fact that not all of them are present. It doesn't effect the search domain being applied, but the end user sees the error through self service attempting to run it multiple times. Anyway here's what I have so far. I can't figure out how to escape the space in between say Display Ethernet or to keep Wi-Fi intact.
#!/bin/bash
declare -x SearchDomains="my.domain.com"
export devlist="$(networksetup -listnetworkserviceorder | grep Hardware |
sed -e 's/^(H.*rt: (.*), Device: (.*))/1/'
-e 's/[()*#]//g' -e 's/[ -]/_/g')"
echo My device list:
for dev in ${devlist}; do /usr/sbin/networksetup -setsearchdomains ${dev} ${SearchDomains}; done
echo
exit 0
Solved! Go to Solution.
Posted on 05-03-2013 07:44 AM
I think I have it:
#!/bin/bash
declare -x SearchDomains="butler.edu"
IFS=$'
'
devlist=(`networksetup -listnetworkserviceorder | grep Hardware | sed -e 's/^(H.*rt: (.*), Device: (.*))/1/' -e 's/[()*#]//g' | uniq`)
tLen=${#devlist[@]}
echo My device list:
for (( i=0; i<${tLen}; i++ ));
do
/usr/sbin/networksetup -setsearchdomains "${devlist[$i]}" $SearchDomains
done
echo
exit 0
You might need to fool around with the echo stuff to get it to print out what you want, but otherwise it seems to work.
--Andy
Posted on 05-03-2013 10:37 AM
Andy, thank you. I owe you many beers at JNUC.
#!/bin/bash
IFS=$'
'
devlist=(`networksetup -listnetworkserviceorder | grep Hardware | sed -e 's/^(H.*rt: (.*), Device: (.*))/1/' -e 's/[()*#]//g' | uniq`)
tLen=${#devlist[@]}
echo My device list:
for (( i=0; i<${tLen}; i++ ));
do
/usr/sbin/networksetup -setsearchdomains "${devlist[$i]}" your1.domain.com your2.domain.com your3.domain.com your4.domain.com
done
echo
exit 0
Posted on 05-02-2013 12:10 PM
Does this accomplish what you had in mind:
#!/bin/bash
declare -x SearchDomains="my.domain.com"
export devlist="$(networksetup -listnetworkserviceorder | grep Hardware | sed -e 's/^(H.*rt: (.*), Device: (.*))/1/' -e 's/[()*#]//g' | sed 's/$/,/g')"
export devarray=(`echo $devlist | tr "," "
" | uniq`)
echo My device list:
for i in ${devarray[@]}; do /usr/sbin/networksetup -setsearchdomains $i $SearchDomains; done
echo
exit 0
Posted on 05-02-2013 12:36 PM
Andy, it's close but the problem still seems to be with how networksetup accepts the interface information. If I echo out devlist the names of the interfaces look fine on both scripts. It's only when you try and send that to network setup do get the line breaks and the error. Thank you for your help.
My device list:Bluetooth DUN, Thunderbolt Ethernet, Display Ethernet, Display Ethernet, Display Ethernet, Display FireWire, Display FireWire, Display FireWire, Wi-Fi, Bluetooth PAN,
Bluetooth is not a recognized network service.
** Error: The parameters were not valid.
DUN is not a recognized network service.
** Error: The parameters were not valid.
Thunderbolt is not a recognized network service.
** Error: The parameters were not valid.
Ethernet is not a recognized network service.
** Error: The parameters were not valid.
Display is not a recognized network service.
** Error: The parameters were not valid.
Ethernet is not a recognized network service.
** Error: The parameters were not valid.
Display is not a recognized network service.
** Error: The parameters were not valid.
FireWire is not a recognized network service.
** Error: The parameters were not valid.
** Error: Unable to commit changes to network database.
Bluetooth is not a recognized network service.
** Error: The parameters were not valid.
PAN is not a recognized network service.
** Error: The parameters were not valid.
Posted on 05-02-2013 12:42 PM
I assume that you have tried putting the ${dev} (or the $i in my script) into double quotes?
do /usr/sbin/networksetup -setsearchdomains "${dev}" ${SearchDomains}
Posted on 05-02-2013 02:11 PM
I did but I still get the same line returns on the input. pmbuko has this on his site which grabs the primary only.
mainInt=$(networksetup -listnetworkserviceorder |
awk -F'\) ' '/(1)/ {print $2}')
networksetup -setsearchdomains "$mainInt" sub.domain.com domain.com
Posted on 05-03-2013 07:44 AM
I think I have it:
#!/bin/bash
declare -x SearchDomains="butler.edu"
IFS=$'
'
devlist=(`networksetup -listnetworkserviceorder | grep Hardware | sed -e 's/^(H.*rt: (.*), Device: (.*))/1/' -e 's/[()*#]//g' | uniq`)
tLen=${#devlist[@]}
echo My device list:
for (( i=0; i<${tLen}; i++ ));
do
/usr/sbin/networksetup -setsearchdomains "${devlist[$i]}" $SearchDomains
done
echo
exit 0
You might need to fool around with the echo stuff to get it to print out what you want, but otherwise it seems to work.
--Andy
Posted on 05-03-2013 09:02 AM
Andy, thank you so much for figuring this out. The one last thing is how do you get a new line inserted? I have 12 search domains. When I run this it puts them all one line.
Posted on 05-03-2013 10:17 AM
If you know what the searchdomains will be then you can do away with the $SearchDomains variable entirely and simply add them like so:
/usr/sbin/networksetup -setsearchdomains "${devlist[$i]}" butler.edu stanford.edu unc.edu
HTH,
--Andy
Posted on 05-03-2013 10:37 AM
Andy, thank you. I owe you many beers at JNUC.
#!/bin/bash
IFS=$'
'
devlist=(`networksetup -listnetworkserviceorder | grep Hardware | sed -e 's/^(H.*rt: (.*), Device: (.*))/1/' -e 's/[()*#]//g' | uniq`)
tLen=${#devlist[@]}
echo My device list:
for (( i=0; i<${tLen}; i++ ));
do
/usr/sbin/networksetup -setsearchdomains "${devlist[$i]}" your1.domain.com your2.domain.com your3.domain.com your4.domain.com
done
echo
exit 0
Posted on 05-03-2013 11:25 AM
Glad I could help! Looking forward to quaffing some Surly at next year's JNUC (assuming it's in MN like last time). I could also dig on some Elysian, if I ever make it out to Seattle again :)
Posted on 01-08-2015 05:48 PM
andyinindy / jhbush1973
Thanks for this. I had been using one script per net interface.
I like this as it will assign the specified search domains to any network interface, even if it is not presently active. thanks again, john