Class: VagrantWindows::Communication::GuestNetwork

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-windows/communication/guestnetwork.rb

Overview

Manages the remote Windows guest network

Constant Summary collapse

PS_GET_WSMAN_VER =
'((test-wsman).productversion.split(" ") | select -last 1).split("\.")[0]'
WQL_NET_ADAPTERS_V2 =
'SELECT * FROM Win32_NetworkAdapter WHERE MACAddress IS NOT NULL'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(winrmshell) ⇒ GuestNetwork



18
19
20
21
22
# File 'lib/vagrant-windows/communication/guestnetwork.rb', line 18

def initialize(winrmshell)
  @logger = Log4r::Logger.new("vagrant_windows::communication::winrmshell")
  @logger.debug("initializing WinRMShell")
  @winrmshell = winrmshell
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



15
16
17
# File 'lib/vagrant-windows/communication/guestnetwork.rb', line 15

def logger
  @logger
end

#winrmshellObject (readonly)

Returns the value of attribute winrmshell.



16
17
18
# File 'lib/vagrant-windows/communication/guestnetwork.rb', line 16

def winrmshell
  @winrmshell
end

Instance Method Details

#configure_dhcp_interface(nic_index, net_connection_id) ⇒ Object

Configures the specified interface for DHCP



49
50
51
52
53
54
55
# File 'lib/vagrant-windows/communication/guestnetwork.rb', line 49

def configure_dhcp_interface(nic_index, net_connection_id)
  @logger.info("Configuring NIC #{net_connection_id} for DHCP")
  if !is_dhcp_enabled(nic_index)
    netsh = "netsh interface ip set address \"#{net_connection_id}\" dhcp"
    @winrmshell.powershell(netsh)
  end
end

#configure_static_interface(nic_index, net_connection_id, ip, netmask) ⇒ Object

Configures the specified interface using a static address



63
64
65
66
67
68
# File 'lib/vagrant-windows/communication/guestnetwork.rb', line 63

def configure_static_interface(nic_index, net_connection_id, ip, netmask)
  @logger.info("Configuring NIC #{net_connection_id} using static ip #{ip}")
  #netsh interface ip set address "Local Area Connection 2" static 192.168.33.10 255.255.255.0
  netsh = "netsh interface ip set address \"#{net_connection_id}\" static #{ip} #{netmask}"
  @winrmshell.powershell(netsh)
end

#is_dhcp_enabled(nic_index) ⇒ Boolean

Checks to see if the specified NIC is currently configured for DHCP.



35
36
37
38
39
40
41
42
43
# File 'lib/vagrant-windows/communication/guestnetwork.rb', line 35

def is_dhcp_enabled(nic_index)
  has_dhcp_enabled = false
  cmd = "Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter \"Index=#{nic_index} and DHCPEnabled=True\""
  @winrmshell.powershell(cmd) do |type, line|
    has_dhcp_enabled = !line.nil?
  end
  @logger.debug("NIC #{nic_index} has DHCP enabled: #{has_dhcp_enabled}")
  has_dhcp_enabled
end

#network_adaptersArray

Returns an array of all NICs on the guest. Each array entry is a Hash of the NICs properties.



28
29
30
# File 'lib/vagrant-windows/communication/guestnetwork.rb', line 28

def network_adapters()
  wsman_version() == 2? network_adapters_v2_winrm() : network_adapters_v3_winrm()       
end

#set_all_networks_to_workObject

Sets all networks on the guest to ‘Work Network’ mode. This is to allow guest access from the host via a private IP on Win7 github.com/WinRb/vagrant-windows/issues/63



73
74
75
76
77
# File 'lib/vagrant-windows/communication/guestnetwork.rb', line 73

def set_all_networks_to_work()
  @logger.info("Setting all networks to 'Work Network'")
  command = VagrantWindows.load_script("set_work_network.ps1")
  @winrmshell.powershell(command)
end