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
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.
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}
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
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
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.
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
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
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 :)
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