-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdistribute_script.py
More file actions
79 lines (73 loc) · 2.88 KB
/
Copy pathdistribute_script.py
File metadata and controls
79 lines (73 loc) · 2.88 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
#!/bin/python
#//- This script's purpose is to automate the distribution and management of scripts across multiple
#//- nodes/servers/sensors. It allows you to copy/distribute, run, or kill scripts from a single
#//- command line execution across numerous systems.
import os, sys, re, json, time
logPath = '/home/assessor/'
startTime = 0.0
def main():
args = sys.argv
args.pop(0) #//- Remove script name from args
#print "ARGS:",args
if len(args) >= 5:
fileName = str(args.pop(0))
userName = str(args.pop(0))
fileDir = str(args.pop(0))
optExec = str(args.pop(0))
if fileDir[-1] != '/':
fileDir += '/'
if not os.path.isfile(fileName):
print "[X] Read Error:\n Script: " + fileName + " does not exist..."
return False
for host in args:
if "copy" in optExec:
cmd = "scp "+fileName+" "+userName+"@"+host+":"+fileDir+"."
os.popen(cmd)
print host+': File '+fileDir+fileName+' Copied.'
if "status" in optExec:
cmd = 'ssh ' + userName + '@' + host + ' ps -ef | grep '+fileDir+fileName+' | grep -v grep'
resp = str(os.popen(cmd).read()).strip().split('\n')
pids = ''
statusStr = {}
if len(resp) and resp[0] != '':
for line in resp:
data = line.split()
if fileDir+fileName in data[-1]:
pids += ' '+data[1].strip()
statusStr[data[1].strip()] = data[4].strip()
#pids = pids.strip()
if pids != '':
#os.popen('ssh ' + userName + '@' + host + ' kill'+pids)
print host+': Process '+fileDir+fileName+' Found:'
for pID in pids.split():
print 'PID: '+pID+'\tStartTime: '+statusStr[str(pID)]
else:
print host+': Process '+fileDir+fileName+' not found.'
if "kill" in optExec:
cmd = 'ssh ' + userName + '@' + host + ' ps -ef | grep '+fileDir+fileName+' | grep -v grep'
resp = str(os.popen(cmd).read()).strip().split('\n')
pids = ''
if len(resp) and resp[0] != '':
for line in resp:
data = line.split()
if fileDir+fileName in data[-1]:
pids += ' '+data[1]
if pids != '':
os.popen('ssh ' + userName + '@' + host + ' kill'+pids)
print host+': Process '+fileDir+fileName+' -- PID:'+pids+' Killed.'
else:
print host+': Process '+fileDir+fileName+' not found.'
if "run" in optExec:
sshCmd = 'nohup /bin/python '+fileDir+fileName+' &'
cmd = 'ssh ' + userName + '@' + host + ' '+sshCmd
os.popen(cmd)
cmd = 'ssh ' + userName + '@' + host + ' ps -ef | grep '+fileDir+fileName+' | grep -v grep'
resp = str(os.popen(cmd).read()).strip().split('\n')
if len(resp) and resp[0] != '':
print host+': Process '+fileDir+fileName+' -- PID: '+resp[0].split()[1]+' Started'
else:
print "[X] Syntax Error:\n script.py fileName userName directory copy/status/kill/run host1 host2 host3...\n"
return False
if __name__ == "__main__":
startTime = time.gmtime()
main()