Introduction
Use OpenSSH and Ncat to run AutoHotkey remotely in Windows 10.
Server
You need install OpenSSH server, AutoHotkey and Ncat from the Nmap Project.
Chocolatey
Run PowerShell as an Administrator to install Chocolatey and other tools.
# Install Chocolatey
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
# Install the tools with Chocolatey
choco install autohotkey
choco install nmap
OpenSSH
Run PowerShell as an Administrator.
# Install the OpenSSH Server
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
# Start the sshd service
Start-Service sshd
# OPTIONAL but recommended:
Set-Service -Name sshd -StartupType 'Automatic'
# Get the public key generated on your client
$clientPublicKey = "****************"
# Copy the public key to the administrators_authorized_keys file
Add-Content -Force -Path $env:ProgramData\ssh\administrators_authorized_keys -Value "$clientPublicKey"
icacls.exe $env:ProgramData\ssh\administrators_authorized_keys /inheritance:r /grant Administrators:F /grant SYSTEM:F
Ncat
Run ncat to listen and execute ahk.exe after establishing a connection. ahk.exe is the executable file which is compiled from the AutoHotkey Script.
ncat --keep-open --listen --exec ahk.exe 127.0.0.1 12345
Client
If the client has PowerShell, you can follow this article to upload the client public key file to the server.
My client is Home Assistant Add-on: Terminal & SSH.
Use tmux and ssh to access the ncat server to run the executable file ahk.exe. The option --idle-timeout 10 is used for the lifetime of ahk.exe. When ncat stops, ahk.exe will also stop.
#!/bin/bash
tmux new -d -s remote
tmux send -t remote "ssh user@server 'ncat --idle-timeout 10 --send-only 127.0.0.1 12345'" Enter
Reference: