Class: Net::IP::Route::Parser
- Inherits:
-
Object
- Object
- Net::IP::Route::Parser
- Defined in:
- lib/net/ip/route/parser.rb
Overview
Parses routing table entries
Class Method Summary collapse
-
.parse(data) ⇒ Array
Parse the output of ip route into an array of hashes.
-
.parse_line(line) ⇒ Hash
Parse a routing table entry into a hash.
Class Method Details
.parse(data) ⇒ Array
Parse the output of ip route into an array of hashes
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/net/ip/route/parser.rb', line 38 def self.parse(data) list = [] in_default = false data.each_line do |line| if in_default == true if line.start_with? "\t" list << parse_line(line.strip.gsub("nexthop", "default")) else in_default = false end elsif line.strip == "default" in_default = true end unless in_default list << parse_line(line.strip) end end list end |
.parse_line(line) ⇒ Hash
Parse a routing table entry into a hash
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/net/ip/route/parser.rb', line 12 def self.parse_line(line) params = {} if line =~ /^(unicast|unreachable|blackhole|prohibit|local|broadcast|throw|nat|via|anycast|multicast)\s+/ params[:type] = $1 line = line[$1.length..-1] end params[:prefix] = line.split.first params[:dev] = $1 if line =~ /\s+dev\s+([^\s]+)/ params[:scope] = $1 if line =~ /\s+scope\s+([^\s]+)/ params[:metric] = $1 if line =~ /\s+metric\s+([^\s]+)/ params[:proto] = $1 if line =~ /\s+proto\s+([^\s]+)/ params[:src] = $1 if line =~ /\s+src\s+([^\s]+)/ params[:via] = $1 if line =~ /\s+via\s+([^\s]+)/ params[:weight] = $1 if line =~ /\s+weight\s+([^\s]+)/ params[:table] = $1 if line =~ /\s+table\s+([^\s]+)/ params[:error] = $1 if line =~ /\s+error\s+([^\s]+)/ params end |