You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.6 KiB
Python
96 lines
2.6 KiB
Python
import core
|
|
import os
|
|
import subprocess
|
|
from multiprocessing import Process, Manager, Pool, TimeoutError, freeze_support
|
|
from sys import platform
|
|
from time import sleep
|
|
import csv
|
|
|
|
displaydata = None
|
|
settings = None
|
|
netdata_res = None
|
|
procdata_res = None
|
|
|
|
def run_ps(cmd):
|
|
if platform == "win32":
|
|
completed = subprocess.run(["powershell", "-Command", cmd], capture_output=True)
|
|
return completed
|
|
|
|
def netstat():
|
|
print("netstat started")
|
|
data = run_ps("netstat -n -o")
|
|
return data
|
|
|
|
def netstat_done(res):
|
|
print("netstat done")
|
|
procdata_res = pool.apply_async(process_netstat, (res,))
|
|
#netdata_res = pool.apply_async(netstat)
|
|
|
|
def process_netstat(data):
|
|
print("netstat processing")
|
|
#if platform == 'win32':
|
|
#output = data.stdout
|
|
#print(output)
|
|
output = data.stdout.decode().split('\r\n')
|
|
|
|
output2 = list(range(len(output)))
|
|
for x in range(len(output)):
|
|
output2[x] = output[x].split(" ")
|
|
output2[x] = [i for i in output2[x] if i]
|
|
output2 = [i for i in output2 if i]
|
|
print(output2)
|
|
output2 = output2[2:]
|
|
with open("out.csv", "w", newline="") as f:
|
|
writer = csv.writer(f)
|
|
writer.writerows(output2)
|
|
print("done")
|
|
|
|
|
|
def mainloop(pool):
|
|
# worker pool: netstat, netstat cleanup, upload, download, ui tasks
|
|
print("start loop")
|
|
global netdata_res
|
|
global procdata_res
|
|
global rawdata
|
|
#print(res.get(timeout=1))
|
|
if netdata_res is None or netdata_res.ready():
|
|
#rawdata = netdata_res.get()
|
|
#procdata_res = pool.apply_async(process_netstat, (rawdata))
|
|
print("netstat starting")
|
|
netdata_res = pool.apply_async(netstat, callback=netstat_done)
|
|
sleep(10)
|
|
|
|
if __name__ == '__main__':
|
|
freeze_support() # required if packaged into EXE
|
|
# create manager to share data to me, background, foreground
|
|
# create worker pool
|
|
with Pool(processes=5) as pool:
|
|
with Manager() as manager:
|
|
displaydata = manager.list(range(2)) # data to be printed
|
|
settings = manager.list(range(20)) # configuration
|
|
# launch background UI app as process
|
|
p = Process(target=core.background, args=(displaydata,settings))
|
|
p.start()
|
|
#p.join() # not a foreground job, so let's not join it
|
|
keeprunning = True
|
|
|
|
# initial setup
|
|
#netdata_res = pool.apply_async(netstat, callback=netstat_done)
|
|
|
|
|
|
# launch loop
|
|
while(keeprunning):
|
|
mainloop(pool)
|
|
|
|
|
|
|
|
|
|
# launch main, non-blocking, loop
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|