Skip to content

Instantly share code, notes, and snippets.

@Overemployed
Last active March 30, 2023 15:20
Show Gist options
  • Save Overemployed/ccf6f48c68af2b874b573d10ba445618 to your computer and use it in GitHub Desktop.
Save Overemployed/ccf6f48c68af2b874b573d10ba445618 to your computer and use it in GitHub Desktop.
PiKVM jiggler 2.0, 1 pixel at a time
[Unit]
Description=Mouse Script
After=network.target
[Service]
User=root
ExecStart=/bin/bash mouse.sh
WorkingDirectory=/root
Restart=on-failure
[Install]
WantedBy=multi-user.target
#!/bin/bash
HOSTNAME="0.0.0.0"
USERNAME="admin"
PASSWORD="admin"
IGNORE_ERRORS=false
function set_relative(){
# Set URL and parameters
url="https://${HOSTNAME}/api/hid/set_params"
params="mouse_output=$(if $1; then echo 'usb_rel'; else echo 'usb'; fi)"
# Set headers
headers=(
"X-KVMD-User: $USERNAME"
"X-KVMD-Passwd: $PASSWORD"
)
# Send POST request with cURL
curl -k -X POST \
-H "${headers[0]}" \
-H "${headers[1]}" \
-d "$params" \
"$url" >/dev/null 2>&1
}
function send_key_event() {
local key="$1"
local state="$2"
local message='{"event_type": "key", "event": { "key": "'"$key"'", "state": '"$state"'}}'
websocat -k wss://$HOSTNAME/api/ws?stream=0 -H "X-KVMD-User:$USERNAME" -H "X-KVMD-Passwd:$PASSWORD" -d "$message" >/dev/null 2>&1
if [ $? -ne 0 ] && ! $IGNORE_ERRORS; then
echo "Error: WebSocket communication failed"
exit 1
fi
}
function random_movement() {
set_relative true
directions=("-1,0" "1,0" "0,-1" "0,1")
random_direction=${directions[$RANDOM % ${#directions[@]}]}
x=${random_direction%,*}
y=${random_direction#*,}
message='{"event_type": "mouse_relative", "event": {"delta": [{"x": '$x', "y": '$y'}], "squash": true}}'
echo "$message" | websocat -k wss://$HOSTNAME/api/ws?stream=0 -H "X-KVMD-User:$USERNAME" -H "X-KVMD-Passwd:$PASSWORD" >/dev/null 2>&1
if [ $? -ne 0 ] && ! $IGNORE_ERRORS; then
echo "Error: WebSocket communication failed"
set_relative false
exit 1
fi
sleep 0.1
set_relative false
}
while true; do
echo "$(date --iso-8601=seconds): Moving mouse cursor"
random_movement
random_sleep=$((RANDOM % 4001))
pad=$((1000 + random_sleep))
sleep_for=$(echo "scale=3; $pad/1000" | bc)
sleep "${sleep_for}s"
done
from flask import Flask, jsonify
import os
import subprocess
app = Flask(__name__)
service_name = "mj.service"
def get_service_status():
result = subprocess.run(["systemctl", "is-active", service_name], capture_output=True, text=True)
return result.stdout.strip()
@app.route('/start', methods=['GET'])
def start_service():
subprocess.run(["sudo", "systemctl", "start", service_name])
return jsonify({'status': get_service_status()})
@app.route('/stop', methods=['GET'])
def stop_service():
subprocess.run(["sudo", "systemctl", "stop", service_name])
return jsonify({'status': get_service_status()})
@app.route('/toggle', methods=['GET'])
def toggle_service():
current_status = get_service_status()
if current_status == "active":
subprocess.run(["sudo", "systemctl", "stop", service_name])
else:
subprocess.run(["sudo", "systemctl", "start", service_name])
return jsonify({'status': get_service_status()})
@app.route('/status', methods=['GET'])
def get_status():
return jsonify({'status': get_service_status()})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
[Unit]
Description=Flask API for Mouse Script
After=network.target
[Service]
User=root
WorkingDirectory=/root
ExecStart=/usr/bin/python toggle-api.py
Restart=on-failure
[Install]
WantedBy=multi-user.target
@Overemployed
Copy link
Author

Overemployed commented Mar 24, 2023

PiKVM Mouse Jiggler 2.0

This gist contains the files and instructions to set up a mouse jiggler on a PiKVM device.

Files

  1. mj.service: Systemd service file for the mouse jiggler script
  2. mouse.sh: Mouse jiggler script
  3. toggle-api.py: Flask API for starting, stopping, and toggling the mouse jiggler service
  4. toggle-api.service: Systemd service file for the Flask API

Instructions

Prepare PiKVM

  1. Enable read-write mode:
rw
  1. Copy all service files to /etc/systemd/system/ directory

  2. Copy py and sh files to /root directory

Install Dependencies and Packages

pacman -Sy
pacman -S openssl python-pip
pip install flask

Enable Services

systemctl enable mj.service
systemctl enable toggle-api.service

Start the toggle-api Service

systemctl start toggle-api.service

Enable Read-Only Mode

ro

Control the Mouse Jiggler

Start jiggler:

curl pikvm.local:5000/start

Stop jiggler:

curl pikvm.local:5000/stop

Toggle jiggler:

curl pikvm.local:5000/toggle

Get status of jiggler:

curl pikvm.local:5000/status

@Overemployed
Copy link
Author

Overemployed commented Mar 24, 2023

I recommend bitfocus companion, for a web interface with buttons to control your mouse jiggler and other things.

You can download the latest release of Bitfocus Companion from the official GitHub repository. Choose the appropriate version for your operating system (Windows, macOS, or Linux).

Here's an example of how I setup one of my Mouse Buttons:

localhost_8888_buttons (1)

The button turns green when the mouse is on, and red when off. We use a variable to keep track of the status, that's set in the Variables tab -> Custom Variables section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment