Available Network Interface Script Help

jhbush
Valued Contributor II

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
2 ACCEPTED SOLUTIONS

andyinindy
Contributor II

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

View solution in original post

jhbush
Valued Contributor II

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

View solution in original post

10 REPLIES 10

andyinindy
Contributor II

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

jhbush
Valued Contributor II

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.

andyinindy
Contributor II

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}

jhbush
Valued Contributor II

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

andyinindy
Contributor II

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

jhbush
Valued Contributor II

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.

andyinindy
Contributor II

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

jhbush
Valued Contributor II

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

andyinindy
Contributor II

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

johnklimeck
Contributor II

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