How To: Change the “Automatic Proxy Configuration” url in System Preferences via a Script

Standard

The “Automatic Proxy Configuration” url in System Preferences is used to point the clients to a PAC file to set the proxy server when on the Office LAN. Below are some scripts to manage this setting.

 

1. This version will only set the “Automatic Proxy Configuration” url for the defined services.

2. This version will set the “Automatic Proxy Configuration” url for all services.

If you happen to want to turn off this setting, the below will turn it off for all interfaces:

Whilst the above will turn off the “Automatic Proxy”, it doesn’t clear it. Sadly there is no way via the networksetup command to remove an “Automatic Proxy URL” once set.

If this bothers you, then then below will set the “Automatic Proxy URL” to ” “. It works the same as clearing the URL, but the space will show when typing into the URL field.

Screen Shot 2014-12-07 at 21.52.56

33 thoughts on “How To: Change the “Automatic Proxy Configuration” url in System Preferences via a Script

  1. Tim Kimpton

    doesn’t work for me and looks very confusing. I use this

    #!/bin/bash

    ######## HARDCODED VALUES ARE SET HERE #########
    pacfile=”file://localhost/proxy.pac”

    Eth=’Ethernet’
    Eth1=’Ethernet 1′
    Eth2=’Ethernet 2′
    Air=’AirPort’
    Wi=’Wi-Fi’
    #################################################

    if
    networksetup -listallnetworkservices | grep “${Eth}”
    then
    networksetup -setautoproxyurl “${Eth}” “${pacfile}”
    else
    echo ” There is not a service for “${Eth}” no need to set the pac file”
    fi

    if
    networksetup -listallnetworkservices | grep “${Eth1}”
    then
    networksetup -setautoproxyurl “${Eth1}” “${pacfile}”
    else
    echo ” There is not a service for “${Eth1}” no need to set the pac file”
    fi

    if
    networksetup -listallnetworkservices | grep “${Eth2}”
    then
    networksetup -setautoproxyurl “${Eth2}” “${pacfile}”
    else
    echo ” There is not a service for “${Eth2}” no need to set the pac file”
    fi

    if
    networksetup -listallnetworkservices | grep “${Air}”
    then
    networksetup -setautoproxyurl “${Air}” “${pacfile}”
    else
    echo ” There is not a service for “${Air}” no need to set the pac file”
    fi

    if
    networksetup -listallnetworkservices | grep “${Wi}”
    then
    networksetup -setautoproxyurl “${Wi}” “${pacfile}”
    else
    echo ” There is not a service for “${Wi}” no need to set the pac file”
    fi

    • Ben Toms

      There are many ways to skin a cat!

      The difference between your script & mine is that yours repeats the same steps 6 times.

      Where mine runs once & loops through the variables.

      Incidentally, what error do you get?

  2. Tim Kimpton

    Please clarify why you have two scripts. What does script one do and what does script two do. Do they need to run together?

    Please can you see script two. Is there information missing under

    # Get a list of all services beginning ‘Ether’ ‘Air’ or ‘VPN’ or ‘Wi-Fi’
    # If your service names are different to the below, you’ll need to change the criteria

    • Tim Kimpton

      Problem is what if the script needs to work on 10.7 and 10.6? Service names are different? Granted not the most elegant way using loads of if statements 🙂

      • Ben Toms

        from the 1st script:

        #Loops through the list of network services
        for i in $(networksetup -listallnetworkservices | tail +2 );
        do

        # Get a list of all services beginning ‘Ether’ ‘Air’ or ‘VPN’ or ‘Wi-Fi’
        # If your service names are different to the below, you’ll need to change the criteria

        So unless you rename your interfaces.. this will set proxies for Ethernert, VPN, AirPort or Wi-Fi.. which also covers 10.6 & 10.7 🙂

    • Ben Toms

      Updated: This 2nd script will set the ”Automatic Proxy Configuration” url in System Preferences for all network services.. if you’re not choosy & just wish this setting applied to all network services.

      • Tim Kimpton

        Ah great thanks very much 🙂 I must have got confused some where causing things to fail. Great stuff will give it a go.

  3. F

    Instead of going thru all the available adapter is there a way to just verify if an virtual adapter created by a VPN Client is present and then update that adapter with the autoconfig script.

  4. Jarrod

    Where are the scripts? I think I might be missing something here, as I can’t seem to find a download link, or link to the 2 scripts.

  5. Marcos

    I tried the PAC file script for all services and it works great! However, it doesn’t seem to enforce the setting. Is there anything else I need to do other than specify the PAC file URL? Sorry, new to Proxy settings on Macs. Thanks.

  6. Jordan

    Getting an error on one device.
    “Script result: /Library/Application Support/JAMF/tmp/Auto Proxy Config: line 45: networksetup: command not found
    Auto proxy present, correct & enabled for all interfaces”
    Any ideas?

  7. Kirk

    Excellent script. I’ve added the following for my own needs to turn off any previously set explicitly defined proxies now that we are using a PAC file that works (previous version my company uses wasn’t working with the Mac uses). Others might find this useful to add to the script for themselves.

    # Disable specific proxy settings for for all other settings
    /usr/sbin/networksetup -setftpproxystate “$i” off
    echo “Turned off ftp proxy state for $i”
    /usr/sbin/networksetup -setwebproxystate “$i” off
    echo “Turned off web proxy state for $i”
    /usr/sbin/networksetup -setsecurewebproxy “$i” off
    echo “Turned off secure web proxy state for $i”
    /usr/sbin/networksetup -setstreamingproxystate “$i” off
    echo “Turned off streaming proxy state for $i”
    /usr/sbin/networksetup -setgopherproxystate “$i” off
    echo “Turned off gopher proxy state for $i”
    /usr/sbin/networksetup -setsocksfirewallproxystate “$i” off
    echo “Turned off socks firewall proxy state for $i”

    One question, have you managed to loop this through all network locations without first switching the active location to the one you want to modify? Meaning, I have remote users with laptops who may have 2 or more locations as a result of our prior proxy woes. While I could delete those locations I’d prefer to simply update them remotely behind the scenes. Obviously we can list locations but I haven’t managed to find a way to make changes to those that aren’t currently active.

    Thanks!

  8. Mat

    Great script, 2 is working for me. Anyway to also set the proxy bypass info at the same time for all services?

  9. zukic

    i cant get the script to run.. i get a lot of command not found errors starting from line 25.
    i am using the script that adds the proxy server to all services.

    live 25 is
            #Loops through the list of network services

    really not sure whats going on… any ideas?

  10. zukic

    ok so i managed to get your script working.
    had to chop and change it a bit but i got there in the end…. i removed a few sections that i just simply couldnt get to run correctly.

    the script runs during logon via jamf.
    i just tested it and it definitely applied the pac file to all interfaces.

    #!/bin/sh
    ####################################################################################################
    #
    # More information: https://macmule.com/2014/12/07/how-to-change-the-automatic-proxy-configuration-url-in-system-preferences-via-a-script/
    #
    # GitRepo: https://github.com/macmule/setAutomaticProxyConfigurationURL
    #
    # License: http://macmule.com/license/
    #
    ####################################################################################################

    # HARDCODED VALUES ARE SET HERE
    autoProxyURL=”pac.file”

    #echo “$autoProxyURL”

    /usr/sbin/networksetup -detectnewhardware

    IFS=$’\n’

    for i in $(networksetup -listallnetworkservices | tail +2)
    do
    #echo “network interface: $i”

    /usr/sbin/networksetup -setautoproxyurl $i $autoProxyURL
    echo “set auto proxy for $i to $autoProxyURL”

    /usr/sbin/networksetup -setautoproxystate “$i” on
    echo “Turned on auto proxy for $i”
    done

    unset IFS

    i will test with a few more users but so far so good.

    thanks!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.