Class: Phut::Route

Inherits:
Object
  • Object
show all
Extended by:
ShellRunner
Includes:
ShellRunner
Defined in:
lib/phut/route.rb

Overview

routing table entry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ShellRunner

sh, sudo

Constructor Details

#initialize(net:, gateway:) ⇒ Route

Returns a new instance of Route.



22
23
24
25
# File 'lib/phut/route.rb', line 22

def initialize(net:, gateway:)
  @net = net
  @gateway = gateway
end

Instance Attribute Details

#gatewayObject (readonly)

Returns the value of attribute gateway.



20
21
22
# File 'lib/phut/route.rb', line 20

def gateway
  @gateway
end

#netObject (readonly)

Returns the value of attribute net.



19
20
21
# File 'lib/phut/route.rb', line 19

def net
  @net
end

Class Method Details

.read(netns) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/phut/route.rb', line 10

def self.read(netns)
  sudo("ip netns exec #{netns} route -n").split("\n").each do |each|
    match = /^(\S+)\s+(\S+)\s+\S+\s+UG\s+/.match(each)
    next unless match
    return new(net: match[1], gateway: match[2])
  end
  nil
end

Instance Method Details

#add(netns) ⇒ Object



29
30
31
32
# File 'lib/phut/route.rb', line 29

def add(netns)
  return unless @net && @gateway
  sudo "ip netns exec #{netns} route add -net #{@net} gw #{@gateway}"
end