-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-command.sh
executable file
·131 lines (119 loc) · 2.38 KB
/
send-command.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
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
#!/bin/bash
#
# send-command.sh
# Nick Accad <[email protected]>
# 2018
#
function runTesting() {
echo $argValue
echo $CombinedFile
echo $HostsFile
echo $Username
echo $Password
echo $ScriptToSend
}
function runSimpleScript() {
chmod 755 $ScriptToSend
for host in `cat $HostsFile`
do
echo -n "Working on $host..."
sshpass -p $Password scp -o StrictHostKeyChecking=no $ScriptToSend root@$host:/tmp/$ScriptToSend
sshpass -p $Password ssh -o StrictHostKeyChecking=no root@$host /tmp/$ScriptToSend
echo "DONE"
done
}
function runCombinedScript() {
echo chmod 755 $ScriptToSend
for line in `cat $CombinedFile`
do
host=`echo $line | cut -d , -f 1`
user=`echo $line | cut -d , -f 2`
pass=`echo $line | cut -d , -f 3`
echo -n "Working on $host..."
sshpass -p $pass scp -o StrictHostKeyChecking=no $ScriptToSend root@$host:/tmp/$ScriptToSend
sshpass -p $pass ssh -o StrictHostKeyChecking=no root@$host /tmp/$ScriptToSend
echo "DONE"
done
}
function usage() {
echo
echo "$0 -u USERNAME -p PASSWORD -f HOSTS_FILE -s SCRIPT_TO_SEND"
echo "$0 -c COMBINED_FILE -s SCRIPT_TO_SEND"
echo
echo "- Sends a script file to a Unix host via scp to /tmp, and then executes that file on the remote host"
echo "- HOSTS_FILE contains a list of hosts to excute this against, one per line"
echo "- COMBINED_FILE is a CSV with HOST,USERNAME,PASSWORD format"
echo "- Requires a copy of SSHPASS available in \$PATH https://sourceforge.net/projects/sshpass/"
echo
echo "Nick Accad <[email protected]> 2018"
echo
}
ArgValue=0
useFunction='runSimple'
while [[ $# -gt 0 ]]
do
key="$1"
case "$key" in
-h|--help)
usage
ArgValue=0
exit 0
;;
-u|--username)
Username=$2
((ArgValue++))
shift
;;
-p|--password)
Password=$2
((ArgValue++))
shift
;;
-f|--hosts)
HostsFile=$2
((ArgValue++))
shift
;;
-s|--script)
ScriptToSend=$2
((ArgValue+=6))
shift
;;
-c|--combined)
useFunction='runCombined'
CombinedFile=$2
((ArgValue+=3))
shift
;;
-d|--testing)
useFunction='runTesting'
((ArgValue+=3))
shift
;;
*)
echo
echo "ERROR: Unknown Parameter $1"
usage
exit 1
;;
esac
shift
done
if [[ $ArgValue -ne 9 ]]
then
echo
echo "ERROR, invalid number of arguments"
usage
exit 1
fi
case "$useFunction" in
runSimple)
runSimpleScript
;;
runCombined)
runCombinedScript
;;
runTesting)
runTesting
;;
esac