|
1 | 1 | # The core of the plugin |
2 | 2 | require "rbconfig" |
3 | 3 | require "open3" |
4 | | -require "resolv" |
5 | 4 | require "os" |
6 | 5 |
|
7 | 6 | module VagrantPlugins |
@@ -98,22 +97,35 @@ def disable_clean(ip_address) |
98 | 97 | return true |
99 | 98 | end |
100 | 99 |
|
| 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 | + |
101 | 118 | def check_hostnames_to_add(ip_address, hostnames) |
102 | 119 | 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) |
112 | 125 | hostnames_to_add.append(hostname) |
113 | 126 | end |
114 | | - rescue StandardError => _e |
115 | | - hostnames_to_add.append(hostname) |
116 | 127 | end |
| 128 | + |
117 | 129 | return hostnames_to_add.join(' ') |
118 | 130 | end |
119 | 131 |
|
|
0 commit comments