-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_tree.bash
More file actions
101 lines (85 loc) · 3 KB
/
process_tree.bash
File metadata and controls
101 lines (85 loc) · 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
GLOBAL_LIST=""
hbf_pidtree() {
#Give a pid as an argument
local -x parent=$1
local -x list
while [ "$parent" ]
do
#If list is empty, start a new one else append
if [ -n "$list" ]
then
list="$list,$parent"
else
list="$parent"
fi
#Make sure that the list is clean with only commas in the middle (if any)
parent=$(echo "$parent" | sed -e 's/,,*/,/g' -e 's/,$//g' -e 's/^,//g')
# ps - get processes with this ID as a parent
#sed - filter out all but numerical characters (no spaces etc...)
# tr - translate newline characters (multiple processes with one parent) to commas.
#sed - remove any multiple commas
#sed - remove any trailing comma
parent=$(ps --ppid "$parent" -o pid h \
| sed -e 's/[^0-9]//g' \
| tr '\n' ',' \
| sed -e 's/,,*/,/g' \
-e 's/,$//g')
done
#Use ps to print the details of all processes in the list
ps -f -p "$list" f
#Update the GLOBAL list (split by pipe)
GLOBAL_LIST="$list,${GLOBAL_LIST}"
}
#View running .sh scripts
bf_script_view() {
GLOBAL_LIST=""
local -i produce_output=1
#get all pids for entries in ps ending in .sh or .bash (all running shell scripts)
for USERPID in `ps ax | grep "\.sh\|\.bash" | grep -v "grep" | sed 's/^ *//g' | cut -d " " -f 1`
do
produce_output=1
#Check if this PID is already in the global list (output has already been produced)
#If it is, switch to not produce any output
for GLOBAL_LIST_PID in `echo "${GLOBAL_LIST}" | tr ',' '\n' `
do
if [ $USERPID -eq $GLOBAL_LIST_PID ]
then
produce_output=0
break
fi
done
if [ $produce_output -eq 1 ]
then
# Produce tree for PID
hbf_pidtree "$USERPID"
fi
done
}
#View processes of all SSH users
bf_session_view() {
GLOBAL_LIST=""
local -i produce_output=1
# Filter for parent pid types:
# - sshd: ssh connections
# - SCREEN screen sessions (even if they aren't actively connected)
# - /bin/login login, shows if anyone has an active session physicaly on the machine (rather than virtual ssh)
for USERPID in `ps ax | grep "sshd: .*@pts/\|SCREEN\|/bin/login" | grep -v "grep" | sed 's,^ *,,g' | cut -d " " -f 1`
do
produce_output=1
#Check if this PID is already in the global list (output has already been produced)
#If it is, switch to not produce any output
for GLOBAL_LIST_PID in `echo "${GLOBAL_LIST}" | tr ',' '\n' `
do
if [ $USERPID -eq $GLOBAL_LIST_PID ]
then
produce_output=0
break
fi
done
if [ $produce_output -eq 1 ]
then
# Produce tree for PID
hbf_pidtree "$USERPID"
fi
done
}