Skip to content

Instantly share code, notes, and snippets.

@yvh
Created August 11, 2014 10:42
Show Gist options
  • Save yvh/30337a800c38f4d988f3 to your computer and use it in GitHub Desktop.
Save yvh/30337a800c38f4d988f3 to your computer and use it in GitHub Desktop.
Set proxy in command line for ubuntu/gnome
#!/bin/bash
# Author Yannick Vanhaeren
# Version 0.1
# Functions
############
function usage
{
echo "usage: set_proxy [-p] | [-s] | [-h] mode"
}
# Main
#######
HTTP=1
SOCKS=1
while getopts ":achr" opt; do
case $opt in
p)
# Remove http
HTTP=0
shift
;;
s)
# Remove socks
SOCKS=0
shift
;;
h)
usage
exit
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
if [ -z "$1" ]; then
echo -e "\e[0;31mYou must specify mode\e[0m"
exit 1
fi
HOST='localhost'
HTTP_PORT=8080
SOCKS_PORT=1080
AUTO_URL="http://localhost/proxy.pac"
gsettings set org.gnome.system.proxy.http host ""
gsettings set org.gnome.system.proxy.http port 0
gsettings set org.gnome.system.proxy.https host ""
gsettings set org.gnome.system.proxy.https port 0
gsettings set org.gnome.system.proxy.socks host ""
gsettings set org.gnome.system.proxy.socks port 0
gsettings set org.gnome.system.proxy autoconfig-url ""
gsettings set org.gnome.system.proxy ignore-hosts "['localhost', '127.0.0.0/8']"
case $1
in
manual)
if [ $HTTP = 1 ]; then
gsettings set org.gnome.system.proxy.http host "$HOST"
gsettings set org.gnome.system.proxy.http port $HTTP_PORT
gsettings set org.gnome.system.proxy.https host "$HOST"
gsettings set org.gnome.system.proxy.https port $HTTP_PORT
fi
if [ $SOCKS = 1 ]; then
gsettings set org.gnome.system.proxy.socks host "$HOST"
gsettings set org.gnome.system.proxy.socks port $SOCKS_PORT
fi
gsettings set org.gnome.system.proxy mode 'manual'
notify-send "Proxy Mode: proxy mode set to Manual"
;;
auto)
gsettings set org.gnome.system.proxy autoconfig-url "$AUTO_URL"
gsettings set org.gnome.system.proxy mode 'auto'
notify-send "Proxy Mode: proxy mode set to Auto"
;;
none)
gsettings set org.gnome.system.proxy mode 'none'
notify-send "Proxy Mode: proxy mode set to None"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment