-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpst.py
executable file
·114 lines (94 loc) · 2.88 KB
/
pst.py
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
#!/usr/bin/env python
"""
Program for showing the hierarchy of processes on a Linux computer
"""
__author__ = "Mike Accardo"
__copyright__ = "Copyright 2019, Mike Accardo"
__license__ = "MIT"
# imports
import traceback
import os
import sys
import subprocess
import argparse
import processparser as pp
file_dir = os.path.dirname(os.path.realpath(__file__))
pst_dir = os.path.join(file_dir, './pst')
sys.path.insert(0, pst_dir)
from _version import __version__
def less(data):
process = subprocess.Popen(["less"], stdin=subprocess.PIPE)
try:
process.stdin.write(data.encode('utf-8'))
process.communicate()
except Exception:
process.terminate()
print(traceback.format_exc())
sys.exit(0)
def my_parse_args():
parser = argparse.ArgumentParser(
description='Show the hierarchy of processes on a Linux computer.')
parser.add_argument(
"-o",
"--output",
action='store',
type=str,
dest='output',
help="directs the output to a file name of your choice")
parser.add_argument(
"-w",
"--write",
action='store_true',
dest='stdout',
help="write to stdout")
parser.add_argument(
"-v",
"--version",
action='version',
version='{version}'.format(version=__version__),
dest='stdout',
help="display version information")
parser.add_argument(
"-u",
"--user",
action='store',
type=str,
dest='user',
help="show only trees rooted at processes of this user")
parser.add_argument(
"-p",
"--pid",
action='store',
type=int,
dest='pid',
help="start at this PID; default is 1 (init)")
args = vars(parser.parse_args())
return args
def main(args):
ps_command = 'ps -e l'
if args['user']:
ps_command = 'ps -fu {}'.format(args['user'])
elif args['pid']:
ps_command = 'ps -p {pid} --ppid {pid} -o pid,ppid,cmd'.format(
pid=args['pid'])
column_header, processes = pp.get_ps_output(ps_command)
# Find the index of the headings that we are interested in
# (PID,PPID,COMMAND)
heading_indexes = pp.get_heading_indexes(column_header)
# Next, using the indexes, extract the process data
process_info = pp.get_process_data(heading_indexes, processes)
# We have all the essential information that we need. Time to build the
# process trees.
process_trees = pp.build_process_trees(process_info)
tree_output = pp.format_process_trees(process_info, process_trees)
if args['output']:
with open(args['output'], 'w') as f:
sys.stdout = f
sys.stdout.write(tree_output)
elif args['stdout']:
sys.stdout.write(tree_output)
else:
less(tree_output)
if __name__ == '__main__':
args = my_parse_args()
main(args)