-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlsof.rb
executable file
·112 lines (93 loc) · 3.04 KB
/
lsof.rb
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
#!/usr/bin/env ruby
DOT_FILE = File.expand_path("~/Desktop/lsof.dot")
IMAGE_FILE = DOT_FILE + ".png"
class Document
def initialize
@applications = []
processLSOF
end
def processLSOF
application = nil
connection = nil
# Get the LSOF data
`lsof -Pi -F`.scan(/^(?:c|n|P|TST).*/).each{ |line|
field, value = line.scan(/(T..|.)(.*)/).flatten
case field
when "c"
application = Application.new(:name=>value)
@applications.push(application)
when "P"
connection = Connection.new(:protocol=>value)
application.connections.push(connection)
when "n"
connection.processLSOF(value)
when "TST"
connection.status = value
end
}
end
def print stream=STDOUT, depth=0
stream.puts "digraph G {"
stream.puts " overlap=false;"
stream.puts " rankdir=LR;"
stream.puts " "
stream.puts %{"My Computer" [shape=box,style=filled,color="#dddddd"];}
@applications.each { |application|
stream.puts %{"My Computer" -> "#{application.name}";}
application.print(stream, depth+1)
}
stream.puts "}"
end
end
class Application
def initialize options={}
@name = options[:name] || ""
@connections = []
end
def name ; return @name ; end
def connections ; return @connections ; end
def print stream=STDOUT, depth=0
indent = "\t" * depth
stream.puts %{#{indent}"#{@name}" [shape="ellipse",style=filled,color="#ddddff"];}
printedConnections = {}
printedPorts = {}
connections.each { |connection|
if(!printedConnections[connection.destination]) then
printedConnections[connection.destination] = true
stream.puts %{#{indent}\t"#{@name}.#{connection.destination}" [shape=rectangle,label="#{connection.destination}"];}
stream.puts %{#{indent}\t"#{@name}" -> "#{@name}.#{connection.destination}";}
end
if(!printedPorts[connection.destinationPort]) then
printedPorts[connection.destinationPort] = true
stream.puts %{#{indent}\t"#{@name}.#{connection.destination}.#{connection.destinationPort}" [shape=plaintext,label="#{connection.destinationPort}"];}
stream.puts %{#{indent}\t"#{@name}.#{connection.destination}" -> "#{@name}.#{connection.destination}.#{connection.destinationPort}" [label="#{connection.protocol}"];}
end
}
end
end
class Connection
def initialize options={}
@protocol = options[:protocol] || ""
@source = ""
@destination = ""
@mode = ""
@status = ""
end
def destination ; return @destination ; end
def destinationPort ; return @destinationPort ; end
def protocol ; return @protocol ; end
def protocol= value ; @protocol = value ; end
def status= value ; @status = value ; end
def processLSOF lsofData
parts = lsofData.scan(/^(?:(.*?):(.*?)\s*->\s*(.*?):(.*?)|(.*?):(.*?))$/)[0]
@source = parts[0]
@sourcePort = parts[1]
@destination = parts[2] || parts[4]
@destinationPort = parts[3] || parts[5]
end
end
stream = File.open(DOT_FILE, "w")
Document.new.print stream
stream.close
`dot -Tpng -o "#{IMAGE_FILE}" "#{DOT_FILE}"`
`open "#{IMAGE_FILE}"`