-
Notifications
You must be signed in to change notification settings - Fork 0
/
multissh.sh
executable file
·89 lines (61 loc) · 2.54 KB
/
multissh.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
88
#!/usr/bin/env bash
echo ""
source "/usr/local/turbolab.it/bash-fx/bash-fx.sh"
fxHeader "🦝🦝 multissh 🦝🦝"
fxConfigLoader "$1"
if [ ! -z "$2" ]; then
MSSH_TARGET_HOSTS_LOCAL_FILE=$2
fi
if [ ! -z "$3" ]; then
MSSH_SCRIPT_LOCAL_FILE=$3
fi
fxTitle "Current config: "
echo "Profile: ##${SCRIPT_NAME}-${1}.conf##"
echo "Target: ##${MSSH_TARGET_HOSTS_LOCAL_FILE}##"
echo "Script: ##${MSSH_SCRIPT_LOCAL_FILE}##"
echo "SSH login: ##${MSSH_REMOTE_LOGIN_USERNAME}##"
echo "Remote run-as: ##${MSSH_REMOTE_RUN_AS_USERNAME}##"
if [ ! -f "${MSSH_TARGET_HOSTS_LOCAL_FILE}" ]; then
fxCatastrophicError "Target hosts file ##${MSSH_TARGET_HOSTS_LOCAL_FILE}## NOT FOUND!"
fi
if [ ! -f "${MSSH_SCRIPT_LOCAL_FILE}" ]; then
fxCatastrophicError "Script file ##${MSSH_SCRIPT_LOCAL_FILE}## NOT FOUND!"
fi
function sectionText()
{
echo -e "\e[1;33m${1}\e[0m"
}
echo ""
while read -r line || [[ -n "$line" ]]; do
FIRSTCHAR="${line:0:1}"
if [ "$FIRSTCHAR" != "#" ] && [ "$FIRSTCHAR" != "" ]; then
MSSH_USER_AT_HOST=${MSSH_REMOTE_LOGIN_USERNAME}@${line}
echo -e "\e[1;43m🏁 ======= MULTISSH ON ${line} is RUNNING =======\e[0m"
echo ""
ssh -tt ${MSSH_USER_AT_HOST} 'echo -e "\e[1;33mRunning on $(hostname)\e[0m"' </dev/null
echo ""
MSSH_SCRIPT_REMOTE_FILE=/tmp/mssh-script-to-execute.sh
sectionText "Uploading to ${MSSH_USER_AT_HOST}:${MSSH_SCRIPT_REMOTE_FILE}"
scp "$MSSH_SCRIPT_LOCAL_FILE" ${MSSH_USER_AT_HOST}:"${MSSH_SCRIPT_REMOTE_FILE}"
echo ""
if [ -z "$MSSH_REMOTE_RUN_AS_USERNAME" ]; then
ssh -tt ${MSSH_USER_AT_HOST} "echo -e \"\e[1;33mRunning the remote script as $(whoami) \e[0m\"" </dev/null
ssh -tt ${MSSH_USER_AT_HOST} "bash \"${MSSH_SCRIPT_REMOTE_FILE}\"" </dev/null
else
ssh -tt ${MSSH_USER_AT_HOST} "echo -e \"\e[1;33mRunning the remote script as ${MSSH_REMOTE_RUN_AS_USERNAME} \e[0m\"" </dev/null
echo ""
ssh -tt ${MSSH_USER_AT_HOST} "sudo -u ${MSSH_REMOTE_RUN_AS_USERNAME} -H bash \"${MSSH_SCRIPT_REMOTE_FILE}\"" </dev/null
fi
echo ""
sectionText "Remove the script from remote..."
ssh -tt ${MSSH_USER_AT_HOST} "rm -f \"${MSSH_SCRIPT_REMOTE_FILE}\"" </dev/null
echo ""
if [ ! -z "${MSSH_POST_EXEC_SCRIPT}" ]; then
sectionText "Running the post-exec script..."
bash "${MSSH_POST_EXEC_SCRIPT}" ${MSSH_REMOTE_LOGIN_USERNAME} ${line} "$MSSH_TARGET_HOSTS_LOCAL_FILE" ${MSSH_REMOTE_RUN_AS_USERNAME}
fi
fxOK "======= MULTISSH ON ${line} is DONE ======="
echo ""
fi
done < "$MSSH_TARGET_HOSTS_LOCAL_FILE"
fxEndFooter