Class: Net::IP::Route

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/net/ip/route.rb

Overview

Class for working with routing table entries.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Route

Note:

This does NOT add the entry to the routing table. See add_route for creating new routes in the routing table.

Create a new route object

Examples:

Create a default route

Net::IP::Route.new(:prefix => 'default', :via => '192.168.0.1')

Create a normal route

Net::IP::Route.new(:prefix => '10.0.0.0/8', :dev => 'eth0')

Parameters:

  • params (Hash) (defaults to: {})


17
18
19
20
21
# File 'lib/net/ip/route.rb', line 17

def initialize(params = {})
  params.each do |k,v|
    send("#{k}=", v)
  end
end

Instance Attribute Details

#devObject

Returns the value of attribute dev.



5
6
7
# File 'lib/net/ip/route.rb', line 5

def dev
  @dev
end

#errorObject

Returns the value of attribute error.



5
6
7
# File 'lib/net/ip/route.rb', line 5

def error
  @error
end

#metricObject

Returns the value of attribute metric.



5
6
7
# File 'lib/net/ip/route.rb', line 5

def metric
  @metric
end

#prefixObject

Returns the value of attribute prefix.



5
6
7
# File 'lib/net/ip/route.rb', line 5

def prefix
  @prefix
end

#protoObject

Returns the value of attribute proto.



5
6
7
# File 'lib/net/ip/route.rb', line 5

def proto
  @proto
end

#scopeObject

Returns the value of attribute scope.



5
6
7
# File 'lib/net/ip/route.rb', line 5

def scope
  @scope
end

#srcObject

Returns the value of attribute src.



5
6
7
# File 'lib/net/ip/route.rb', line 5

def src
  @src
end

#tableObject

Returns the value of attribute table.



5
6
7
# File 'lib/net/ip/route.rb', line 5

def table
  @table
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/net/ip/route.rb', line 5

def type
  @type
end

#viaObject

Returns the value of attribute via.



5
6
7
# File 'lib/net/ip/route.rb', line 5

def via
  @via
end

#weightObject

Returns the value of attribute weight.



5
6
7
# File 'lib/net/ip/route.rb', line 5

def weight
  @weight
end

Class Method Details

.add_route(route) ⇒ void

This method returns an undefined value.

Add a route to the routing table

Examples:

Create a route to the 10.0.0.0/8 network

route = Net::IP::Route.new(:prefix => '10.0.0.0/8', :dev => 'eth0')
Net::IP::Route.add_route(route)

Parameters:

  • route (Route)

    Route to add to the table.



60
61
62
63
# File 'lib/net/ip/route.rb', line 60

def self.add_route(route)
  result = `ip route add #{route.build_param_string}`
  raise result unless $?.success?
end

.allArray<Route>

Get a list of all routes

Returns:



32
33
34
# File 'lib/net/ip/route.rb', line 32

def self.all
  RouteParser.parse(`ip route`).collect {|r| new(r)}
end

.each {|Route| ... } ⇒ void

This method returns an undefined value.

Enumerate all routes

Yields:



26
27
28
# File 'lib/net/ip/route.rb', line 26

def self.each(&block)
  RouteParser.parse(`ip route`).each {|r| yield(new(r))}
end

.find_gatewaysArray<Route>

Get a list of all default gateway routes

Returns:



38
39
40
# File 'lib/net/ip/route.rb', line 38

def self.find_gateways
  find_all {|r| r.prefix == "default"}
end

.flush(selector) ⇒ void

This method returns an undefined value.

Flush the routing table based on a selector

Examples:

Flush the routing table cache

Net::IP::Route.flush(:cache)

Parameters:

  • selector (String)

    The selector string.



70
71
72
73
# File 'lib/net/ip/route.rb', line 70

def self.flush(selector)
  result = `ip route flush #{selector}`
  raise result unless $?.success?
end

.update_gateways(gateways) ⇒ void

This method returns an undefined value.

Update the list of default gateways

Examples:

Change the default gateway to 192.168.0.1

gateway = Net::IP::Route.new(:prefix => 'default', :via => '192.168.0.1')
Net::IP::Route.update_gateways([gateway])

Parameters:

  • gateways (Array<Route>)

    List of default gateways to use.



48
49
50
51
52
# File 'lib/net/ip/route.rb', line 48

def self.update_gateways(gateways)
  params = gateways.collect {|gateway| "nexthop " + gateway.build_param_string}
  result = `ip route replace default #{params.join(" ")}`
  raise result unless $?.success?
end