79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/idrac/network.rb', line 79
def set_bmc_network(ipv4: nil, mask: nil, gateway: nil,
dns_primary: nil, dns_secondary: nil, hostname: nil,
dhcp: false)
puts "🔧 iDRAC set_bmc_network called with: ipv4=#{ipv4}, mask=#{mask}, gateway=#{gateway}, dhcp=#{dhcp}".cyan
begin
puts "🔍 Getting iDRAC version information...".yellow
version_info = get_idrac_version_info
puts "✅ Detected iDRAC Generation #{version_info[:generation]}, Firmware: #{version_info[:firmware_version]}".green
puts " Model: #{version_info[:model]}, Firmware Numeric: #{version_info[:firmware_numeric]}".cyan
rescue => e
puts "❌ Error getting iDRAC version info: #{e.class} - #{e.message}".red
raise e
end
puts "🔍 Getting ethernet interfaces collection...".yellow
collection_response = authenticated_request(:get, "/redfish/v1/Managers/iDRAC.Embedded.1/EthernetInterfaces")
if collection_response.status == 200
puts "✅ Got ethernet interfaces collection successfully".green
collection = JSON.parse(collection_response.body)
puts "🔍 Collection members: #{collection["Members"]&.size || 0} interfaces found".cyan
if collection["Members"] && collection["Members"].any?
interface_path = collection["Members"][0]["@odata.id"]
puts "✅ Using interface path: #{interface_path}".green
if dhcp
puts "Setting iDRAC to DHCP mode...".yellow
body = {
"DHCPv4" => {
"DHCPEnabled" => true
}
}
else
puts "Configuring iDRAC network settings...".yellow
body = {}
if version_info[:generation] >= 9 && version_info[:firmware_numeric] > 0
puts "Using iDRAC9+ SCP approach for network configuration".yellow
puts " Delegating to set_idrac_ip method for reliable configuration".cyan
return set_idrac_ip(new_ip: ipv4, new_gw: gateway, new_nm: mask)
else
puts "Using legacy iDRAC8 API approach (IPv4Addresses)".yellow
body["DHCPv4"] = {
"DHCPEnabled" => false
}
if ipv4 && mask
body["IPv4Addresses"] = [{
"Address" => ipv4,
"SubnetMask" => mask,
"Gateway" => gateway,
"AddressOrigin" => "Static"
}]
puts " IP: #{ipv4}/#{mask}".cyan
puts " Gateway: #{gateway}".cyan if gateway
end
end
if dns_primary || dns_secondary
dns_servers = []
dns_servers << dns_primary if dns_primary
dns_servers << dns_secondary if dns_secondary
body["StaticNameServers"] = dns_servers
puts " DNS: #{dns_servers.join(', ')}".cyan
end
if hostname
body["HostName"] = hostname
puts " Hostname: #{hostname}".cyan
end
end
puts "🔍 Sending PATCH request to #{interface_path}".yellow
puts "📦 Request body: #{JSON.pretty_generate(body)}".cyan
begin
response = authenticated_request(
:patch,
interface_path,
body: body.to_json,
headers: { 'Content-Type' => 'application/json' }
)
puts "✅ Got response with status: #{response.status}".green
rescue => e
puts "❌ Error sending PATCH request: #{e.class} - #{e.message}".red
raise e
end
if response.status.between?(200, 299)
puts "iDRAC network configured successfully using #{version_info[:generation] >= 9 ? 'newer' : 'legacy'} API.".green
if ipv4 && !dhcp
puts "Initiating iDRAC restart to apply network configuration...".yellow
restart_response = authenticated_request(
:post,
"/redfish/v1/Managers/iDRAC.Embedded.1/Actions/Manager.Reset",
body: { "ResetType" => "GracefulRestart" }.to_json,
headers: { 'Content-Type' => 'application/json' }
)
if restart_response.status == 204
puts "✓ iDRAC restart initiated successfully.".green
puts "Network configuration will be applied after restart (2-3 minutes).".cyan
puts "New IP: #{ipv4}".cyan
else
puts "⚠ iDRAC restart failed (#{restart_response.status}). You may need to restart manually.".yellow
puts "Configuration is saved but may not be active until restart.".yellow
end
else
puts "WARNING: iDRAC may restart network services. Connection may be lost.".yellow
end
puts "✅ Returning true from set_bmc_network".green
true
else
puts "❌ Network configuration failed with status: #{response.status}".red
puts "🔍 Response body: #{response.body}".red
error_msg = "Failed to configure iDRAC network (Generation #{version_info[:generation]}, FW #{version_info[:firmware_version]}): #{response.status} - #{response.body}"
raise Error, error_msg
end
else
puts "❌ No ethernet interfaces found in collection".red
raise Error, "No ethernet interfaces found"
end
else
puts "❌ Failed to get ethernet interfaces collection, status: #{collection_response.status}".red
puts "🔍 Response body: #{collection_response.body}".red
raise Error, "Failed to get ethernet interfaces"
end
end
|