Class: VirtualBox::Net::Dhcp
- Inherits:
-
Object
- Object
- VirtualBox::Net::Dhcp
- Defined in:
- lib/virtual_box/net/dhcp.rb
Overview
Specification for a virtual DHCP server.
Instance Attribute Summary collapse
-
#end_ip ⇒ String
The last IP address in this DHCP server’s address pool.
-
#ip ⇒ String
This DHCP server’s IP address on the virtual network that it serves.
-
#netmask ⇒ String
The network mask reported by this DHCP server.
-
#start_ip ⇒ String
The first IP address in this DHCP server’s address pool.
Class Method Summary collapse
-
.all ⇒ Hash<String, VirtualBox::Dhcp>
The DHCP servers added to with VirtualBox.
-
.ip_btos(ip_number) ⇒ String
Converts an IP number into a string.
-
.ip_stob(ip_string) ⇒ Integer
Converts an IP string into a number.
Instance Method Summary collapse
-
#add(net_or_name) ⇒ VirtualBox::Net::Dhcp
Adds this DHCP server to VirtualBox.
-
#from_dhcp_info(dhcp_info) ⇒ String
Parses information about a DHCP server returned by VirtualBox.
-
#initialize(options = {}) ⇒ Dhcp
constructor
Creates a DHCP server specification based on the given attributes.
-
#remove(net_or_name) ⇒ VirtualBox::Net::Dhcp
Removes this DHCP server from VirtualBox.
-
#to_hash ⇒ Hash<Symbol, Object>
Hash capturing this specification.
Constructor Details
#initialize(options = {}) ⇒ Dhcp
Creates a DHCP server specification based on the given attributes.
The DHCP server is not automatically added to VirtualBox.
51 52 53 |
# File 'lib/virtual_box/net/dhcp.rb', line 51 def initialize( = {}) .each { |k, v| self.send :"#{k}=", v } end |
Instance Attribute Details
#end_ip ⇒ String
The last IP address in this DHCP server’s address pool.
21 22 23 |
# File 'lib/virtual_box/net/dhcp.rb', line 21 def end_ip @end_ip end |
#ip ⇒ String
This DHCP server’s IP address on the virtual network that it serves.
9 10 11 |
# File 'lib/virtual_box/net/dhcp.rb', line 9 def ip @ip end |
#netmask ⇒ String
The network mask reported by this DHCP server.
13 14 15 |
# File 'lib/virtual_box/net/dhcp.rb', line 13 def netmask @netmask end |
#start_ip ⇒ String
The first IP address in this DHCP server’s address pool.
17 18 19 |
# File 'lib/virtual_box/net/dhcp.rb', line 17 def start_ip @start_ip end |
Class Method Details
.all ⇒ Hash<String, VirtualBox::Dhcp>
The DHCP servers added to with VirtualBox.
104 105 106 107 108 109 110 111 112 |
# File 'lib/virtual_box/net/dhcp.rb', line 104 def self.all output = VirtualBox.run_command! ['VBoxManage', '--nologo', 'list', '--long', 'dhcpservers'] Hash[output.split("\n\n").map { |dhcp_info| dhcp = new if_name = dhcp.from_dhcp_info(dhcp_info) [if_name, dhcp] }] end |
.ip_btos(ip_number) ⇒ String
Converts an IP number into a string.
137 138 139 |
# File 'lib/virtual_box/net/dhcp.rb', line 137 def self.ip_btos(ip_number) [ip_number].pack('N').unpack('C*').join('.') end |
.ip_stob(ip_string) ⇒ Integer
Converts an IP string into a number.
145 146 147 |
# File 'lib/virtual_box/net/dhcp.rb', line 145 def self.ip_stob(ip_string) ip_string.split('.').map(&:to_i).pack('C*').unpack('N').first end |
Instance Method Details
#add(net_or_name) ⇒ VirtualBox::Net::Dhcp
Adds this DHCP server to VirtualBox.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/virtual_box/net/dhcp.rb', line 69 def add(net_or_name) command = ['VBoxManage', 'dhcpserver', 'add', '--ip', ip, '--netmask', netmask, '--lowerip', start_ip, '--upperip', end_ip, '--enable'] if net_or_name.kind_of? VirtualBox::Net command.push '--ifname', net_or_name.name else command.push '--netname', net_or_name end VirtualBox.run_command! command self end |
#from_dhcp_info(dhcp_info) ⇒ String
Parses information about a DHCP server returned by VirtualBox.
The parsed information is used to replace this network’s specification.
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/virtual_box/net/dhcp.rb', line 120 def from_dhcp_info(dhcp_info) info = Hash[dhcp_info.split("\n").map { |line| line.split(':', 2).map(&:strip) }] self.ip = info['IP'] self.netmask = info['NetworkMask'] self.start_ip = info['lowerIPAddress'] self.end_ip = info['upperIPAddress'] info['NetworkName'] end |
#remove(net_or_name) ⇒ VirtualBox::Net::Dhcp
Removes this DHCP server from VirtualBox.
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/virtual_box/net/dhcp.rb', line 88 def remove(net_or_name) command = ['VBoxManage', 'dhcpserver', 'remove'] if net_or_name.kind_of? VirtualBox::Net command.push '--ifname', net_or_name.name else command.push '--netname', net_or_name end VirtualBox.run_command command self end |
#to_hash ⇒ Hash<Symbol, Object>
Hash capturing this specification. Can be passed to Dhcp#new.
59 60 61 62 |
# File 'lib/virtual_box/net/dhcp.rb', line 59 def to_hash { :net_name => 'net_name', :ip => ip, :netmask => netmask, :start_ip => start_ip, :end_ip => end_ip } end |