Handing Off Remote Commands
Bridge the last interactive step without making the human retype a command.
SSH transports the command to the remote clipboard; the human remains the
security boundary by reviewing, pasting, and executing it locally.
Workflow
- Confirm ordinary SSH reaches the intended machine and identify its clipboard
command. Use
pbcopy on macOS, wl-copy on Wayland, or xclip -selection clipboard on X11. Never install a clipboard utility as part of the handoff.
- Build the command locally with a single-quoted heredoc so local variables,
command substitutions, backticks, and quotes remain literal.
- Pipe it over SSH to the clipboard command. Copying is not execution: tell the
human to paste, review, and run it in a local terminal.
- If the result is needed remotely, make the pasted command redirect only the
required output to a mode-0600 file under
$HOME/.cache/agent-command-handoffs/. Use a random token in its filename.
Shell redirection must happen outside sudo, so the file remains owned by
the user.
- After the human confirms completion, read the bounded result over SSH, remove
the result file, and clear the remote clipboard. Do not retain the command or
output elsewhere.
macOS Pattern
Command-only handoff:
cat <<'REMOTE_COMMAND' | ssh -- "$target" pbcopy
sudo launchctl kickstart -k system/com.example.service
REMOTE_COMMAND
Handoff with a retrievable result:
token="$(openssl rand -hex 8)"
result_rel=".cache/agent-command-handoffs/$token.out"
cat <<REMOTE_COMMAND | ssh -- "$target" pbcopy
umask 077
mkdir -p "\$HOME/.cache/agent-command-handoffs"
sudo example-inspect --read-only > "\$HOME/$result_rel" && echo "Diagnostic complete"
REMOTE_COMMAND
After the human runs it:
ssh -- "$target" "cat ~/$result_rel && rm -f ~/$result_rel"
printf '' | ssh -- "$target" pbcopy
Quote the outer heredoc delimiter when no generated token or result path must be
inserted. When interpolation is needed, interpolate only locally generated,
validated path components and escape every remote $ as shown above.
Guardrails
- Use this only with a machine and SSH account the user authorized.
- Put no password, token, recovery code, decrypted secret, or sensitive result
on a clipboard. Universal Clipboard and clipboard managers may replicate or
retain clipboard contents.
- Keep the pasted command narrow and visible. Never hide it behind encoded text,
a downloader,
eval, or an opaque script.
- Use the pattern to cross an interactive boundary, not to bypass one. The human
explicitly runs privileged commands and handles authentication locally.
- Capture only output needed for the task. Prefer a short filtered query over a
database dump or broad system log.
- Treat a missing result file as unknown execution state. Ask the human what the
terminal showed instead of rerunning a potentially non-idempotent command.
1---2name: handing-off-remote-commands3description: Copies commands to an authorized remote machine's clipboard over SSH and retrieves explicitly captured results. Use when a human must run a local interactive or privileged command on another machine but cannot conveniently transfer the command between devices.4---56# Handing Off Remote Commands78Bridge the last interactive step without making the human retype a command.9SSH transports the command to the remote clipboard; the human remains the10security boundary by reviewing, pasting, and executing it locally.1112## Workflow13141. Confirm ordinary SSH reaches the intended machine and identify its clipboard15 command. Use `pbcopy` on macOS, `wl-copy` on Wayland, or `xclip -selection16clipboard` on X11. Never install a clipboard utility as part of the handoff.172. Build the command locally with a single-quoted heredoc so local variables,18 command substitutions, backticks, and quotes remain literal.193. Pipe it over SSH to the clipboard command. Copying is not execution: tell the20 human to paste, review, and run it in a local terminal.214. If the result is needed remotely, make the pasted command redirect only the22 required output to a mode-0600 file under23 `$HOME/.cache/agent-command-handoffs/`. Use a random token in its filename.24 Shell redirection must happen outside `sudo`, so the file remains owned by25 the user.265. After the human confirms completion, read the bounded result over SSH, remove27 the result file, and clear the remote clipboard. Do not retain the command or28 output elsewhere.2930## macOS Pattern3132Command-only handoff:3334```bash35cat <<'REMOTE_COMMAND' | ssh -- "$target" pbcopy36sudo launchctl kickstart -k system/com.example.service37REMOTE_COMMAND38```3940Handoff with a retrievable result:4142```bash43token="$(openssl rand -hex 8)"44result_rel=".cache/agent-command-handoffs/$token.out"4546cat <<REMOTE_COMMAND | ssh -- "$target" pbcopy47umask 07748mkdir -p "\$HOME/.cache/agent-command-handoffs"49sudo example-inspect --read-only > "\$HOME/$result_rel" && echo "Diagnostic complete"50REMOTE_COMMAND51```5253After the human runs it:5455```bash56ssh -- "$target" "cat ~/$result_rel && rm -f ~/$result_rel"57printf '' | ssh -- "$target" pbcopy58```5960Quote the outer heredoc delimiter when no generated token or result path must be61inserted. When interpolation is needed, interpolate only locally generated,62validated path components and escape every remote `$` as shown above.6364## Guardrails6566- Use this only with a machine and SSH account the user authorized.67- Put no password, token, recovery code, decrypted secret, or sensitive result68 on a clipboard. Universal Clipboard and clipboard managers may replicate or69 retain clipboard contents.70- Keep the pasted command narrow and visible. Never hide it behind encoded text,71 a downloader, `eval`, or an opaque script.72- Use the pattern to cross an interactive boundary, not to bypass one. The human73 explicitly runs privileged commands and handles authentication locally.74- Capture only output needed for the task. Prefer a short filtered query over a75 database dump or broad system log.76- Treat a missing result file as unknown execution state. Ask the human what the77 terminal showed instead of rerunning a potentially non-idempotent command.