lib/idrac/screenshot.rb



require 'httparty'
require 'nokogiri'

module IDRAC
  # Reverse engineered screenshot functionality for iDRAC
  # This uses introspection on how the web UI creates screenshots rather than the Redfish API
  class Screenshot
    attr_reader :client

    def initialize(client)
      @client = client
    end

    def capture
      # Login to get the forward URL and cookies
      forward_url = client.login
      
      # 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 = client.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 = client.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
  end
end