Skip to content

Instantly share code, notes, and snippets.

@smithjw
Last active October 6, 2021 01:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smithjw/b7ea09571b4c70bbbbdb5ee9bcd0fa41 to your computer and use it in GitHub Desktop.
Save smithjw/b7ea09571b4c70bbbbdb5ee9bcd0fa41 to your computer and use it in GitHub Desktop.
#!/bin/bash
# 23-Sep-2021 James Smith Overhauled script to function offline and based on information in the management_info plist
pb="/usr/libexec/PlistBuddy -c"
logged_in_user=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )
management_plist="/Library/Management/management_info.plist"
nomad_plist="/Users/$logged_in_user/Library/Preferences/com.trusourcelabs.NoMAD.plist"
interface_name=""
search_domains=""
bypass_domains=""
country_code=""
proxy_url=""
check_network() {
# Determine if the network is up by looking for any non-loopback (taken from /etc/rc.common)
# internet network interfaces.
local test
if [ -z "${NETWORKUP:=}" ]; then
test=$(ifconfig -a inet 2>/dev/null | sed -n -e '/127.0.0.1/d' -e '/0.0.0.0/d' -e '/inet/p' | wc -l)
if [ "${test}" -lt 1 ]; then
echo "Could not find a network."
exit 0
fi
fi
}
get_prefs() {
local __variable_name="$1"
local __key_name="$2"
local __file_name="$3"
if [ -e "$__file_name" ]; then
__key_value=$($pb "Print $__key_name" "$__file_name" 2>/dev/null)
exit_code=$?
if [ $exit_code == 0 ]; then
# shellcheck disable=SC2140
eval "$__variable_name"="'$__key_value'"
else
echo "$__key_name does not exist in plist"
exit 0
fi
else
echo "Could not find management plist"
exit 0
fi
}
## Main Section
# Make sure the network is up
check_network
# Make sure we have the correct information for updating the proxy URL & associated domains
get_prefs bypass_domains ":network_info:bypass_domains" "$management_plist"
get_prefs search_domains ":network_info:search_domains" "$management_plist"
# Make sure we have the correct information about the user's country
get_prefs country_code ":UserAttributes:c" "$nomad_plist"
# Set proxy variable based on country data from NoMAD
case $country_code in
AU)
get_prefs proxy_url ":network_info:au_proxy" "$management_plist"
;;
IN)
get_prefs proxy_url ":network_info:in_proxy" "$management_plist"
;;
NZ)
get_prefs proxy_url ":network_info:nz_proxy" "$management_plist"
;;
MY)
get_prefs proxy_url ":network_info:my_proxy" "$management_plist"
;;
GU)
get_prefs proxy_url ":network_info:gu_proxy" "$management_plist"
;;
*)
get_prefs proxy_url ":network_info:au_proxy" "$management_plist"
;;
esac
echo "Using the following values for proxy configuration:"
echo " Search Domains: $search_domains"
echo " Bypass Domains: $bypass_domains"
echo " Country Code: $country_code"
echo " Proxy URL: $proxy_url"
networksetup -listallnetworkservices | sed '1d' | grep -Ev '^\*' |
while read -r interface_name; do
if [[ "$interface_name" != "Bluetooth PAN" && "$interface_name" != "Thunderbolt Bridge" && "$interface_name" != "Display FireWire" && "$interface_name" != "iPhone USB" && "$interface_name" != "iPad USB" ]]; then
echo "Configuring Interface: $interface_name"
networksetup -setautoproxyurl "$interface_name" "$proxy_url"
networksetup -setproxybypassdomains "$interface_name" "$bypass_domains"
# shellcheck disable=SC2086
# In this case we want word-splitting to occur
networksetup -setsearchdomains "$interface_name" $search_domains
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment