Module: VirtualboxWSL2::WSL2Provider

Included in:
VagrantPlugins::ProviderVirtualBox::Provider
Defined in:
lib/virtualbox_WSL2.rb

Instance Method Summary collapse

Instance Method Details

#ssh_infoObject

Returns the SSH info for accessing the VirtualBox VM.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/virtualbox_WSL2.rb', line 59

def ssh_info
  # If the VM is not running that we can't possibly SSH into it
  return nil if state.id != :running

  # Return what we know. The host is always "127.0.0.1" because
  # VirtualBox VMs are always local. The port we try to discover
  # by reading the forwarded ports.
  host_ip = "127.0.0.1"

  # If we run on WSL2, we need to ssh through Windows IP, which is set as the default route
  if Vagrant::Util::Platform.wsl?
    result = Vagrant::Util::Subprocess.execute("/bin/sh", "-c", "ip route | grep default | grep -Po '\\d+.\\d+.\\d+.\\d+'")
    if result.exit_code == 0
      host_ip = result.stdout.strip
    end
  end

  return {
    host: host_ip,
    port: @driver.ssh_port(@machine.config.ssh.guest_port)
  }
end