-
Notifications
You must be signed in to change notification settings - Fork 1
/
killRemote.sh
executable file
·134 lines (112 loc) · 2.58 KB
/
killRemote.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
131
132
133
134
#! /bin/bash
# input discription
# $1 ---> remote_index: index of remote server (for this script) in remoteAddresses.txt (optional)
# $2 ---> port that want to kill (optional)
DIR=`dirname $0`
#methods
findServer() {
#gets remoteAddresses path and an index as input
#finds the specified server (specification with input index)
#return 0 if succesful 1 if failed
declare -i i=1
declare -i n=$2
input=$1
declare -i wordCount
server=""
serverPath=""
while IFS= read -r line
do
if [[ $i -eq $n ]]; then
wordCount=0
for word in $line
do
tempArr[wordCount]=$word
wordCount=$wordCount+1
done
if [[ $wordCount -eq 3 ]]; then
server=${tempArr[0]}
serverPath=${tempArr[2]}
return 0
elif [[ $wordCount -eq 2 ]]; then
server=${tempArr[0]}
serverPath="Desktop/RemoteGamesFiles"
return 0
else
return 1
fi
fi
i=$i+1
done < $input
return 1
}
killall() {
input=$1
while IFS= read -r line
do
wordCount=0
for word in $line
do
tempArr[wordCount]=$word
wordCount=$wordCount+1
done
server=""
serverPath=""
if [[ $wordCount -eq 3 ]]; then
server=${tempArr[0]}
serverPath=${tempArr[2]}
elif [[ $wordCount -eq 2 ]]; then
server=${tempArr[0]}
serverPath="Desktop/RemoteGamesFiles"
fi
if [[ -n $server ]] && [[ ${server:0:1} != "#" ]]; then
ssh $server "$serverPath/kill.sh" </dev/null
fi
done < $input
}
initializeArr() {
declare -i idx=0
for var in $line ; do
arr[$index]=$var
index=$index+1
done
}
readFileAndKill() {
input=$1
port=$2
while IFS= read -r line; do
initializeArr
#arr[0] ---> name of process
#arr[1] ---> port of process
#arr[2] ---> pid of process
#except start
if ! [[ -n $port ]]; then
if [[ ${arr[0]} = "start" ]] ; then
kill ${arr[1]}
else
kill ${arr[2]}
fi
elif [[ $port = ${arr[1]} ]] && [[ ${arr[0]} = "server" ]]; then
echo ${arr[1]} > $DIR/data/killed.txt
kill ${arr[2]}
fi
done < $input
}
#main method
main() {
server_index=$1
port=$2
if [[ -n $server_index ]]; then
findServer "$DIR/data/remoteAddresses.txt" "$server_index"
if ! [[ -n $port ]] ; then
echo "You must enter the port!"
elif [[ -n $server ]] && [[ ${server:0:1} != "#" ]]; then
ssh $server "$serverPath/kill.sh" $port </dev/null
fi
else
#Local processes kill
readFileAndKill "$DIR/proc.txt" $port
killall "$DIR/data/remoteAddresses.txt"
fi
}
#
main $1 $2