Class: IDRAC::Web

Inherits:
Object
  • Object
show all
Defined in:
lib/idrac/web.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Web

Returns a new instance of Web.



10
11
12
13
14
15
# File 'lib/idrac/web.rb', line 10

def initialize(client)
  @client = client
  @session_id = nil
  @cookies = nil
  @tried_clearing_sessions = false
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/idrac/web.rb', line 8

def client
  @client
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



8
9
10
# File 'lib/idrac/web.rb', line 8

def cookies
  @cookies
end

#session_idObject (readonly)

Returns the value of attribute session_id.



8
9
10
# File 'lib/idrac/web.rb', line 8

def session_id
  @session_id
end

Instance Method Details

#capture_screenshotObject

Capture a screenshot

Raises:



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
# File 'lib/idrac/web.rb', line 123

def capture_screenshot
  # Login to get the forward URL and cookies
  forward_url = 
  return nil unless forward_url
  
  # Extract the key-value pairs from the forward URL (format: index?ST1=ABC,ST2=DEF)
  tokens = forward_url.split("?").last.split(",").inject({}) do |acc, kv| 
    k, v = kv.split("=")
    acc[k] = v
    acc
  end
  
  # Generate a timestamp for the request
  timestamp_ms = (Time.now.to_f * 1000).to_i
  
  # First request to trigger the screenshot capture
  path = "data?get=consolepreview[manual%20#{timestamp_ms}]"
  res = get(path: path, headers: tokens)
  raise Error, "Failed to trigger screenshot capture." unless res.code.between?(200, 299)
  
  # Wait for the screenshot to be generated
  sleep 2
  
  # Second request to get the actual screenshot image
  path = "capconsole/scapture0.png?#{timestamp_ms}"
  res = get(path: path, headers: tokens)
  raise Error, "Failed to retrieve screenshot image." unless res.code.between?(200, 299)
  
  # Save the screenshot to a file
  filename = "idrac_screenshot_#{timestamp_ms}.png"
  File.open(filename, "wb") { |f| f.write(res.body) }
  
  # Return the filename
  filename
end

#get(path:, headers: {}) ⇒ Object

HTTP GET request for WebUI operations



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/idrac/web.rb', line 160

def get(path:, headers: {})
  headers_to_use = {
    "User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
    "Accept-Encoding" => "deflate, gzip"
  }
  
  if @cookies
    headers_to_use["Cookie"] = @cookies
  elsif client.direct_mode
    # In direct mode, use Basic Auth
    headers_to_use["Authorization"] = "Basic #{Base64.strict_encode64("#{client.username}:#{client.password}")}"
  end
  
  HTTParty.get(
    "#{base_url}/#{path}",
    headers: headers_to_use.merge(headers),
    verify: false
  )
end

#login(retry_count = 0) ⇒ Object

Login to the WebUI



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/idrac/web.rb', line 18

def (retry_count = 0)
  # Limit retries to prevent infinite loops
  if retry_count >= 3
    puts "Maximum retry count reached for WebUI login".red
    return false
  end
  
  # Skip if we already have a session ID
  return true if @session_id
  
  begin
    puts "Logging in to WebUI...".light_cyan
    
    # Create the login URL
     = "#{base_url}/data/login"
    
    # Create the login payload
    payload = {
      'user' => client.username,
      'password' => client.password
    }
    
    # Make the login request
    response = HTTParty.post(
      ,
      body: payload,
      verify: client.verify_ssl,
      headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }
    )
    
    # Check if the login was successful
    if response.code == 200
      # Extract the session ID from the response
      if response.body.include?('ST2')
        @session_id = response.body.match(/ST2=([^;]+)/)[1]
        @cookies = response.headers['set-cookie']
        puts "WebUI login successful".green
        return response.body
      else
        puts "WebUI login failed: No session ID found in response".red
        return false
      end
    elsif response.code == 400 && response.body.include?("maximum number of user sessions")
      puts "Maximum sessions reached during WebUI login".light_red
      
      # Try to clear sessions if auto_delete_sessions is enabled
      if client.auto_delete_sessions && !@tried_clearing_sessions
        puts "Auto-delete sessions is enabled, attempting to clear sessions".light_cyan
        @tried_clearing_sessions = true
        
        if client.session.force_clear_sessions
          puts "Successfully cleared sessions, trying WebUI login again".green
          return (retry_count + 1)
        else
          puts "Failed to clear sessions for WebUI login".red
          return false
        end
      else
        puts "Auto-delete sessions is disabled or already tried clearing".light_yellow
        return false
      end
    else
      puts "WebUI login failed: #{response.code} - #{response.body}".red
      return false
    end
  rescue => e
    puts "Error during WebUI login: #{e.message}".red.bold
    return false
  end
end

#logoutObject

Logout from the WebUI



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
# File 'lib/idrac/web.rb', line 90

def logout
  return unless @session_id
  
  begin
    puts "Logging out from WebUI...".light_cyan
    
    # Create the logout URL
    logout_url = "#{base_url}/data/logout"
    
    # Make the logout request
    response = HTTParty.get(
      logout_url,
      verify: client.verify_ssl,
      headers: { 'Cookie' => @cookies }
    )
    
    # Check if the logout was successful
    if response.code == 200
      puts "WebUI logout successful".green
      @session_id = nil
      @cookies = nil
      return true
    else
      puts "WebUI logout failed: #{response.code} - #{response.body}".red
      return false
    end
  rescue => e
    puts "Error during WebUI logout: #{e.message}".red.bold
    return false
  end
end