Skip to content

Instantly share code, notes, and snippets.

@jresendiz27
Created January 30, 2023 23:00
Show Gist options
  • Save jresendiz27/a2a5e0fef0962d7496cafd793a225c7c to your computer and use it in GitHub Desktop.
Save jresendiz27/a2a5e0fef0962d7496cafd793a225c7c to your computer and use it in GitHub Desktop.
Do not lock my screen!

Disclaimer

Follow your company security guidelines! :D

Purpose

Sometimes you need to have a process running on the background but your laptop policies (automatic lock) are the problem. So you need to have a backup plan for this cases, here's where this solution gets interesting.

What does the script do?

Moves your mouse certain pixels randomly during an especific period of time and certain interval so the computer detects interaction and the lock screen is never reached!

Requirements

How to use it?

Save the script described below as do_not_lock_the_screen.rb on your computer and grant execution privileges chmod +x do_not_lock_the_screen.rb

This can be also executed directly using the ruby interpreter!

Script do_not_lock_the_screen.rb

#!/usr/bin/env ruby

require 'rumouse'
require 'time'
require 'logger'
require 'optparse'

ARGV << '-h' if ARGV.empty?


Options = Struct.new(:name)

program_options = {}

logger = Logger.new(STDOUT)
logger.level = Logger::WARN



option_parser = OptionParser.new do |opts|
  opts.banner = "Usage: do_not_block_me.rb [options]"

  opts.on('-i', '--script-interval [VALUE]', Integer, 'Wait interval in seconds') do |script_interval|
    program_options[:script_interval] = script_interval
  end 

  opts.on('-d', '--script-duration [VALUE]', Integer, 'script duration in seconds') do |script_duration|
    program_options[:script_duration] = script_duration
  end 

  opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
    program_options[:verbose] = v
    if v then
      logger.level = Logger::DEBUG
    end
  end
  opts.on("-h", "--help", "Prints this help") do
    puts opts
    exit
  end
end

option_parser.parse!

mouse = RuMouse.new
now = Time.now
random_boolean = [true, false]


loop do
  if program_options[:script_duration] > 0 then
    logger.warn "sleeping #{program_options[:script_interval]} seconds ... "
    sleep(program_options[:script_interval])
    break if Time.now > (now + program_options[:script_duration])
    logger.debug "Remaining time: #{((now + program_options[:script_duration]) - Time.now).to_i}"
  end

  rand_position = rand(10)

  logger.debug "rand_position: #{rand_position}"

  if [true, false].sample 
    rand_position = rand_position * -1
  end


  mouse_pos = mouse.position
  new_pos = {
    :x => (mouse_pos[:x] + rand_position),
    :y => (mouse_pos[:y] + rand_position)
  }

  puts "Moving mouse from:#{mouse_pos}, to:#{new_pos}"

  mouse.move new_pos[:x], new_pos[:y]
end

Execute

Execute the script

./do_not_lock_the_screen.rb --script-interval 10 --script-duration 300
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment