Skip to main content
Solved

Available Network Interface Script Help


jhbush
Forum|alt.badge.img+26
  • Esteemed Contributor
  • 539 replies

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

Best answer by andyinindy

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 original
Did this topic help you find an answer to your question?

10 replies

Forum|alt.badge.img+18
  • Valued Contributor
  • 238 replies
  • May 2, 2013

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
Forum|alt.badge.img+26
  • Author
  • Esteemed Contributor
  • 539 replies
  • May 2, 2013

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.


Forum|alt.badge.img+18
  • Valued Contributor
  • 238 replies
  • May 2, 2013

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
Forum|alt.badge.img+26
  • Author
  • Esteemed Contributor
  • 539 replies
  • May 2, 2013

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

Forum|alt.badge.img+18
  • Valued Contributor
  • 238 replies
  • Answer
  • May 3, 2013

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
Forum|alt.badge.img+26
  • Author
  • Esteemed Contributor
  • 539 replies
  • May 3, 2013

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.


Forum|alt.badge.img+18
  • Valued Contributor
  • 238 replies
  • May 3, 2013

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
Forum|alt.badge.img+26
  • Author
  • Esteemed Contributor
  • 539 replies
  • May 3, 2013

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

Forum|alt.badge.img+18
  • Valued Contributor
  • 238 replies
  • May 3, 2013

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


Forum|alt.badge.img+12
  • Contributor
  • 134 replies
  • January 9, 2015

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings