-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.py
More file actions
127 lines (103 loc) · 3.96 KB
/
Copy pathrunner.py
File metadata and controls
127 lines (103 loc) · 3.96 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3
#
# FILE: runner.py
#
#
# DESCRIPTION: Manage rules
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# AUTHOR: Twinkll Sisodia
# COMPANY: Red Hat
# VERSION: 1.0
# CREATED: 08/21/2020
# ===============================================================================
import argparse
import os
import subprocess
import sys
import threading
import time
import shlex
parser = argparse.ArgumentParser(description='Manage rules')
parser.add_argument('-com', '--command',
action='store',
required=True, dest='command')
parser.add_argument('-c', '--count', type=int,
action='store',
required=True, dest='count')
parser.add_argument('-m', '--mode',
action='store', choices={'debug','help'}, dest='mode')
parser.add_argument('--sys-trace', '--sys',
action='store', choices={'network','memory'}, dest='sys')
args = parser.parse_args()
class TestThreading(object):
# --- check count and also checks the returncode - if it's success or failure
def __init__(self, interval=1):
self.interval = interval
thread = threading.Thread(target=self.run, args=())
thread.start()
thread.join()
def run(self, timeout=0):
num = 0
# --- check and execute ping command with -c
while num < args.count:
print("CALLING 1st process")
process = subprocess.Popen(shlex.split(args.command),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output, errors = process.communicate()
print(shlex.split(args.command))
print(output)
print(errors)
process.poll()
proc = process.pid
# str_proc = str(proc)
print("Printing pid", proc)
num = num + 1
time.sleep(0.2)
if process.returncode == 0:
print("Successful execution")
else:
def netTracing():
command = str(args.command)
print("Calling NETSTAT process")
netTrace = subprocess.Popen(["netstat", "-at", " | ", "grep", str(proc)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# print(netTrace.stdout)
# print(netTrace.stderr)
output, errors = netTrace.communicate()
print(output)
print(errors)
netTrace.poll()
time.sleep(0.2)
# print(netTrace.stdout.readline())
def sysTracing():
print("Calling SYSTRACE process")
sysTrace = subprocess.Popen(["sudo","strace","-s", "80", "-fp", str(proc)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
sysCommand = shlex.split(args.sys)
sysTrace1 = subprocess.Popen(["sudo","strace","-e", "trace=","sysCommand", "-p", str(proc)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output1, errors1 = sysTrace1.communicate()
output, errors = sysTrace.communicate()
sysTrace.poll()
print(output1)
print(errors1)
print(output)
print(errors)
time.sleep(0.2)
print("SYSTRACE RUN FINISHED")
a = threading.Thread(target=netTracing, name='Thread-a')
print("NOW RUNNING NETSTAT")
a.start()
a.join()
b = threading.Thread(target=sysTracing, name='Thread-b')
print("NOW RUNNING SYSTRACE")
b.start()
b.join()
tr = TestThreading()