-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote-script
More file actions
executable file
·124 lines (112 loc) · 3.38 KB
/
Copy pathremote-script
File metadata and controls
executable file
·124 lines (112 loc) · 3.38 KB
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
#!/bin/bash
### exec shell script remotely as root
# vars
_SRV=""
_SCRPTSRC="$HOME/remote-scripts"
_LST="$HOME/list"
_LOGFOLDER="$HOME/logs"
_SCRPT="${_LOGFOLDER}/remote.scr"
[ ! -d "$_LOGFOLDER" ] && mkdir "$_LOGFOLDER"
# color
if ! type _MYECHO >/dev/null 2>&1; then
if [ -f "/etc/profile.d/01-myecho-colors.sh" ]; then
source /etc/profile.d/01-myecho-colors.sh >/dev/null 2>&1
elif [ -f "$HOME/01-myecho-colors.sh" ]; then
source "$HOME/01-myecho-colors.sh" >/dev/null 2>&1
else
echo "Nice Output - Install 'myecho' function (in homedir: 01-myecho-colors.sh)"
cd "$HOME"
curl -s -LO https://raw.githubusercontent.com/zephxs/bash/master/functions/01-myecho-colors.sh
head -20 01-myecho-colors.sh |grep -q "^_MYECHO () {" && echo "myecho installed!" || { echo "myecho install failed, exit.."; exit 1; }
source "$HOME/01-myecho-colors.sh" >/dev/null 2>&1
fi
fi
# hekop
_HELPFUNCTION(){
echo -e "### Remote Exec
# execute remote code from: $HOME/remote-script
# on server list: $HOME/list
# local logs location: $HOME/logs/
# or provide script or list file with
# Usage:
-s|--script # script file to execute
-l|--list) # list file with server names
* # server name
# Exemples:
$(basename $0) -s myscript.sh -l server.list
$(basename $0) hostname
"
}
while (( "$#" )); do
case "$1" in
-s|--script)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
_SCRPTSRC="$2"; shift 2;
else
echo "$2 Source Script missing..";
return;
fi
;;
-l|--list)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
_LST="$2"; shift 2;
else
echo "$2 List file missing.."; return;
fi
;;
-h|--help)
_HELPFUNCTION
exit
;;
*) _SRV="$1"; shift
;;
esac;
done
# input command if no script provided or script file is empty
if [ ! -s "${_SCRPTSRC}" ]; then
echo "Input command line to remote exec :"
read -p "# " _INP
echo "$_INP" >> ${_SCRPTSRC}
echo
fi
# show server list and script to run + validation
if [ -s "${_SCRPTSRC}" ]; then
_BLU "# Server list = ${_LST} :"
[ ! -z "$_SRV" ] && echo $_SRV || cat ${_LST}
echo
_BLU "# input command :" | tee -a ${_LOG}
cat ${_SCRPTSRC} | grep -Ev '^$|^#' | tee -a ${_LOG}
echo
_GRN "Press 'c' to Cancel, or any other key to Continue.."
read -n 1
if [ "$REPLY" = 'c' ]; then echo && exit 1 ; fi
fi
# exec function to run on each server
_EXECR () {
_LOG="${_LOGFOLDER}/${_SRV}.$(date +"%d%m%Y_%H%M%S").log"
cat ${_SCRPTSRC} > $_SCRPT
echo "################################" | tee -a ${_LOG}
echo "### Server = $_SRV" | tee -a ${_LOG}
echo "### Date = $(date +'%d/%m/%Y %H:%M')" | tee -a ${_LOG}
echo "### Cmds = " >> ${_LOG}
cat ${_SCRPTSRC} >> ${_LOG}
echo "### Output = " | tee -a ${_LOG}
#ssh -o StrictHostKeyChecking=no -o BatchMode=true -o ConnectTimeout=5 $_SRV TERM=xterm 'sudo -i bash -s' < ${_SCRPT} | tee -a $_LOG
#ssh -o StrictHostKeyChecking=no -o BatchMode=true -o ConnectTimeout=5 $_SRV 'sudo -i bash -s' < ${_SCRPT} | tee -a $_LOG
ssh -o StrictHostKeyChecking=no -o BatchMode=true -o ConnectTimeout=3 $_SRV TERM=xterm 'bash -s' < ${_SCRPT} | tee -a ${_LOG}
echo "" >> ${_LOG}
}
# main
if [ -z "$_SRV" ]; then
if [ -s "${_LST}" ]; then
for _SRV in $(cat ${_LST}); do
_EXECR
done
else
_MAV "No server in file list.."
read -p "Please Enter Server name: " _SRV
_EXECR
fi
else
_EXECR
fi