add_proxy_exception.sh
#!/bin/sh
####################################################################################################
#Author: Ajay Mittal 6/19/2017
PathToFireFoxProfile="/Users/$USER/Library/Application Support/Firefox/Profiles"
echo $PathToFireFoxProfile
#PathToFireFoxProfile="/Users/amittal/scripts/test/Profiles"
echo $PathToFireFoxProfile
#FirefoxNameOfPreferencesFile="mac_prefs.js"
FirefoxNameOfPreferencesFile="prefs.js"
#Check if Profiles folder exists and if not then exit
#If Firefox profile exists. Else firefox is not installed
#--------------------------------------------------------
if [ -d "$PathToFireFoxProfile" ]; then
# Control will enter here if $DIRECTORY doesn't exist.
echo "Directory exists"
#exit 0
echo "Directory Exists"
#Now find Default folder name
for entry in `ls "$PathToFireFoxProfile/"`
do
echo "Entry ls$entry"
if echo "$entry" | grep -q "default"
then
PathToFireFoxProfile="$PathToFireFoxProfile/$entry/"
echo "Entry contains default: $PathToFireFoxProfile"
break
else
echo "Entry does not contain default"
fi
done
#let's read line by line of the file prefs.js
FirefoxNameOfPreferencesFile="$PathToFireFoxProfile$FirefoxNameOfPreferencesFile"
echo $FirefoxNameOfPreferencesFile
FirefoxNameOfPreferencesNewFile="$PathToFireFoxProfile""prefs_new.js"
echo $FirefoxNameOfPreferencesNewFile
rm $FirefoxNameOfPreferencesNewFile
#Whether prefs.js contained the line exceptions
lineWithExceptions=0
echo "" > $FirefoxNameOfPreferencesNewFile
while read line1; do
echo ""$line1
entry="$line1"
#echo "$entry"
if echo "$entry" | grep -q '"network.proxy.no_proxies_on"'
then
#PathToFireFoxProfile="$PathToFireFoxProfile/$entry/"
lineWithExceptions=1
echo "Found a line with Exceptions lineWithExceptions=1 $entry"
#check if the entry contains on24local
echo "$entry"
if echo "$entry" | grep -q "on24local.com"
then
echo "Nothing to do. Found on24local.com"
lineWithExceptions=2
else
echo "Need to add entry"
#user_pref("network.proxy.no_proxies_on", "localhost, 127.0.0.1");
#there is already a comma
if echo $entry | grep -q '");'
then
echo ""
leftHandSide=`echo $entry | sed 's/");//' | sed -e's/[[:space:]]*$//'`
lastCh="${leftHandSide: -1}"
if [[ "$lastCh" == "," ]]
then
echo "Last Character is a comma"
entry=$leftHandSide"on24local.com\");"
echo $entry
else
echo "Last Character is not a comma"
entry=$leftHandSide",on24local.com\");"
echo $entry
fi
#echo "leftHandSide=$leftHandSide""Terminate""$lastCh"
#echo "Entry:"$entry
echo "$entry" >> $FirefoxNameOfPreferencesNewFile
else
echo ""
fi #if echo $entry | grep -q '");'
fi
else
#lineWithExceptions=0 #echo ""
#echo $entry
echo "$entry" >> $FirefoxNameOfPreferencesNewFile
fi
done < $FirefoxNameOfPreferencesFile
if [ $lineWithExceptions -eq 0 ]
then
echo "No line with exceptions found in the prefs.js"
#Backup the existing prefs.js Add the exception default line to the existing prefs.js
#echo $FirefoxNameOfPreferencesFile
cp "$FirefoxNameOfPreferencesFile" "$PathToFireFoxProfile""/prefs_backup.js"
echo 'user_pref("network.proxy.no_proxies_on", "localhost, 127.0.0.1,on24local.com");' >> "$FirefoxNameOfPreferencesFile"
#add to the end
elif [ $lineWithExceptions -eq 1 ]
then
cp "$FirefoxNameOfPreferencesFile" "$PathToFireFoxProfile""/prefs_backup.js"
cp "$PathToFireFoxProfile/prefs_new.js" "$FirefoxNameOfPreferencesFile"
else
echo "No changes made to prefs.js file"
fi #if lineWithExceptions=0
fi #if [ ! -d "$PathToFireFoxProfile" ]; then
#If Firefox profile exists. Else firefox is not installed
#--------------------------------------------------------
# Setting value on "SetUDIDSets" to define the "Sets" name as this will be different on each computer
#Add exception to each network device
#----------------------------------------------------------------------------------------
#---ADD PROXY EXCEPTION FOR CHROME,SAFARI I.E; SYSTEM
#exit 0
cp /Library/Preferences/SystemConfiguration/preferences.plist ~/preferences_plist.backup
SetUDIDSets=$(/usr/libexec/PlistBuddy -c "print :Sets" /Library/Preferences/SystemConfiguration/preferences.plist | perl -lne 'print $1 if /^ (\S*) =/')
IFS=$'\n'
# Loops through the list of network services and sets Exclude Simple Hostnames to 1.
for i in $(/usr/libexec/PlistBuddy -c "print :Sets:$SetUDIDSets:Network:Global:IPv4:ServiceOrder" /Library/Preferences/SystemConfiguration/preferences.plist | awk 'NR>2{ print l} {l=$0}' | perl -pe 's/^\s+//');
do
# If the setting Exclude Simple Hostnames never has been touched we need to create this
#sudo /usr/libexec/PlistBuddy -c "add :NetworkServices:$i:Proxies:ExcludeSimpleHostnames integer 1" /Library/Preferences/SystemConfiguration/preferences.plist
#sudo /usr/libexec/PlistBuddy -c "set :NetworkServices:$i:Proxies:ExcludeSimpleHostnames on24local.com" /Library/Preferences/SystemConfiguration/preferences.plist
#sudo /usr/libexec/PlistBuddy -c "add :NetworkServices:$i:Proxies:ExcludeSimpleHostnames integer 1" /Library/Preferences/SystemConfiguration/preferences.plist
#sudo /usr/libexec/PlistBuddy -c "set :NetworkServices:$i:Proxies:ExcludeSimpleHostnames on24local.com" /Library/Preferences/SystemConfiguration/preferences.plist
echo "--------------------------------------------------------------"
echo "Working on $i"
if sudo /usr/libexec/PlistBuddy -c "print :NetworkServices:$i:Proxies:ExceptionsList" /Library/Preferences/SystemConfiguration/preferences.plist | grep -q "on24local"
then
echo "Entry contains on24local"
else
echo "Entry does not contain on24local"
#sudo /usr/libexec/PlistBuddy -c "add :NetworkServices:3553B5CE-0F4A-4D96-8BC1-CFBDC3A9F7D9:Proxies:ExceptionsList:0 string on24local.com" /Library/Preferences/SystemConfiguration/preferences.plist
sudo /usr/libexec/PlistBuddy -c "add :NetworkServices:$i:Proxies:ExceptionsList:0 string on24local.com" /Library/Preferences/SystemConfiguration/preferences.plist
fi
#sudo /usr/libexec/PlistBuddy -c "add :NetworkServices:$i:Proxies:ExceptionsList:0 string on24local.com" /Library/Preferences/SystemConfiguration/preferences.plist
echo "Exclude Simple Hostnames is now set for $i"
done
unset IFS
defaults read /Library/Preferences/SystemConfiguration/preferences.plist
echo "We're done!"