Skip to content

Commit 4c7d201

Browse files
authored
Replace DNS resolution with hosts file checking (#58)
1 parent 0349fdd commit 4c7d201

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

lib/vagrant-goodhosts/GoodHosts.rb

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# The core of the plugin
22
require "rbconfig"
33
require "open3"
4-
require "resolv"
54
require "os"
65

76
module VagrantPlugins
@@ -98,22 +97,35 @@ def disable_clean(ip_address)
9897
return true
9998
end
10099

100+
# Check if a specific IP/hostname mapping exists in the hosts file
101+
# Uses the CLI check command instead of DNS resolution
102+
def hosts_entry_exists?(ip_address, hostname)
103+
cli = get_cli
104+
# Use check command - no sudo needed for read-only operation
105+
if cli.include? ".exe"
106+
# Windows: direct command execution
107+
_stdout, _stderr, status = Open3.capture3("\"#{cli}\" check \"#{ip_address}\" \"#{hostname}\"")
108+
else
109+
# Unix/macOS: no sudo needed for check
110+
_stdout, _stderr, status = Open3.capture3("'#{cli}' check '#{ip_address}' '#{hostname}'")
111+
end
112+
return status.success?
113+
rescue StandardError => e
114+
@ui.warn "[vagrant-goodhosts] Error checking host entry: #{e.message}"
115+
return false
116+
end
117+
101118
def check_hostnames_to_add(ip_address, hostnames)
102119
hostnames_to_add = Array.new
103-
hostnames = hostnames.split
104-
# check which hostnames actually need adding
105-
hostnames.each do |hostname|
106-
begin
107-
address = Resolv.getaddress(hostname)
108-
if address != ip_address
109-
hostnames_to_add.append(hostname)
110-
end
111-
rescue StandardError => _e
120+
hostnames_arr = hostnames.split
121+
122+
# Check each hostname using the CLI check command
123+
hostnames_arr.each do |hostname|
124+
unless hosts_entry_exists?(ip_address, hostname)
112125
hostnames_to_add.append(hostname)
113126
end
114-
rescue StandardError => _e
115-
hostnames_to_add.append(hostname)
116127
end
128+
117129
return hostnames_to_add.join(' ')
118130
end
119131

0 commit comments

Comments
 (0)