Last active
December 18, 2015 20:29
-
-
Save nhanb/5840791 to your computer and use it in GitHub Desktop.
For RMIT proxy use. Execute this script everytime you open a new terminal.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ============================================================================= | |
# If the computer is currently connected to RMIT's network then this | |
# script will set the appropriate proxy environment variables, otherwise | |
# it just unsets everything. | |
# | |
# Also includes special proxy configs for: | |
# - git | |
# - apt | |
# - gsettings (which many programs, including Firefox, use) | |
# | |
# Make sure this file is executable using this command: | |
# chmod +x rmitproxy_public.sh | |
# | |
# For ubuntu users, put this script in | |
# /etc/network/if-up.d | |
# to execute it every time your computer connects to a network :) | |
# (but the environment variables will not be set system-wide so you have to | |
# execute it every time you open a new terminal just to be sure) | |
# | |
# Inpired by: | |
# http://askubuntu.com/questions/150210/how-do-i-set-systemwide-proxy-servers-in-xubuntu-lubuntu-or-ubuntu-studio/151047#151047 | |
# ============================================================================= | |
# Sensitive info. Edit where needed. | |
user=s1111111 # Your s-number here | |
pass='password' # Your RMIT password here (surrounded by '' just to be safe) | |
sudopw='someOtherPassword' # Your Linux sudo password | |
host=proxy.rmit.edu.vn | |
port=8080 | |
# ============================================================================= | |
# DO NOT TOUCH anything below unless you know what you're doing! | |
# ============================================================================= | |
# Let root know which X display we're using. Without this, the | |
# notification will not be displayed | |
export DISPLAY=:0.0 | |
# RMIT's SSID | |
ssid=RMITWPA | |
# Check if connected to wired (ethernet) RMIT network. | |
isRmitWired=`ifconfig eth0 | grep 172.16.` # Ugly hack. TODO improve this. | |
rmitOn() { | |
# Standard environment variables | |
http_str=http://$user:$pass@$host:$port | |
https_str=https://$user:$pass@$host:$port | |
http=( http_proxy HTTP_PROXY FTP_PROXY ftp_proxy ) | |
for envar in $http | |
do | |
export $envar=$http_str | |
done | |
https=( https_proxy HTTPS_PROXY ) | |
for envar in $https | |
do | |
export $envar=$https_str | |
done | |
export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com" | |
export NO_PROXY="localhost,127.0.0.1,localaddress,.localdomain.com" | |
# Gnome proxy settings | |
gsettings set org.gnome.system.proxy.http host $host | |
gsettings set org.gnome.system.proxy.https host $host | |
gsettings set org.gnome.system.proxy mode 'manual' | |
# Hardcore stuff ahead - requiring sudo permission | |
# ===================================================================== | |
# /etc/apt/apt.conf - If you ever want to install stuff while at RMIT | |
aptconf=$(echo -e "Acquire::http::proxy \"$http_str\";\nAcquire::https::proxy \"$https_str\";") | |
# Write apt.conf file content to temporary file then move it to its | |
# appropriate location. For some weird reason I can't write directly to | |
# a file in /etc/apt/ | |
echo $aptconf > ~/tmp_apt_conf | |
echo $sudopw | sudo -S mv -f ~/tmp_apt_conf /etc/apt/apt.conf || true | |
# Git | |
git config --global http.proxy "$http_str" || true | |
git config --global https.proxy "$https_str" || true | |
# Comment out the following line if you haven't got a specific gitproxy | |
# executable. I myself have a small script to pipe it through socat. | |
#git config --global core.gitproxy '/bin/gitproxy' | |
notify-send "RMIT Mode On" | |
} | |
rmitOff() { | |
envars=( https_proxy HTTPS_PROXY http_proxy HTTP_PROXY FTP_PROXY ftp_proxy all_proxy ALL_PROXY) | |
for envar in $envars | |
do | |
export $envar="" | |
done | |
# Gnome proxy settings | |
gsettings set org.gnome.system.proxy.http host "" | |
gsettings set org.gnome.system.proxy.https host "" | |
gsettings set org.gnome.system.proxy mode 'none' | |
# Hardcore stuff ahead - requiring sudo permission | |
# ===================================================================== | |
# Switch apt conf file | |
aptconf="" | |
# Write apt.conf file content to temporary file then move it to its | |
# appropriate location. For some weird reason I can't write directly to | |
# a file in /etc/apt/ | |
echo $aptconf > ~/tmp_apt_conf | |
echo $sudopw | sudo -S mv -f ~/tmp_apt_conf /etc/apt/apt.conf || true | |
# Git | |
git config --global http.proxy "" || true | |
git config --global https.proxy "" || true | |
git config --global core.gitproxy '' | |
notify-send "RMIT Mode Off" | |
} | |
# If one of these is true: | |
# - connected to a wired (ethernet) network that has RMIT's IP scheme | |
# - current network SSID is RMIT's | |
# then set up the proxy, otherwise unset everything | |
if [ `iwgetid -s | grep $ssid` ] || [ "$isRmitWired" ] ; then | |
rmitOn | |
echo "Connected to RMIT network. Proxy settings applied." | |
else | |
rmitOff | |
echo "Not on '$ssid'. Proxy settings cleared." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment