Bash Scripting: Proxy Examples

Hi all. Welcome back to the final part of the network configuration series.

In this blog I will try to provide an example script to configure all the options and to explain each line.

The end goal

The first thing you should do is sit down and work out exactly what you need to achieve. For our example we will be taking a fresh install and want the following goals:

  1. Replace the Automatic Network location with two new ones; Home that is unproxied and Office that will have all the proxy settings.
  2. The Office location should be the default for a fresh machine
  3. The Ethernet interface only should have the proxy setting
  4. The proxy server for HTTP and HTTPS is “” port 8080
  5. There should be the standard exceptions in place (“*.local” and “169.254/16”) as well as “”, “” and “”.

To achieve this, the example script will run the following in this order:

  1. Create a location called “Home” and auto-populate with the network interfaces
  2. Switch to “Home”
  3. Delete the “Automatic” interface
  4. Create a location called “Office” and auto-populate with the network interfaces
  5. Switch to “Office”
  6. Set the Ethernet HTTP proxy
  7. Set the Ethernet HTTPS proxy
  8. Set the Ethernet proxy exception

Now we have our goals and our task list, lets get scripting.

Section 1: Introduction

This first section should be used to describe what the script does and to set any variables for easy modification.

bash scripting proxy examples

Section 3: Applying proxy settings to Ethernet

This third section is used to apply the Proxy Server settings as per goals 6 – 7:

Applying proxy settings to Ethernet

Section 4: Applying proxy the Proxy Exceptions

This fourth section is used to apply the Proxy exception settings as per the final goal:

Applying proxy the Proxy Exceptions

Section 5: Finishing up

This fifth and final section is used simply to exit the script:

exit the script

Further work

There you have it, that’s the entire script. It should do the job but could also do with some additional tweaks. Off the top of my head, I can think of:

  • Add a log file to leave a record of what happened.
  • Introduce a lot more error checking to cover any possible variances in the client configuration and to cover any failures.
  • Add a ‘for’ loop to simplify adding these settings to a list or all interfaces.

Feel free to attempt these yourself and let me know of any successes (or failures!) in the comments below.

For the complete series to date on Bash Scripting, click here

1 Reply to "Bash Scripting: Proxy Examples"

  • sickboi78

    Any idea how to set the Exclude simple hostnames using a bash script?

    I can’t find a way of doing it anywhere!

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept the Privacy Policy * for Click to select the duration you give consent until.