NetworkManager Script to Set HTTP Proxy

While Gnome and it’s derivatives support automatic proxy detection,  it do not work well for all programs, particularly for command line programs.   I’ve found  that using simple script in /etc/NetworkManager/dispatcher.d works better for me, which sets and unsets fixed proxy works better.   NM dispatcher scripts are run each time network connections change (network up, down, VPN connect etc.) and received two parameters ( interface name and status) and bunch of environment variables.

This script is useful for exploring how NM dispatcher scripts are called:

For proxy activation and deactivation we first need to set it up in Network preferences as manual proxy (or you can use dconf_editor – which enables to setup also URLs to bypass proxy – look under key system/proxy).  To activate or deactivate proxy we can later just switch between manual and none proxy modes (which we can  do easily with gsettings command).

Here is final script:

Some gotchas:

  • Scripts must have proper permissions,   NetworkManager runs only executable scripts and ignores scripts with too permissive access ( write for others)
  • Script runs under root, but gsettings and send-notify should be run under your user account and they also need current desktop dbus address (DBUS env.variable in your desktop session)
  • in case of DHCP ‘up’ status is often not enough – interface can be up, but yet not received IP address from DHCP server, we should look also for dhcp4-change status
  • Skype does not handle change of proxy (from manual to none and vice versa), this function could be used to restart skype (not yet fully reliable):

     

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">