Last active
March 30, 2023 15:20
-
-
Save Overemployed/ccf6f48c68af2b874b573d10ba445618 to your computer and use it in GitHub Desktop.
PiKVM jiggler 2.0, 1 pixel at a time
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
[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 |
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
#!/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 |
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
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) |
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
[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 |
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:
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
PiKVM Mouse Jiggler 2.0
This gist contains the files and instructions to set up a mouse jiggler on a PiKVM device.
Files
mj.service
: Systemd service file for the mouse jiggler scriptmouse.sh
: Mouse jiggler scripttoggle-api.py
: Flask API for starting, stopping, and toggling the mouse jiggler servicetoggle-api.service
: Systemd service file for the Flask APIInstructions
Prepare PiKVM
Copy all service files to
/etc/systemd/system/
directoryCopy
py
andsh
files to/root
directoryInstall Dependencies and Packages
Enable Services
Start the toggle-api Service
Enable Read-Only Mode
Control the Mouse Jiggler
Start jiggler:
Stop jiggler:
Toggle jiggler:
Get status of jiggler: