-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
107 lines (83 loc) · 3.3 KB
/
Copy pathexample.py
File metadata and controls
107 lines (83 loc) · 3.3 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
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('-fc', '--failed-count', type=int,
# action='store',
# required=True, dest='failed')
parser.add_argument('-m', '--mode',
action='store', choices={'debug','help'}, dest='mode')
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(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)
output, errors = sysTrace.communicate()
sysTrace.poll()
# print(sysTrace.stdout)
# print(sysTrace.stderr)
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()