Created
February 6, 2023 04:29
-
-
Save safiire/2a436511335c7b54875f395d006e7086 to your computer and use it in GitHub Desktop.
Grab a screenshot of my Rigol 1054 off the LAN and convert it to png
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'csv' | |
require 'socket' | |
require 'tempfile' | |
IP = '192.168.0.24' | |
PORT = 5555 | |
class Screenshot | |
TEMPFILE = %w{scope bmp} | |
Scope = Struct.new(:name, :model, :serial, :firmware) | |
def initialize(host, port) | |
@sock = TCPSocket.new(host, port) | |
end | |
def identify | |
sock.puts('*IDN?') | |
response = CSV.parse(sock.gets.chomp).flatten | |
Scope.new(*response) | |
end | |
def grab(filename = generate_filename) | |
filename.tap do |filename| | |
temp_file = Tempfile.new(TEMPFILE) | |
File.open(temp_file.path, 'wb') do |fp| | |
fp.write(read_screenshot) | |
end | |
system("convert #{temp_file.path} #{filename}") | |
exit_fail unless $?.success? | |
puts "[+] Wrote #{filename}" | |
end | |
end | |
private | |
attr_reader :sock | |
def read_screenshot | |
sock.puts(':DISP:DATA?') | |
sock.read(1) | |
sock.read(read_integer(read_integer(1))) | |
end | |
def read_integer(n) | |
sock.read(n).to_i | |
end | |
def generate_filename | |
model = identify.model | |
time = Time.now.strftime('%F.%H:%M:%S') | |
[model, time, 'png'].join('.') | |
end | |
def exit_fail | |
STDERR.puts('Failed to convert to png') | |
exit 1 | |
end | |
end | |
Screenshot.new(IP, PORT).grab |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment