Repository navigation
Expand file tree
/
Copy pathcontrolVMs.sh
More file actions
executable file
·61 lines (53 loc) · 1.82 KB
/
Copy pathcontrolVMs.sh
File metadata and controls
executable file
·61 lines (53 loc) · 1.82 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
#!/bin/bash
#Runs commands on multiple machines
#Requires private key authentication and NOPASSWD in sudo
#Usage - ./controlVMs.sh < update | shutdown | -c "command" >
hosts=( "jdc-centos-network" "jdc-centos-app" "jdc-centos-app2" )
operation=$1
[ -z "$operation" ] && echo 'ERROR: Missing arguments, exiting...' && exit -1
for i in ${hosts[@]}
do
if ping -c 1 $i &> /dev/null
then
if [ $operation = "shutdown" ]
then
ssh $i 'sudo systemctl poweroff'
echo "Shutdown command sent to $i."
elif [ $operation = "update" ]
then
ssh $i 'correctpkg=$(whereis apt) && [ "$correctpkg" = "apt:" ] && sudo yum update -y' &
ssh $i 'correctpkg=$(whereis yum) && [ "$correctpkg" = "yum:" ] && sudo apt-get update -y && sudo apt-get upgrade -y' &
echo "Update command sent to $i."
elif [ $operation = "-c" ]
then
comm=$2
echo "Running command on $i - $comm"
echo $comm | xargs ssh $i #need to use this method because passing the command as a variable into ssh does not work
echo "Return code - $?"
else
echo "Invalid operation specified."
fi
else
echo "$i currently not reachable, skipping...."
fi
done
wait
echo "Remote machines processed."
if [ $operation = "shutdown" ]
then
echo "Press any key to shutdown the local machine, or press CTRL-C to exit script."
read -s -n 1 && sudo systemctl poweroff
elif [ $operation = "update" ]
then
echo "Updating local machine....."
correctpkg=$(whereis apt) && [ "$correctpkg" = "apt:" ] && sudo yum update -y
correctpkg=$(whereis yum) && [ "$correctpkg" = "yum:" ] && sudo apt-get update -y && sudo apt-get upgrade -y
elif [ $operation = "-c" ]
then
comm=$2
echo "Running command on local machine - $comm"
$comm
echo "Return code - $?"
else
echo "Invalid operation specified."
fi