Posted on 11-18-2015 03:31 AM
Hi all,
at my client we have a zoo of MacPro's Ethernet , MacBooks with Thunderbolt-Ethernet and Thunderbolt-Displays.
While we're upgrading the Macs to latest Yosemite (ElCapitan is not an option because of the productive environment), clients often "forget" the Network-Settings (DNS-Servers and Proxy).
Normally, we have three locations:
Client_Net:
Only Ethernet, Thunderbolt-Ethernet or Display-Ethernet enabled
Special DNS and Proxy
Client_WIFI:
Only WIFI enabled
Special DNS and Proxy
WIFI-Free
Any Adapter can be active and can be used (WIFI, Thunderbolt-Ethernet,...)
No special settings.
My question:
How to put it with "networksetup" into a script that these networksettings/locations are with a firstboot-script are enabled again?
Thank you!
Holger
Solved! Go to Solution.
Posted on 11-18-2015 04:55 AM
Hi Holger,
here's a redacted version of the networksetup script i use,
you may be able to adjust it to your needs:
#! /bin/sh
####################################################################################################
#hardcoded variables
usb="USB Ethernet"
display="Display Ethernet"
tbolt="Thunderbolt Ethernet"
wired="Ethernet"
wireless="Wi-Fi"
btDUN="Bluetooth DUN"
btPAN="Bluetooth PAN"
firewire="FireWire"
####################################################################################################
#define proxy servers
proxy="proxy.company.net"
####################################################################################################
#detect network interfaces
usbAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: USB Ethernet"`
displayAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: Display Ethernet"`
tboltAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: Thunderbolt Ethernet"`
wiredAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: Ethernet"`
wifiAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: Wi-Fi"`
btDUNAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: Bluetooth DUN"`
btPANAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: Bluetooth PAN"`
firewireAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: FireWire"`
####################################################################################################
#######
#Create new Location Client_Net
#######
networksetup -createlocation "Client_Net" populate
#Switch to Location Client_Net
networksetup -switchtolocation "Client_Net"
##############
## Ethernet ##
##############
if [ "$wiredAdapter" != "" ]; then
/usr/sbin/networksetup -setdhcp "$wired"
/usr/sbin/networksetup -setv6off "$wired"
/usr/sbin/networksetup -setwebproxy "$wired" "$proxy" 8080
/usr/sbin/networksetup -setftpproxy "$wired" "$proxy" 8080
/usr/sbin/networksetup -setstreamingproxy "$wired" "$proxy" 8080
/usr/sbin/networksetup -setgopherproxy "$wired" "$proxy" 8080
/usr/sbin/networksetup -setsocksfirewallproxy "$wired" "$proxy" 8080
/usr/sbin/networksetup -setsecurewebproxy "$wired" "$proxy" 8080
/usr/sbin/networksetup -setproxybypassdomains "$wired" *.local 169.254/16 whatever.company.net
/usr/sbin/networksetup -setsearchdomains "$wired" company.net whatever.net
/usr/sbin/networksetup -setpassiveftp "$wired" on
fi
##################
## USB Ethernet ##
##################
if [ "$usbAdapter" != "" ]; then
/usr/sbin/networksetup -setdhcp "$usb"
/usr/sbin/networksetup -setv6off "$usb"
/usr/sbin/networksetup -setwebproxy "$usb" "$proxy" 8080
/usr/sbin/networksetup -setftpproxy "$usb" "$proxy" 8080
/usr/sbin/networksetup -setstreamingproxy "$usb" "$proxy" 8080
/usr/sbin/networksetup -setgopherproxy "$usb" "$proxy" 8080
/usr/sbin/networksetup -setsocksfirewallproxy "$usb" "$proxy" 8080
/usr/sbin/networksetup -setsecurewebproxy "$usb" "$proxy" 8080
/usr/sbin/networksetup -setproxybypassdomains "$usb" *.local 169.254/16 whatever.company.net
/usr/sbin/networksetup -setsearchdomains "$usb" company.net whatever.net
/usr/sbin/networksetup -setpassiveftp "$usb" on
fi
#####################
## Display Adapter ##
#####################
if [ "$displayAdapter" != "" ]; then
/usr/sbin/networksetup -setdhcp "$display"
/usr/sbin/networksetup -setv6off "$display"
/usr/sbin/networksetup -setwebproxy "$display" "$proxy" 8080
/usr/sbin/networksetup -setftpproxy "$display" "$proxy" 8080
/usr/sbin/networksetup -setstreamingproxy "$display" "$proxy" 8080
/usr/sbin/networksetup -setgopherproxy "$display" "$proxy" 8080
/usr/sbin/networksetup -setsocksfirewallproxy "$display" "$proxy" 8080
/usr/sbin/networksetup -setsecurewebproxy "$display" "$proxy" 8080
/usr/sbin/networksetup -setproxybypassdomains "$display" *.local 169.254/16 whatever.company.net
/usr/sbin/networksetup -setsearchdomains "$display" company.net whatever.net
/usr/sbin/networksetup -setpassiveftp "$display" on
fi
#########################
## Thunderbolt Adapter ##
#########################
if [ "$tboltAdapter" != "" ]; then
/usr/sbin/networksetup -setdhcp "$tbolt"
/usr/sbin/networksetup -setv6off "$tbolt"
/usr/sbin/networksetup -setwebproxy "$tbolt" "$proxy" 8080
/usr/sbin/networksetup -setftpproxy "$tbolt" "$proxy" 8080
/usr/sbin/networksetup -setstreamingproxy "$tbolt" "$proxy" 8080
/usr/sbin/networksetup -setgopherproxy "$tbolt" "$proxy" 8080
/usr/sbin/networksetup -setsocksfirewallproxy "$tbolt" "$proxy" 8080
/usr/sbin/networksetup -setsecurewebproxy "$tbolt" "$proxy" 8080
/usr/sbin/networksetup -setproxybypassdomains "$tbolt" *.local 169.254/16 whatever.company.net
/usr/sbin/networksetup -setsearchdomains "$tbolt" company.net whatever.net
/usr/sbin/networksetup -setpassiveftp "$tbolt" on
fi
################################
## Disable non-wired adapters ##
################################
if [ "$wifiAdapter" != "" ]; then
/usr/sbin/networksetup -setnetworkserviceenabled "$wireless" off
fi
if [ "$btDUNAdapter" != "" ]; then
/usr/sbin/networksetup -setnetworkserviceenabled "$btDUN" off
fi
if [ "$btPANAdapter" != "" ]; then
/usr/sbin/networksetup -setnetworkserviceenabled "$btPAN" off
fi
if [ "$firewireAdapter" != "" ]; then
/usr/sbin/networksetup -setnetworkserviceenabled "$firewire" off
fi
####################################################################################################
#######
#Create new Location Client_WIFI
#######
networksetup -createlocation "Client_WIFI" populate
#Switch to Location Client_WIFI
networksetup -switchtolocation "Client_WIFI"
# Adapter settings as above, with desired adjustments
####################################################################################################
#######
#Create new Location WIFI-Free
#######
networksetup -createlocation "WIFI-Free" populate
#Switch to Location WIFI-Free
networksetup -switchtolocation "WIFI-Free"
# Adapter settings as above, with desired adjustments
####################################################################################################
####################################################################################################
#Switch back to Location Client_Net
networksetup -switchtolocation "Client_Net"
exit 0
Depending on in which scenario you use it, it might be useful to blow away any locations that existed previously before running this (or add a couple lines to the beginning to take care of this)
Posted on 11-18-2015 04:55 AM
Hi Holger,
here's a redacted version of the networksetup script i use,
you may be able to adjust it to your needs:
#! /bin/sh
####################################################################################################
#hardcoded variables
usb="USB Ethernet"
display="Display Ethernet"
tbolt="Thunderbolt Ethernet"
wired="Ethernet"
wireless="Wi-Fi"
btDUN="Bluetooth DUN"
btPAN="Bluetooth PAN"
firewire="FireWire"
####################################################################################################
#define proxy servers
proxy="proxy.company.net"
####################################################################################################
#detect network interfaces
usbAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: USB Ethernet"`
displayAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: Display Ethernet"`
tboltAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: Thunderbolt Ethernet"`
wiredAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: Ethernet"`
wifiAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: Wi-Fi"`
btDUNAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: Bluetooth DUN"`
btPANAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: Bluetooth PAN"`
firewireAdapter=`/usr/sbin/networksetup -listallhardwareports | grep "Hardware Port: FireWire"`
####################################################################################################
#######
#Create new Location Client_Net
#######
networksetup -createlocation "Client_Net" populate
#Switch to Location Client_Net
networksetup -switchtolocation "Client_Net"
##############
## Ethernet ##
##############
if [ "$wiredAdapter" != "" ]; then
/usr/sbin/networksetup -setdhcp "$wired"
/usr/sbin/networksetup -setv6off "$wired"
/usr/sbin/networksetup -setwebproxy "$wired" "$proxy" 8080
/usr/sbin/networksetup -setftpproxy "$wired" "$proxy" 8080
/usr/sbin/networksetup -setstreamingproxy "$wired" "$proxy" 8080
/usr/sbin/networksetup -setgopherproxy "$wired" "$proxy" 8080
/usr/sbin/networksetup -setsocksfirewallproxy "$wired" "$proxy" 8080
/usr/sbin/networksetup -setsecurewebproxy "$wired" "$proxy" 8080
/usr/sbin/networksetup -setproxybypassdomains "$wired" *.local 169.254/16 whatever.company.net
/usr/sbin/networksetup -setsearchdomains "$wired" company.net whatever.net
/usr/sbin/networksetup -setpassiveftp "$wired" on
fi
##################
## USB Ethernet ##
##################
if [ "$usbAdapter" != "" ]; then
/usr/sbin/networksetup -setdhcp "$usb"
/usr/sbin/networksetup -setv6off "$usb"
/usr/sbin/networksetup -setwebproxy "$usb" "$proxy" 8080
/usr/sbin/networksetup -setftpproxy "$usb" "$proxy" 8080
/usr/sbin/networksetup -setstreamingproxy "$usb" "$proxy" 8080
/usr/sbin/networksetup -setgopherproxy "$usb" "$proxy" 8080
/usr/sbin/networksetup -setsocksfirewallproxy "$usb" "$proxy" 8080
/usr/sbin/networksetup -setsecurewebproxy "$usb" "$proxy" 8080
/usr/sbin/networksetup -setproxybypassdomains "$usb" *.local 169.254/16 whatever.company.net
/usr/sbin/networksetup -setsearchdomains "$usb" company.net whatever.net
/usr/sbin/networksetup -setpassiveftp "$usb" on
fi
#####################
## Display Adapter ##
#####################
if [ "$displayAdapter" != "" ]; then
/usr/sbin/networksetup -setdhcp "$display"
/usr/sbin/networksetup -setv6off "$display"
/usr/sbin/networksetup -setwebproxy "$display" "$proxy" 8080
/usr/sbin/networksetup -setftpproxy "$display" "$proxy" 8080
/usr/sbin/networksetup -setstreamingproxy "$display" "$proxy" 8080
/usr/sbin/networksetup -setgopherproxy "$display" "$proxy" 8080
/usr/sbin/networksetup -setsocksfirewallproxy "$display" "$proxy" 8080
/usr/sbin/networksetup -setsecurewebproxy "$display" "$proxy" 8080
/usr/sbin/networksetup -setproxybypassdomains "$display" *.local 169.254/16 whatever.company.net
/usr/sbin/networksetup -setsearchdomains "$display" company.net whatever.net
/usr/sbin/networksetup -setpassiveftp "$display" on
fi
#########################
## Thunderbolt Adapter ##
#########################
if [ "$tboltAdapter" != "" ]; then
/usr/sbin/networksetup -setdhcp "$tbolt"
/usr/sbin/networksetup -setv6off "$tbolt"
/usr/sbin/networksetup -setwebproxy "$tbolt" "$proxy" 8080
/usr/sbin/networksetup -setftpproxy "$tbolt" "$proxy" 8080
/usr/sbin/networksetup -setstreamingproxy "$tbolt" "$proxy" 8080
/usr/sbin/networksetup -setgopherproxy "$tbolt" "$proxy" 8080
/usr/sbin/networksetup -setsocksfirewallproxy "$tbolt" "$proxy" 8080
/usr/sbin/networksetup -setsecurewebproxy "$tbolt" "$proxy" 8080
/usr/sbin/networksetup -setproxybypassdomains "$tbolt" *.local 169.254/16 whatever.company.net
/usr/sbin/networksetup -setsearchdomains "$tbolt" company.net whatever.net
/usr/sbin/networksetup -setpassiveftp "$tbolt" on
fi
################################
## Disable non-wired adapters ##
################################
if [ "$wifiAdapter" != "" ]; then
/usr/sbin/networksetup -setnetworkserviceenabled "$wireless" off
fi
if [ "$btDUNAdapter" != "" ]; then
/usr/sbin/networksetup -setnetworkserviceenabled "$btDUN" off
fi
if [ "$btPANAdapter" != "" ]; then
/usr/sbin/networksetup -setnetworkserviceenabled "$btPAN" off
fi
if [ "$firewireAdapter" != "" ]; then
/usr/sbin/networksetup -setnetworkserviceenabled "$firewire" off
fi
####################################################################################################
#######
#Create new Location Client_WIFI
#######
networksetup -createlocation "Client_WIFI" populate
#Switch to Location Client_WIFI
networksetup -switchtolocation "Client_WIFI"
# Adapter settings as above, with desired adjustments
####################################################################################################
#######
#Create new Location WIFI-Free
#######
networksetup -createlocation "WIFI-Free" populate
#Switch to Location WIFI-Free
networksetup -switchtolocation "WIFI-Free"
# Adapter settings as above, with desired adjustments
####################################################################################################
####################################################################################################
#Switch back to Location Client_Net
networksetup -switchtolocation "Client_Net"
exit 0
Depending on in which scenario you use it, it might be useful to blow away any locations that existed previously before running this (or add a couple lines to the beginning to take care of this)
Posted on 11-18-2015 07:55 AM
Hi,
thank you! This is really a big help...
Cheers,
Holger
Posted on 07-05-2017 05:44 AM
Thanks Chris. Used your script as reference and created this per our needs. Enhanced a bit to eliminate hardcoding of Network Adapter information.
#Proxy Information. Input your proxy information here.
proxy="proxy.domain.com"
port="1234"
bypass="domain1.com, domain2.com, *.domain.com"
#Detect existing Network Adapters and store in an array
detectadapters=$(networksetup -listallhardwareports | grep "Hardware Port" | sed 's/Hardware Port: //g' | grep 'Wi-Fi|Ethernet|USB|LAN')
IFS=$'
'
netadapters=($detectadapters)
unset IFS
#Check if Network Locations are already existing and remove them
work=`networksetup -listlocations | grep "Work Networks"`
nonwork=`networksetup -listlocations | grep "Non-Work Networks"`
automatic=`networksetup -listlocations | grep "Automatic"`
if [ "$automatic" != "" ]; then
networksetup -switchtolocation "Automatic"
else
networksetup -createlocation "Automatic" populate
networksetup -switchtolocation "Automatic"
fi
if [ "$work" != "" ]; then
networksetup -deletelocation "Work Networks"
fi
if [ "$nonwork" != "" ]; then
networksetup -deletelocation "Non-Work Networks"
fi
#Setup Network Locations
networksetup -createlocation "Non-Work Networks" populate
networksetup -createlocation "Work Networks" populate
networksetup -switchtolocation "Work Networks"
networksetup -deletelocation "Automatic"
# Configure Proxy Looping through the available network adapters
for i in "${netadapters[@]}"
do
networksetup -setwebproxy "$i" "$proxy" "$port"
networksetup -setsecurewebproxy "$i" "$proxy" "$port"
networksetup -setproxybypassdomains "$i" "$bypass"
done
exit 0
Any suggestions for further enhancement are Welcome.