FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
lsofgraph-python/lsofgraph.py at master · akme/lsofgraph-python · GitHub
akme
/
lsofgraph-python
Public
Notifications
You must be signed in to change notification settings
Fork
10
Star
131
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
lsofgraph-python
/
lsofgraph.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
187 lines (163 loc) · 6.44 KB
Breadcrumbs
lsofgraph-python
/
lsofgraph.py
Copy path
File metadata and controls
187 lines (163 loc) · 6.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python
# Convert the output of `lsof -F` into PID USER CMD OBJ
import
sys
def
parse_lsof
():
procs
=
{}
cur
=
{}
proc
=
{}
file
=
{}
for
line
in
sys
.
stdin
:
if
line
.
startswith
(
"COMMAND"
):
print
(
"did you run lsof without -F?"
)
exit
(
1
)
tag
=
line
[
0
]
val
=
line
[
1
:].
rstrip
(
'
\n
'
)
if
tag
==
'p'
:
if
val
not
in
procs
:
proc
=
{
'files'
: []}
file
=
None
cur
=
proc
procs
[
val
]
=
proc
else
:
proc
=
{}
cur
=
{}
elif
tag
==
'f'
and
proc
:
file
=
{
'proc'
:
proc
}
cur
=
file
proc
[
'files'
].
append
(
file
)
if
cur
:
cur
[
tag
]
=
val
# skip kernel threads
if
proc
:
if
file
and
all
(
k
in
file
.
keys
()
for
k
in
(
"f"
,
"t"
)):
if
file
[
'f'
]
==
"txt"
and
file
[
't'
]
==
"unknown"
:
procs
[
proc
[
'p'
]]
=
{}
proc
=
{}
file
=
None
cur
=
None
return
procs
def
find_connections
(
procs
):
cs
=
{
'fifo'
: {},
'unix'
: {},
'tcp'
: {},
'udp'
: {},
'pipe'
: {}
}
for
pid
,
proc
in
procs
.
items
():
if
'files'
in
proc
:
for
_
,
file
in
enumerate
(
proc
[
'files'
]):
if
't'
in
file
and
file
[
't'
]
==
"unix"
:
if
'i'
in
file
:
i
=
file
[
'i'
]
if
i
in
cs
[
'unix'
]:
cs
[
'unix'
][
i
].
append
(
file
)
else
:
cs
[
'unix'
][
i
]
=
[]
cs
[
'unix'
][
i
].
append
(
file
)
else
:
i
=
file
[
'd'
]
if
i
in
cs
[
'unix'
]:
cs
[
'unix'
][
i
].
append
(
file
)
else
:
cs
[
'unix'
][
i
]
=
[]
cs
[
'unix'
][
i
].
append
(
file
)
if
't'
in
file
and
file
[
't'
]
==
"FIFO"
:
if
'i'
in
file
:
if
file
[
'i'
]
in
cs
[
'fifo'
]:
cs
[
'fifo'
][
file
[
'i'
]].
append
(
file
)
else
:
cs
[
'fifo'
][
file
[
'i'
]]
=
[]
cs
[
'fifo'
][
file
[
'i'
]].
append
(
file
)
if
't'
in
file
and
file
[
't'
]
==
"PIPE"
:
for
n
in
file
[
'n'
].
lstrip
(
'->'
):
ps
=
{
file
[
'd'
],
n
}
ps
=
sorted
(
ps
)
if
len
(
ps
)
==
2
:
id
=
ps
[
0
]
+
"
\\
n"
+
ps
[
1
]
fs
=
cs
[
'pipe'
]
if
id
in
fs
:
fs
[
id
].
append
(
file
)
else
:
fs
[
id
]
=
[]
fs
[
id
].
append
(
file
)
if
't'
in
file
and
(
file
[
't'
]
==
'IPv4'
or
file
[
't'
]
==
'IPv6'
):
if
'->'
in
file
[
'n'
]:
a
,
b
=
file
[
'n'
].
split
(
"->"
)
ps
=
{
a
,
b
}
ps
=
sorted
(
ps
)
if
len
(
ps
)
==
2
:
id
=
ps
[
0
]
+
"
\\
n"
+
ps
[
1
]
if
file
[
'P'
]
==
"TCP"
:
fs
=
cs
[
'tcp'
]
if
id
in
fs
:
fs
[
id
].
append
(
file
)
else
:
fs
[
id
]
=
[]
fs
[
id
].
append
(
file
)
else
:
fs
=
cs
[
'udp'
]
if
id
in
fs
:
fs
[
id
].
append
(
file
)
else
:
fs
[
id
]
=
[]
fs
[
id
].
append
(
file
)
return
cs
def
print_graph
(
procs
,
conns
):
colors
=
{
'fifo'
:
"green"
,
'unix'
:
"purple"
,
'tcp'
:
"red"
,
'udp'
:
"orange"
,
'pipe'
:
"blue"
}
# Generate graph
print
(
"digraph G {"
)
print
(
"
\t
graph [ center=true, margin=0.2, nodesep=0.1, ranksep=0.3, rankdir=LR];"
)
print
(
"
\t
node [ shape=box, style=
\"
rounded,filled
\"
width=0, height=0, fontname=Helvetica, fontsize=10];"
)
print
(
"
\t
edge [ fontname=Helvetica, fontsize=10];"
)
# Parent/child relationships
for
pid
,
proc
in
procs
.
items
():
if
'R'
in
proc
and
proc
[
'R'
]
==
"1"
:
color
=
"grey70"
else
:
color
=
"white"
if
'p'
in
proc
and
'n'
in
proc
:
print
(
"
\t
p%s [ label =
\"
%s
\\
n%s %s
\"
fillcolor=%s ];"
%
(
proc
[
'p'
],
proc
[
'n'
],
proc
[
'p'
],
proc
[
'L'
],
color
))
elif
'p'
in
proc
:
if
'L'
in
proc
:
# there could be no L flag if process running, but user was removed. lsof: no pwd entry for UID
print
(
"
\t
p%s [ label =
\"
%s
\\
n%s %s
\"
fillcolor=%s ];"
%
(
proc
[
'p'
],
proc
[
'c'
],
proc
[
'p'
],
proc
[
'L'
],
color
))
else
:
print
(
"
\t
p%s [ label =
\"
%s
\\
n%s %s
\"
fillcolor=%s ];"
%
(
proc
[
'p'
],
proc
[
'c'
],
proc
[
'p'
],
"no user"
,
color
))
if
'R'
in
proc
and
proc
[
'R'
]
in
procs
:
proc_parent
=
procs
[
proc
[
'R'
]]
if
proc_parent
:
if
proc_parent
[
'p'
]
!=
"1"
:
print
(
"
\t
p%s -> p%s [ penwidth=2 weight=100 color=grey60 dir=
\"
none
\"
];"
%
(
proc
[
'R'
],
proc
[
'p'
]))
for
type
,
conn
in
conns
.
items
():
for
id
,
files
in
conn
.
items
():
if
len
(
files
)
==
2
:
if
files
[
0
][
'proc'
]
!=
files
[
1
][
'proc'
]:
label
=
type
+
":
\\
n"
+
id
dir
=
"both"
if
files
[
0
][
'a'
]
==
"w"
:
dir
=
"forward"
elif
files
[
0
][
'a'
]
==
"r"
:
dir
=
"backward"
print
(
"
\t
p%s -> p%s [ color=
\"
%s
\"
label=
\"
%s
\"
dir=
\"
%s
\"
];"
%
(
files
[
0
][
'proc'
][
'p'
],
files
[
1
][
'proc'
][
'p'
],
colors
[
type
]
or
"black"
,
label
,
dir
))
print
(
"}"
)
if
__name__
==
'__main__'
:
sys
.
setrecursionlimit
(
15000
)
# Increase the recursion limit before processing starts
try
:
procs
=
parse_lsof
()
conns
=
find_connections
(
procs
)
print_graph
(
procs
,
conns
)
except
RuntimeError
as
e
:
print
(
"Error: Recursion depth exceeded. Consider increasing the recursion limit."
)
Back
|
FazBrowse Home
|
New Git URL