Apple’s Worldwide Developers Conference to kick off June 10 at 10 a.m. PDT with Keynote address

The Keynote will be available to stream on apple.com, the Apple Developer app, the Apple TV app, and the Apple YouTube channel. On-demand playback will be available after the conclusion of the stream.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Using a bash script to set up Proxies on macOS

Mojave 10.14.6


My company use Proxy. One Proxy for HTTP and One Proxy for HTTPS and some Bypass proxy settings for these Hosts & Domains.


I was not able to reach google.com website because I had to specify the proxy which I did and everything is working as expected.


But I want to automate my process instead of using the GUI.


I have a strange behavior with my script below when I try to set up: System Preferences > Network > "Example Wi-Fi" > Advanced > Proxies.


Again, if I set all the Proxies parameters using the GUI (manually) everything is working as expected but I if try to use my script below I am having issues like I cannot reach web sites, etc.


I am pretty sure I am missing some command lines from my script. The funny things that when I run the script I can literally see all the parameters being applied to the GUI (Network > Proxies), as if I had to manually inserted. Again they are all there. I tried to reboot my Mac Laptop, I also try to reset some DNS cache but nothing, still same strange issue.


What could be the issue? What command line from the script I am missing?


See my script below (for security reasons I am faking the IP address and domain name.)


Thank you in advance.


#!/bin/bash

networksetup -setwebproxy "Wi-Fi" 10.10.10.10 8080
networksetup -setsecurewebproxy "Wi-Fi" 10.10.10.20 8443 
networksetup -setproxybypassdomains "Wi-Fi" "127.0.0.1,*.local,169.254/16,*.subdomain.maindomain.com,*.maindomain.com"
networksetup -setwebproxystate "Wi-Fi" on

Posted on Aug 21, 2020 7:33 AM

Reply
Question marked as Best reply

Posted on Aug 21, 2020 11:19 AM

You could do what they do at my company.


They use "Automatic Proxy Configuration" -> http://wpad/wpad.dat


When the laptop is inside of work, or VPN connected to work, that above URL resolves. When Not in the work environment, the URL does not resolve, and the user does not have proxies.


It basically does the right thing automatically.


As to your networksetup scripts. I think you use tools like

scutil --proxy
scutil --dns

to see what the current settings are, and compare when you are getting from your script against what the GUI created settings are and keep modifying your script until you get the same results.


Similar questions

3 replies
Question marked as Best reply

Aug 21, 2020 11:19 AM in response to fabio168

You could do what they do at my company.


They use "Automatic Proxy Configuration" -> http://wpad/wpad.dat


When the laptop is inside of work, or VPN connected to work, that above URL resolves. When Not in the work environment, the URL does not resolve, and the user does not have proxies.


It basically does the right thing automatically.


As to your networksetup scripts. I think you use tools like

scutil --proxy
scutil --dns

to see what the current settings are, and compare when you are getting from your script against what the GUI created settings are and keep modifying your script until you get the same results.


Aug 21, 2020 8:25 AM in response to fabio168

My suggestion is to create 2 Network Locations. Then switch between the locations.


If you want to script the switch, you can use

/usr/sbin/scselect "Location Name"


System Preferences -> Network -> Location -> Edit Locations -> [+]


If you have a lot of customization already in your "Automatic" location, you can click on the gear icon under "Edit Locations" and duplicate the existing "Automatic" Location, and then change that to be what you need.


Each Location gets its own set of customizable interfaces. Each interface can have its own Advanced options.


After you setup your 2nd Location, you can use the Apple menu icon -> Locations to switch from the desktop, or as mentioned above you can use the /usr/sbin/scselect command in a script, or from an Automator app -> Run Shell Script, or via System Preferences.


This will work very well, and I've been using this trick for at least 15 years, maybe a little longer.

Aug 21, 2020 8:37 AM in response to BobHarris

Thanks to take the time to answer me. Well the location must be only one. The user must use those system global proxy configurations regardless if they are WiFi or Connected to Work Lan and regardless if they are home or at work. I am the one setting up these macOS laptop, I use Jamf as MDM but I am trying to not to manually typed everything all these proxy settings but create a script and push it. The problem is with my script. thanks a lot.

Using a bash script to set up Proxies on macOS

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.