-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinux_netstat.rb
More file actions
42 lines (37 loc) · 894 Bytes
/
linux_netstat.rb
File metadata and controls
42 lines (37 loc) · 894 Bytes
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
$prg_cache = {}
def read_proc_entry(proc_entry)
Dir.glob("#{proc_entry}/fd/[0-9]*") do |fd_entry|
begin
fd_link = File.readlink(fd_entry)
if fd_link.start_with?("socket")
cmdline = File.open("#{proc_entry}/cmdline") { |f| f.gets }
inode = fd_link.scan(/[0-9]+/).first
$prg_cache[inode] = cmdline
end
rescue => err
p err
end
end
end
def read_proc_net_tcp
File.open("/proc/net/tcp") do |f|
header = f.gets
p header
f.each_line do |line|
fields = line.split(" ")
if fields[0] == "sl"
next
end
local_addr, local_port = fields[1].split(":")
inode = fields[9]
p "#{$prg_cache[inode]}: Port #{local_port.to_i(16)}"
end
end
end
def load_prg_cache
Dir.glob("/proc/[0-9]*").each do |proc_entry|
read_proc_entry proc_entry
end
end
load_prg_cache
read_proc_net_tcp