-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestPython
More file actions
76 lines (60 loc) · 1.99 KB
/
testPython
File metadata and controls
76 lines (60 loc) · 1.99 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
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 02 15:16:13 2017
@author: leo
"""
import paramiko
import numpy as np
def sshclient_execmd(hostname, port, username, password, execmd="ps -ef|grep mqueue|grep -v \"grep\"|grep -v \"statu_mqueue\""):
paramiko.util.log_to_file("paramiko.log")
#process_time = 'ps -eo pid,lstart etime | grep'
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname=hostname, port=port, username=username, password=password)
stdin, stdout, stderr = s.exec_command (execmd)
stdin.write("Y") # Generally speaking, the first connection, need a simple interaction.
strout = stdout.read()
save('out.txt', strout)
#print strout
s.close()
pid = []
mask = []
time = []
date = []
with open('out.txt') as f:
for line in f.readlines():
ll = line.split()
pid.append(ll[0])
time.append(ll[2])
date.append(ll[1])
if len(ll[2]) >8:
mask.append(1)
else:
mask.append(0)
print 'pid',pid
print 'mask.num:',len(pid)
return pid,mask,time,date
def save(filename, contents):
fh = open(filename, 'w')
fh.write(contents)
fh.close()
def main():
hostname = '172.24.1.196'
port = 22
username = 'mqm'
password = 'decnet'
# execmd = "ls"
code ="ps -A -opid,stime,etime,args|grep mqueue|grep -v \"grep\"|grep -v \"statu_mqueue\""
a = tuple()
a = sshclient_execmd(hostname, port, username, password, code)
mask = np.array(a[1])
pidd = np.array(a[0])
kill_list = pidd[mask>0]
#print kill_list
for id in kill_list:
kill = "kill -9 " + id
sshclient_execmd(hostname, port, username, password, kill)
#kill = "kill -9 " + a[0][0]
#sshclient_execmd(hostname, port, username, password, kill)
if __name__ == "__main__":
main()