-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocesses.py
More file actions
executable file
·111 lines (86 loc) · 3.08 KB
/
processes.py
File metadata and controls
executable file
·111 lines (86 loc) · 3.08 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
#!/usr/bin/python
# you may need to install these packages on your machine
# sudo pip install paramiko
# sudo apt-get install libffi6, libffi-dev, python2.7-dev, libssl-dev
# sudo pip install cryptography --force-reinstall
import re, sys, shlex, argparse, getpass, paramiko, time
from subprocess import Popen, PIPE
from os import listdir
from os.path import isfile, isdir, join
host_domain = ".informatik.hu-berlin.de"
server_list = [ "gruenau1"
, "gruenau2"
, "gruenau3"
, "gruenau4"
, "gruenau5"
, "gruenau6"
, "gruenau7"
, "gruenau8"
]
def get_num_processes(ssh, verbose):
s = search_for(ssh, "simloid" , verbose)
e = search_for(ssh, "evolution", verbose)
if e != 2*s:
print("WARNING: broken process chain.")
return s
def search_for(ssh, processname, verbose):
cmd = "ps -e | grep '{}'".format(processname)
stdin, stdout, stderr = ssh.exec_command(cmd)
lines = stdout.readlines() + stderr.readlines()
if verbose:
print("\n({1}) {0}".format(processname, len(lines)))
if lines > 0:
for line in lines:
print(line),
return len(lines)
def kill(ssh, processname):
cmd = "pkill -f -9 {}".format(processname)
stdin, stdout, stderr = ssh.exec_command(cmd)
lines = stdout.readlines() + stderr.readlines()
def search_process_on_server(server, user, pswd, verbose):
print("Connecting to: {0}".format(server)),
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(server, username=user, password=pswd, timeout=10)
num = get_num_processes(ssh, verbose)
if not verbose:
print(": {0}". format(num))
ssh.close()
except:
print("FAILED.")
def kill_process_on_server(server, user, pswd):
print("Connecting to: {0}".format(server)),
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(server, username=user, password=pswd, timeout=10)
kill(ssh, "evolution.py")
kill(ssh, "simloid")
kill(ssh, "evolution")
ssh.close()
print("KILLED.")
except:
print("FAILED.")
def search_all(server_list, user, pswd, verbose):
for server in server_list:
search_process_on_server(server+host_domain, user, pswd, verbose)
def kill_all(server_list, user, pswd):
for server in server_list:
kill_process_on_server(server+host_domain, user, pswd)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('-k', '--kill' , action='store_true')
args = parser.parse_args()
user = raw_input("Username: ")
if user == "":
print("Aborted.")
return
pswd = getpass.getpass()
if args.kill:
kill_all(server_list, user, pswd)
else:
search_all(server_list, user, pswd, args.verbose)
print("\n____\nDONE.")
if __name__ == "__main__": main()