-
Notifications
You must be signed in to change notification settings - Fork 0
/
play_with_proxy.sh
87 lines (86 loc) · 2.76 KB
/
play_with_proxy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# !/bin/bash
# Author: Kushal K S V S
# Script to change the proxy settings in command line in macos.
# Takes user input and then changes the proxy state.
# Function to check the proxy settings.
function check_proxy(){
echo -e "\nHTTP Proxy State";
echo "$(networksetup -getwebproxy "Wi-Fi")";
echo -e "\nHTTPS Proxy State";
echo "$(networksetup -getsecurewebproxy "Wi-Fi")";
}
# Function to set the proxy settings.
function set_proxy(){
# Checking if username is empty.
if [ $# == 2 ]; then
networksetup -setsecurewebproxy "Wi-Fi" $1 $2;
networksetup -setwebproxy "Wi-Fi" $1 $2;
else
networksetup -setsecurewebproxy "Wi-Fi" $1 $2 on $3 $4;
networksetup -setwebproxy "Wi-Fi" $1 $2 on $3 $4;
fi
}
# This is the proxy.presets file path. Use absolute path.
PRESETS_FILE_PATH=$HOME"/proxy.presets";
# Number of presets. Doing by counting the number of lines.
NUMBER_OF_PRESETS="$(grep -v '^[[:space:]]*$' $PRESETS_FILE_PATH | wc -l)"
# Checking what proxy is in place.
check_proxy;
# Loding presets from the proxy.presets file.
# If you change the location of the presets file, change the command accordingly.
source $PRESETS_FILE_PATH;
# List of things to choose from
echo -e "\nChoose an option number below\n";
echo "1 Enable Proxy"
echo "2 Disable Proxy"
echo "3 Change Proxy"
# Declare an array to store all the preset names.
declare -a ARRAY_OF_PRESETS
# Reading the file and storing all the preset names and printing options.
#If you change the location of the presets file, change the here too.
COUNT=0;
while read line
do
IFS='='
read -ra ARRAY <<< "$line"
echo "$((COUNT+4)). Set Proxy=$ARRAY"
ARRAY_OF_PRESETS[$COUNT]=$ARRAY;
COUNT=$((COUNT+1));
done < $PRESETS_FILE_PATH;
#Taking input from the user
read choice;
#Action based on the user input
if [ $choice -gt $((NUMBER_OF_PRESETS+3)) ]; then
echo "...................";
echo "Not a valid option.";
echo "...................";
exit 0;
fi
if [ $choice == 1 ]; then
#Switching the proxy 'ON'
networksetup -setsecurewebproxystate "Wi-Fi" on;
networksetup -setwebproxystate "Wi-fi" on;
check_proxy;
elif [ $choice == 2 ]; then
#Switching the proxy 'OFF'
networksetup -setwebproxystate "Wi-fi" off;
networksetup -setsecurewebproxystate "Wi-Fi" off;
check_proxy;
elif [ $choice == 3 ]; then
#Taking proxy details from the user.
#If not authenticated leave username and password fields empty.
echo "Enter server address";
read SERVER;
echo "Enter port number";
read PORT;
echo "Enter the username if authenticated"
read USERNAME;
echo "Enter the password if authenticated"
read PASSWORD;
set_proxy $SERVER $PORT $USERNAME $PASSWORD;
check_proxy;
elif [ $choice > 3 ]; then
CONFIG_STRING=${!ARRAY_OF_PRESETS[$((choice-4))]}
eval set_proxy $CONFIG_STRING
check_proxy;
fi