2021-06-16 20:20:35 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
|
|
from os import path, getpid
|
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
from sys import argv, platform
|
|
|
|
|
|
|
|
|
|
|
|
class Proc:
|
|
|
|
"""
|
|
|
|
Find a running process from Python
|
|
|
|
"""
|
2021-06-17 07:03:53 +03:00
|
|
|
@classmethod
|
|
|
|
def _list_windows(cls):
|
2021-06-16 20:20:35 +03:00
|
|
|
"""
|
|
|
|
Find all running process with wmi
|
|
|
|
:return: list of dictionaries with descriptions of found processes
|
|
|
|
"""
|
|
|
|
execlist = []
|
|
|
|
separate = b'\r\r\n'
|
|
|
|
out, err = Popen(['wmic', 'process', 'get', 'CommandLine,ExecutablePath,Name,ProcessId', '/format:list'],
|
|
|
|
stdout=PIPE, stderr=PIPE).communicate()
|
|
|
|
for line in out.split(separate + separate):
|
|
|
|
execpid, exename, exepath, cmdline = None, None, None, None
|
|
|
|
for subline in line.split(separate):
|
|
|
|
if b'ProcessId=' in subline:
|
|
|
|
execpid = subline.split(b'=')[1].decode('utf-8')
|
|
|
|
if b'Name=' in subline:
|
|
|
|
exename = subline.split(b'=')[1].decode('utf-8')
|
|
|
|
if b'ExecutablePath=' in subline:
|
|
|
|
exepath = subline.split(b'=')[1].decode('utf-8')
|
|
|
|
if b'CommandLine=' in subline:
|
|
|
|
cmdline = subline.split(b'=')[1].decode('utf-8')
|
|
|
|
if execpid and exename:
|
|
|
|
execlist.append({'execpid': execpid, 'exename': exename, 'exepath': exepath, 'cmdline': cmdline})
|
|
|
|
return execlist
|
|
|
|
|
2021-06-17 07:03:53 +03:00
|
|
|
@classmethod
|
|
|
|
def _list_linux(cls):
|
2021-06-16 20:20:35 +03:00
|
|
|
"""
|
|
|
|
Find all running process with ps
|
|
|
|
:return: list of dictionaries with descriptions of found processes
|
|
|
|
"""
|
|
|
|
execlist = []
|
|
|
|
out, err = Popen(['/bin/ps', '-eo', 'pid,args'], stdout=PIPE, stderr=PIPE).communicate()
|
|
|
|
for line in out.splitlines():
|
2021-06-16 22:33:45 +03:00
|
|
|
execpid = line.split()[0].decode('utf-8')
|
|
|
|
exepath = line.split()[1].decode('utf-8')
|
2021-06-16 20:20:35 +03:00
|
|
|
exename = path.basename(exepath)
|
2021-06-16 22:33:45 +03:00
|
|
|
cmdline = line.split(None, 1)[1].decode('utf-8')
|
2021-06-16 20:20:35 +03:00
|
|
|
if execpid and exename:
|
|
|
|
execlist.append({'execpid': execpid, 'exename': exename, 'exepath': exepath, 'cmdline': cmdline})
|
|
|
|
return execlist
|
|
|
|
|
2021-06-17 07:03:53 +03:00
|
|
|
@classmethod
|
|
|
|
def list(cls):
|
2021-06-16 20:20:35 +03:00
|
|
|
"""
|
|
|
|
Find all running process
|
|
|
|
:return: list of dictionaries with descriptions of found processes
|
|
|
|
"""
|
|
|
|
if platform.startswith('linux'):
|
2021-06-17 07:03:53 +03:00
|
|
|
return cls._list_linux()
|
2021-06-16 20:20:35 +03:00
|
|
|
elif platform.startswith('win32'):
|
2021-06-17 07:03:53 +03:00
|
|
|
return cls._list_windows()
|
2021-06-16 20:20:35 +03:00
|
|
|
elif platform.startswith('darwin'):
|
2021-06-17 07:03:53 +03:00
|
|
|
return cls._list_linux()
|
2021-06-16 20:20:35 +03:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2021-06-17 07:03:53 +03:00
|
|
|
@classmethod
|
|
|
|
def search(cls, find: str, exclude: str = None):
|
2021-06-16 20:20:35 +03:00
|
|
|
"""
|
|
|
|
Find specified processes
|
|
|
|
:param find: find process pid, name or arguments
|
|
|
|
:param exclude: exclude process pid, name or arguments
|
|
|
|
:return: list of dictionaries with descriptions of found processes
|
|
|
|
"""
|
|
|
|
proc_found = []
|
2021-06-17 08:59:01 +03:00
|
|
|
try:
|
|
|
|
for proc in cls.list():
|
|
|
|
if exclude and (exclude in proc['execpid'] or exclude in proc['exename'] or
|
|
|
|
exclude in proc['exepath'] or exclude in proc['cmdline']):
|
|
|
|
pass
|
|
|
|
elif find in proc['execpid'] or find in proc['exename'] or \
|
|
|
|
find in proc['exepath'] or find in proc['cmdline']:
|
|
|
|
proc_found.append(proc)
|
|
|
|
except TypeError as ex:
|
|
|
|
print('ON', platform, 'PLATFORM', 'search ERROR:', ex)
|
|
|
|
finally:
|
|
|
|
if len(proc_found) == 0:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return proc_found
|
2021-06-16 20:20:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
|
|
|
args = ArgumentParser(
|
|
|
|
prog='Proc',
|
|
|
|
description='Find a running process from Python',
|
|
|
|
epilog='Dependencies: Python 3 (tested version 3.9.5)'
|
|
|
|
)
|
|
|
|
args.add_argument('--find', type=str, required=False,
|
|
|
|
help='find process pid, name or arguments')
|
|
|
|
args.add_argument('--exclude', type=str, default=None, required=False,
|
|
|
|
help='exclude process pid, name or arguments')
|
|
|
|
args.add_argument('--self', action='store_true', required=False,
|
|
|
|
help='find a clones of self')
|
|
|
|
args = vars(args.parse_args())
|
|
|
|
if args['find']:
|
|
|
|
processes = Proc.search(args['find'], args['exclude'])
|
|
|
|
elif args['self']:
|
|
|
|
processes = Proc.search(' '.join(argv), str(getpid()))
|
|
|
|
else:
|
|
|
|
processes = Proc.list()
|
|
|
|
if processes:
|
|
|
|
for process in processes:
|
|
|
|
print(process)
|