-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmuvazzaf
295 lines (258 loc) · 7.7 KB
/
muvazzaf
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/bin/bash
# Required commands: ansible sponge ssh grep cat gawk sshpass sed
# shellcheck source=/dev/null
trap FinalCleaning EXIT
STR_SUCCESS="Success"
STR_ERROR="ERROR"
APP_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
test -z "${SCRIPT_DIR}" && SCRIPT_DIR="${APP_DIR}/scripts"
test -z "${COMP_DIR}" && COMP_DIR="${APP_DIR}/components"
test -z "${CACHE_DIR}" && CACHE_DIR="${APP_DIR}/cache"
test -z "${SSH_KEY}" && SSH_KEY="${APP_DIR}/ssh-key"
test -z "${MACHINE_LIST}" && MACHINE_LIST="${APP_DIR}/machine-list"
MACHINE_NUMBER="$(wc -l < "${MACHINE_LIST}")"
ACTIVE_LIST="${CACHE_DIR}/active-machine-list"
PROCESS_ID_LIST="${CACHE_DIR}/proccess-ids"
TIMEOUT_VALUE=5
COMMON_SSH_OPTIONS="-o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=${TIMEOUT_VALUE}"
for COMPONENT in "${COMP_DIR}"/*
do
if [ -f "${COMPONENT}" ]
then
source "${COMPONENT}"
fi
done
function CPUUsage()
{
cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS="" '{printf "%.0f\n", ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}'
}
function MemoryInfo()
{
case ${1,,} in
tenpercentofyourmemory)
awk '/MemTotal/ {print int($2*0.1)}' /proc/meminfo;;
available)
awk '/MemAvailable/ {print $2}' /proc/meminfo;;
esac
}
function WaitIfSystemResourceIsInsufficient()
{
while true
do
if [ "$(MemoryInfo Available)" -gt "$(MemoryInfo TenPercentOfYourMemory)" ] && [ "$(CPUUsage)" -lt "90" ]
then
break
else
sleep 5
fi
done
}
function ColorEcho()
{
# shellcheck disable=SC2034
RED='\e[31m'
# shellcheck disable=SC2034
GREEN='\e[32m'
# shellcheck disable=SC2034
BLUE='\e[34m'
# shellcheck disable=SC2034
YELLOW='\e[1;33m'
# shellcheck disable=SC2034
L_BLUE='\e[1;34m'
# shellcheck disable=SC2034
NONCOLOR='\e[0m'
echo -e "${!1}${2}${NONCOLOR}"
}
function CreateNTLMAccessFile()
{
cat > "${1}" << EOF
[windows:vars]
ansible_user=${2}
ansible_password=${3}
ansible_port=5985
ansible_connection=winrm
ansible_winrm_transport=ntlm
[windows]
${4}
EOF
}
function RunCommandViaWinRM()
{
{ [ -z "${1}" ] || [ -z "${IP}" ] || [ -z "${ACCESS}" ]; } && return 2
ansible "${IP}" -T "${TIMEOUT_VALUE}" -m win_command -a "powershell -ExecutionPolicy Unrestricted -Command \"${1}\"" -i "${ACCESS}"
}
function RunCommandViaSSH()
{
{ [ -z "${1}" ] || [ -z "${IP}" ]; } && return 2
test -z "${PORT}" && local PORT=22
test -z "${USER}" && local USER=root
test -z "${PASSWORD}" || local SSHPASS="sshpass -p ${PASSWORD}"
# shellcheck disable=SC2086
${SSHPASS} ssh -p "${PORT}" -i "${SSH_KEY}" ${COMMON_SSH_OPTIONS} ${SSH_OPTIONS} "${USER}@${IP}" -- "${1}"
}
function RunLocalScriptRemotelyViaSSH()
{
{ [ -z "${1}" ] || [ -z "${IP}" ]; } && return 2
test -z "${PORT}" && local PORT=22
test -z "${USER}" && local USER=root
test -z "${PASSWORD}" || local SSHPASS="sshpass -p ${PASSWORD}"
# shellcheck disable=SC2086
${SSHPASS} ssh -p "${PORT}" -i "${SSH_KEY}" ${COMMON_SSH_OPTIONS} ${SSH_OPTIONS} "${USER}@${IP}" -- sh -s < "${1}"
}
function CopyFilesViaWinRM()
{
{ [ -z "${1}" ] || [ -z "${IP}" ] || [ -z "${ACCESS}" ]; } && return 2
ansible "${IP}" -T "${TIMEOUT_VALUE}" -m win_copy -a "src=${1} dest=${2}" -i "${ACCESS}" > /dev/null 2>&1
}
function CopyFilesViaSSH()
{
{ [ -z "${1}" ] || [ -z "${IP}" ]; } && return 2
test -z "${PORT}" && local PORT=22
test -z "${USER}" && local USER=root
test -z "${PASSWORD}" || local SSHPASS="sshpass -p ${PASSWORD}"
# shellcheck disable=SC2086
${SSHPASS} scp -P "${PORT}" -i "${SSH_KEY}" ${COMMON_SSH_OPTIONS} ${SSH_OPTIONS} "${USER}@${IP}:${1}" "${2}" > /dev/null 2>&1
}
function AddToList()
{
{ [ -z "${UNIQUE}" ] || [ -z "${ACTIVE_LIST}" ]; } && return 2
grep -q "^${UNIQUE}$" "${ACTIVE_LIST}" 2>/dev/null || echo "${UNIQUE}" >> "${ACTIVE_LIST}"
}
function CheckLivesViaWinRM()
{
{ [ -z "${IP}" ] || [ -z "${ACCESS}" ]; } && return 2
ansible "${IP}" -T "${TIMEOUT_VALUE}" -m win_shell -a "whoami;hostname;(Get-WmiObject win32_operatingsystem).caption;(Get-WmiObject -Class Win32_ComputerSystem).Domain;(Get-ItemProperty HKLM:\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine).PowerShellVersion" -i "${ACCESS}"
}
function CheckLivesViaSSH()
{
test -z "${IP}" && return 2
test -z "${PORT}" && local PORT=22
test -z "${USER}" && local USER=root
test -z "${PASSWORD}" || local SSHPASS="sshpass -p ${PASSWORD}"
# shellcheck disable=SC2086
${SSHPASS} ssh -p "${PORT}" -i "${SSH_KEY}" ${COMMON_SSH_OPTIONS} ${SSH_OPTIONS} "${USER}@${IP}" -- 'whoami;cat /etc/issue.net;hostname -s;uname -a'
}
function FinalCleaning()
{
rm -rf "${PROCESS_ID_LIST}"
}
function KeepProcessID()
{
{ [ -z "${1}" ] || [ -z "${UNIQUE}" ]; } && return 2
printf "%s,%s\n" "${UNIQUE}" "${1}" >> "${PROCESS_ID_LIST}"
}
function DeleteProcessID()
{
test -z "${UNIQUE}" && return 2
test -f "${PROCESS_ID_LIST}" || return 1
sed -i "/${UNIQUE}/d" "${PROCESS_ID_LIST}"
sed -i '/^$/d' "${PROCESS_ID_LIST}"
}
function FetchFromMachineList()
{
# 1 MACHINE LIST LINE NUMBER
test -z "${1}" && return 2
sed -n "${1}"p "${MACHINE_LIST}"
}
function ObtainingMachineInformation()
{
unset UNIQUE IP HNAME MACHINE_TYPE PORT SSH_OPTIONS USER PASSWORD
UNIQUE="$(sha256sum <<< "${1}" | awk '{print $1}')"
IP="$(awk -F "," '{print $1}' <<< "${1}")"
HNAME="$(awk -F "," '{print $2}' <<< "${1}")"
MACHINE_TYPE="$(awk -F "," '{print $3}' <<< "${1}")"
PORT="$(awk -F "," '{print $4}' <<< "${1}")"
SSH_OPTIONS="$(awk -F "," '{print $5}' <<< "${1}")"
USER="$(awk -F "," '{print $6}' <<< "${1}")"
PASSWORD="$(awk -F "," '{print $7}' <<< "${1}" | base64 -d)"
}
function Preliminary()
{
MACHINE_WORK_DIR="${CACHE_DIR}/${UNIQUE}"
mkdir -p "${MACHINE_WORK_DIR}"
# Liveness and connectivity checks
case "${MACHINE_TYPE,,}" in
windows)
ACCESS="${MACHINE_WORK_DIR}/access-file"
CreateNTLMAccessFile "${ACCESS}" "${USER}" "${PASSWORD}" "${IP}"
CheckLivesViaWinRM > "${MACHINE_WORK_DIR}/output" 2>&1
local EXIT_CODE=${?}
# This operation is required to clear the DOS-CRLF output
dos2unix < "${MACHINE_WORK_DIR}/output" | sponge "${MACHINE_WORK_DIR}/output"
;;
linux)
echo "${IP}" > "${MACHINE_WORK_DIR}/output"
CheckLivesViaSSH > "${MACHINE_WORK_DIR}/output" 2>&1
local EXIT_CODE=${?}
;;
*)
return 1
esac
if [ ${EXIT_CODE} == 0 ]
then
test -f "${MACHINE_WORK_DIR}/output" && mv "${MACHINE_WORK_DIR}/output" "${MACHINE_WORK_DIR}/machine-info"
AddToList
test ! -z "${HNAME}" || HNAME="$(sed -n "3p" "${MACHINE_WORK_DIR}/machine-info")"
test ! -z "${QUERY}" && ColorEcho "GREEN" "${IP}\t${STR_SUCCESS}"
else
test -f "${MACHINE_WORK_DIR}/output" && mv "${MACHINE_WORK_DIR}/output" "${MACHINE_WORK_DIR}/machine-error"
test ! -z "${QUERY}" && ColorEcho "RED" "${IP}\t${STR_ERROR}"
fi
test ! -z "${QUERY}" && DeleteProcessID
return ${EXIT_CODE}
}
function StartTask()
{
case "${MACHINE_TYPE,,}" in
windows)
source "${SCRIPT_DIR}/windowstask"
;;
linux)
source "${SCRIPT_DIR}/linuxtask"
;;
esac
DeleteProcessID
}
function WaitForProcessesToFinish()
{
until [ ! -s "${PROCESS_ID_LIST}" ]
do
sleep 1
done
}
function MainProcess()
{
rm -rf "${CACHE_DIR}" "${ACTIVE_LIST}"
mkdir -p "${CACHE_DIR}"
T=1
if [ "${1,,}" == "query" ]
then
QUERY=true
while [ ${T} -le "${MACHINE_NUMBER}" ]
do
WaitIfSystemResourceIsInsufficient
unset MACHINE_INFO
local MACHINE_INFO
MACHINE_INFO="$(FetchFromMachineList "${T}")"
T=$((T+1))
ObtainingMachineInformation "${MACHINE_INFO}"
Preliminary &
KeepProcessID "${!}"
done
else
while [ ${T} -le "${MACHINE_NUMBER}" ]
do
WaitIfSystemResourceIsInsufficient
unset MACHINE_INFO
local MACHINE_INFO
MACHINE_INFO="$(FetchFromMachineList "${T}")"
T=$((T+1))
ObtainingMachineInformation "${MACHINE_INFO}"
Preliminary || continue
StartTask &
KeepProcessID "${!}"
done
fi
WaitForProcessesToFinish
}
MainProcess "${@}"