Class: VagrantPlugins::GuestMSYS2::GuestNetwork

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-guest-msys2/guest_network.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 Method Summary collapse

Constructor Details

#initialize(communicator) ⇒ GuestNetwork

Returns a new instance of GuestNetwork.



10
11
12
13
# File 'lib/vagrant-guest-msys2/guest_network.rb', line 10

def initialize(communicator)
  @logger       = Log4r::Logger.new("vagrant::windows::guestnetwork")
  @communicator = communicator
end

Instance Method Details

#configure_dhcp_interface(nic_index, net_connection_id) ⇒ Object

Configures the specified interface for DHCP

Parameters:

  • The (Integer)

    interface index.

  • The (String)

    unique name of the NIC, such as ‘Local Area Connection’.



42
43
44
45
46
47
48
# File 'lib/vagrant-guest-msys2/guest_network.rb', line 42

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"
    @communicator.execute(netsh)
  end
end

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

Configures the specified interface using a static address

Parameters:

  • The (Integer)

    interface index.

  • The (String)

    unique name of the NIC, such as ‘Local Area Connection’.

  • The (String)

    static IP address to assign to the specified NIC.

  • The (String)

    network mask to use with the static IP.



56
57
58
59
60
61
# File 'lib/vagrant-guest-msys2/guest_network.rb', line 56

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}"
  @communicator.execute(netsh)
end

#is_dhcp_enabled(nic_index) ⇒ Boolean

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

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
# File 'lib/vagrant-guest-msys2/guest_network.rb', line 28

def is_dhcp_enabled(nic_index)
  cmd = <<-EOH
    if (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "Index=#{nic_index} and DHCPEnabled=True") {
      exit 0
    }
    exit 1
  EOH
  @communicator.test(cmd)
end

#network_adaptersArray

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

Returns:

  • (Array)


19
20
21
22
23
# File 'lib/vagrant-guest-msys2/guest_network.rb', line 19

def network_adapters
  @logger.debug("querying 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



66
67
68
69
70
# File 'lib/vagrant-guest-msys2/guest_network.rb', line 66

def set_all_networks_to_work
  @logger.info("Setting all networks to 'Work Network'")
  command = File.read(File.expand_path("../scripts/set_work_network.ps1", __FILE__))
  @communicator.execute(command)
end