Skip to content

Instantly share code, notes, and snippets.

@mdevaev
Created June 20, 2020 13:23
Show Gist options
  • Save mdevaev/4266ec6c8911482d45b40a228ad94083 to your computer and use it in GitHub Desktop.
Save mdevaev/4266ec6c8911482d45b40a228ad94083 to your computer and use it in GitHub Desktop.
import sys
import asyncio
import json
import aiohttp.web
async def run_script(url: str, user: str, passwd: str, script_path: str) -> None:
with open(script_path) as script_file:
record = json.loads(script_file.read())
params = {
"verify_ssl": False,
"headers": {"X-KVMD-User": user, "X-KVMD-Passwd": passwd},
}
async with aiohttp.ClientSession() as session:
async with session.ws_connect(f"{url}/api/ws?stream=0", **params) as ws:
for event in record:
print(event)
if event["event_type"] == "delay":
await asyncio.sleep(event["event"]["millis"] / 1000)
elif event["event_type"] == "print":
async with session.post(f"{url}/api/hid/print?limit=0", data=event["event"]["text"], **params) as response:
response.raise_for_status()
elif event["event_type"] in ["key", "mouse_button", "mouse_move", "mouse_wheel"]:
await ws.send_json(event)
if __name__ == "__main__":
assert len(sys.argv) == 5, f"Usage: {sys.argv[0]} https://pikvm user passwd path/to/script.json"
asyncio.run(run_script(*sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment