Skip to content

Add support for RemoteCommand option in kitten ssh#9859

Merged
kovidgoyal merged 2 commits intokovidgoyal:masterfrom
zzhaolei:feature/support-ssh-remote-command
Apr 14, 2026
Merged

Add support for RemoteCommand option in kitten ssh#9859
kovidgoyal merged 2 commits intokovidgoyal:masterfrom
zzhaolei:feature/support-ssh-remote-command

Conversation

@zzhaolei
Copy link
Copy Markdown
Contributor

Problem

When using kitten ssh, users cannot specify RemoteCommand in ssh_config, since OpenSSH can only execute a single RemoteCommand.

Solution

This PR adds support for a dedicated remote_command option in kitten ssh as an alternative. When users specify this option, kitten will execute the provided command on the remote machine after establishing the SSH connection, with behavior similar to OpenSSH's RemoteCommand option.

Changes

1. Added remote_command configuration option

File: kittens/ssh/main.py

  • New option that accepts a shell command to execute on the remote machine
  • Supports complex commands and shell constructs
  • Fully documented in the option's help text

2. Implemented command execution logic

File: kittens/ssh/main.go

  • Added prepare_remote_cmd() function to handle remote command preparation
  • Supports both shell script and Python script contexts with appropriate encoding (base64 for Python)
  • Integrated remote command into bootstrap script setup with proper placeholders

3. Updated bootstrap scripts

Files: shell-integration/ssh/bootstrap.py and bootstrap.sh

  • Added execution of remote command if specified
  • Properly decodes and executes the command in the user's shell
  • Maintains compatibility with existing shell integration

Usage Example

remote_command if command -v fish >/dev/null 2>&1; then exec fish; else exec $SHELL -l; fi

The above command will check if the fish shell is available on the remote machine. If it exists, it will be executed; otherwise, the default login shell will be used instead.

@kovidgoyal
Copy link
Copy Markdown
Owner

Why not just use the actual RemoteCommand in the ssh config, as
described here: #3988 (comment)

Remember to reset the RemoteCOmmand to empty on the actual command line
when executing ssh.

@zzhaolei zzhaolei force-pushed the feature/support-ssh-remote-command branch from 77e3851 to 9b02a59 Compare April 14, 2026 03:11
@zzhaolei zzhaolei changed the title Add support for remote_command option in kitten ssh Add support for RemoteCommand option in kitten ssh Apr 14, 2026
@zzhaolei
Copy link
Copy Markdown
Contributor Author

zzhaolei commented Apr 14, 2026

Sorry, I didn’t see this issue earlier.

Right now my approach is to parse the SSH config (ssh -G) to extract the user-defined RemoteCommand, then execute it inside the kitten script, while explicitly overriding (RemoteCommand=none) on the actual ssh invocation to avoid double execution.

At the moment I’m only parsing remotecommand. Do you think it’s necessary to also parse and handle other fields from the SSH config in this context?

@kovidgoyal
Copy link
Copy Markdown
Owner

kovidgoyal commented Apr 14, 2026 via email

@zzhaolei
Copy link
Copy Markdown
Contributor Author

zzhaolei commented Apr 14, 2026

Does running ssh -G take a lot of time?

This is the speed of executing ssh -G on my machine(Apple M4 Pro):

$ time /usr/bin/ssh -G bd-edit
...
________________________________________________________
Executed in   13.72 millis    fish           external
   usr time    4.73 millis    0.42 millis    4.30 millis
   sys time    6.16 millis    2.20 millis    3.96 millis

@kovidgoyal
Copy link
Copy Markdown
Owner

Hmm For me:

On Linux:
hyperfine -N 'ssh -G main'    master 
Benchmark 1: ssh -G main
Time (mean ± σ): 3.8 ms ± 0.3 ms [User: 2.4 ms, System: 1.3 ms]
Range (min … max): 3.2 ms … 5.2 ms 654 runs
On macOS:
hyperfine --warmup 10 -N 'ssh -G main'
Benchmark 1: ssh -G main
Time (mean ± σ): 3.9 ms ± 0.1 ms [User: 1.5 ms, System: 0.9 ms]
Range (min … max): 3.7 ms … 4.2 ms 713 runs

I guess it depends on how much you have in your ssh config files too.

4 ms is ok 13 ms is probably not. Possibly have it run at the beginning
of run_ssh() asynchronously and wait for the results only when actually
needed close to the bottom of run_ssh()

@kovidgoyal kovidgoyal closed this Apr 14, 2026
@kovidgoyal
Copy link
Copy Markdown
Owner

Apologies closed this accidentally.

@kovidgoyal kovidgoyal reopened this Apr 14, 2026
@zzhaolei
Copy link
Copy Markdown
Contributor Author

I refactored it into an asynchronous version. Based on simple testing, it reduced latency by about 5ms.

test like:

now := time.Now()
...
debugprintln(time.Since(now))

@kovidgoyal kovidgoyal merged commit 8697da9 into kovidgoyal:master Apr 14, 2026
14 checks passed
@zzhaolei zzhaolei deleted the feature/support-ssh-remote-command branch April 14, 2026 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants