-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathset-proxyVariables.zsh
176 lines (150 loc) · 5.45 KB
/
set-proxyVariables.zsh
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env zsh
: <<'COMMENT-BLOCK'
.SYNOPSIS
Sets organization's proxy variables in system shell profile and creates files if missing.
.DESCRIPTION
Sets organization's proxy variables in system shell profile and creates files if missing.
Setting these values ensures command-line operations, like curl, are proxy-aware and can function with minimal user impact.
.EXAMPLE
set-ProxyVariables.zsh
.NOTES
Terminal.app will need to be restarted fully in order for printenv to reflect values
Author: hkystar35
Date Created: 2023-04-19 16:41 MST
Github: https://github.com/hkystar35/MDM/new/main/Scripts/set-proxyVariables.zsh
History:
2023-04-19 hkystar35 - created script
2023-04-19 hkystar35 - added comment blocks and timestamps to line entries for auditing
2023-04-28 hkystar35 - updated variables to include UPPER and lower cases and used variables
2023-06-21 hkystar35 - re-wrote script to NOT mess with user's profiles
- /etc/zshrc is modified with a test command to try sourcing "/etc/${orgShortName:l}/${orgShortName:l}_shell_proxySettings.sh"
- "/etc/${orgShortName:l}/${orgShortName:l}_shell_proxySettings.sh" contains the proxy variables
- backup created of /etc/zshrc
- log function writes to /etc/$orgShortName/$log_file
COMMENT-BLOCK
# Jamf built-in variables
## comment out or modify these if not using Jamf's built in script runner
mountPoint="$1"
computerName="$2"
userName="$3"
# Script Parameters
## Be sure to modify the numbers if not using Jamf's script runner
orgName="$4" # Jamf parameter name: Full Organization Name, like Example International, LLC
orgShortName="$5" # Jamf parameter name: Org short name, like Example. Will be lower-cased in script.
webproxy_domain="$6" # Jamf parameter name: Proxy domain, like proxy.example.com (don't include http prefixes)
webproxy_port="$7" # Jamf parameter name: Proxy port, like 80
noProxyString="$8" # Jamf parameter name: No Proxy string, comma separated list like .example.com,127.0.0.1,localhost,*.example.com
webproxy_prefix="http"
webproxy_URL="$webproxy_prefix://$webproxy_domain"
webproxy_fullURL="$webproxy_prefix://$webproxy_domain:$webproxy_port"
# Global settings file
globalSettings_FullPath="/etc/${orgShortName:l}/${orgShortName:l}_shell_proxySettings.sh"
globalSettings_Folder="${globalSettings_FullPath:h}"
globalSettings_FileName="${globalSettings_FullPath:t}"
# Script variables
log_file="${globalSettings_Folder}/${globalSettings_FileName:t:r}.log"
system_shells=("/etc/profile" "/etc/zshenv")
# Functions
# log [-e] [-f FILE] MESSAGE...
function log() {
function timestamp() {
date '+%F %T %Z'
}
set -eu
local prefix="$(timestamp)"
local stream=1
local files=("$log_file")
# handle options
while ! ${1+false}; do
case "$1" in
-e | --error)
prefix="$(timestamp) ERROR:"
stream=2
;;
-f | --file)
shift
files+=("${1-}")
;;
--)
shift
break
;; # end of arguments
-*)
log -e "log: invalid option '$1'"
return 1
;;
*) break ;; # start of message
esac
shift
done
if ${1+false}; then
log -e "log: no message!"
return 1
fi
# if we have a prefix, update our argument list
if [ "$prefix" ]; then
set -- "$prefix " "$@"
fi
# now perform the action
for file ("$files[@]"); do
/bin/mkdir -p $file:h
/usr/bin/touch $file
done
printf '%b' "$@" '\n' | tee -a "${files[@]}" >&$stream
}
function lines() {
local ret
reply=( "${(@f)$(cat -- "$@"; ret=$?; echo .; exit $ret)}" )
ret=$?
reply[-1]=( ${reply[-1]%.} )
return $ret
}
# Main script
log " ###"
log "START of script"
log " ==="
# Log variables
log "Script Param | Organization Name: $orgName"
log "Script Param | Organization Short Name: $orgShortName"
log "Script Param | Web Proxy full URL: $webproxy_fullURL"
log "Script Variable | Log folder: $globalSettings_Folder"
log "Script Variable | Log file: $log_file"
log "Script Variable | Target settings file: $globalSettings_FullPath"
# Create files and folders
log "Creating folder path: $globalSettings_Folder"
/bin/mkdir -p "$globalSettings_Folder"
log "Creating file: $globalSettings_FullPath"
/usr/bin/touch "$globalSettings_FullPath"
# $globalSettings_FullPath Content
cat << GLOBALSETTINGSBLOCK > "$globalSettings_FullPath"
# $orgName proxy settings
proxyDef="$webproxy_fullURL"
export http_proxy="\${proxyDef}"
export HTTP_PROXY="\${proxyDef}"
export https_proxy="\${proxyDef}"
export HTTPS_PROXY="\${proxyDef}"
export no_proxy="$noProxyString"
export NO_PROXY="$noProxyString"
GLOBALSETTINGSBLOCK
log "Added proxy settings to $globalSettings_FullPath"
i=0
lines "$globalSettings_FullPath" &&
for line in "$reply[@]"; do
((++i))
log " Line$i | $line"
done
# Modify System Shells
for shell in "$system_shells[@]"; do
log "Creating file: $shell"
/usr/bin/touch "$shell"
log "Adding missing lines to $shell, if needed"
backup_shell="$shell.bak"
/bin/cp $shell $backup_shell
shell_line1="# $orgName Proxy settings"
grep -qxF "$shell_line1" $shell || echo "$shell_line1" >> $shell
shell_line2="test -f \"$globalSettings_FullPath\" && source \"$globalSettings_FullPath\""
grep -qxF "$shell_line2" $shell || echo "$shell_line2" >> $shell
done
log " ==="
log "END of script"
log " ###"