Skip to content
jswhPublic

About

An SSH client that keeps its own host list and returns remote output as structured JSON — with jump chains, encrypted password storage, and runtime independence from ~/.ssh/config

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

sshgo

sshgo

An SSH client that keeps its own host list and returns remote output as data.

License: MIT Go Version Release

English | 简体中文


sshgo is an SSH client written in Go. It talks the protocol itself through golang.org/x/crypto/ssh rather than shelling out, keeps the hosts you work with in one file of its own, and gives remote commands back as stdout, stderr and the remote exit code in JSON.

It is not a replacement for the whole ssh option grammar: ssh:// URIs and comma-separated hop lists are rejected, and IPv6 addresses are not supported. It does not verify host keys yet, which is stated in the limitations section below rather than buried in a footnote.

Get started

curl -fsSL https://github.com/jswh/sshgo/raw/main/install.sh | sh
export PATH="$HOME/.local/bin:$PATH"

sshgo config import          # take the hosts from ~/.ssh/config, once
sshgo exec web-01 "uptime"   # run something, get JSON back

The output of that second command is shaped like this:

{
  "command": "uptime",
  "stdout": " 10:12:03 up 41 days,  2:11,  1 user,  load average: 0.00, 0.01, 0.05\n",
  "stderr": "",
  "exit_code": 0,
  "duration": "412ms"
}

The import step is optional. You can work entirely with direct addresses (sshgo root@1.2.3.4:2222) and never import anything.

The three commands you will use most

exec: remote commands as structured data

sshgo exec db-staging "SELECT count(*) FROM users"
sshgo exec --raw web-01 "tail -n 100 /var/log/nginx/access.log"   # passthrough, no JSON
sshgo exec --sudo db-staging "systemctl restart postgresql"

The JSON carries command, stdout, stderr, exit_code and duration; duration is a human-readable string such as 412ms and is always present in the envelope. --raw writes the remote streams through unchanged and prints no JSON at all. exit_code is the remote command's own code, so a script can branch on it. Local failures (unknown host, no credentials, a hop that cannot be reached) always exit 1, deliberately: one exit code for everything that went wrong before the remote command ran, so the number your script reads never means two different things.

exec never asks for the Master Password, and it does not prompt for a host credential: when it finds none it fails instead. It tries the SSH agent, the host's IdentityFile (or, when it has none, the default key files), and a saved password, in that order. The last one is readable only while the password store is unlocked, by an Unlock Session from sshgo unlock (see "Three files, one master password" below). If a hop has no key to try and the store is locked, that is a local failure: exit 1, nothing on stdout — no JSON and no --raw output — and a message naming the host plus the two commands that fix it, sshgo unlock and sshgo config set-password <host>. If a key exists but the server refuses it while the store is locked, the usual JSON failure comes back and one hint line on stderr points at sshgo unlock; neither the exit code nor the JSON changes. This holds for every hop of a jump chain. A CI job cannot answer a prompt and cannot unlock, so the unlock is one deliberate act beforehand, never a question in the middle of a run.

The sudo password is sent to the target only, as stdin of its sudo -S. It is never part of any hop's authentication. exec --sudo resolves that password in a fixed order: a saved sudo password, then the saved SSH password, then a prompt on a terminal, and finally whatever the target's NOPASSWD configuration allows. Without a terminal the prompt is skipped, and the message points at sshgo unlock as well as sshgo config set-sudo-password <host>. That sudo prompt is the only one exec can make.

info: what sshgo will do for a name

sshgo info db1
sshgo info --json db1
Host: db1
  Hostname: 10.0.0.5
  Port:     22
  User:     ops
  Source:   imported
  Jump:     bastion
  Auth:
    - SSH Agent (3 keys)
    - Password store (encrypted)

--json returns name, hostname, port, user, source, auth_methods, meta (the user-defined fields, present for anything but a Direct Address), and jump when the host is reached through a chain. Authentication is reported as method names and counts: SSH Agent (3 keys), IdentityFile (key), Default key (id_ed25519), or Password store (encrypted). That last line means the encrypted store exists, not that this particular host has a credential in it. Key paths, passwords and passphrases never appear in any output; that exclusion is the contract, not a formatting choice. info resolves the chain and reports a broken one without dialling, so it reaches the same verdict a connection would.

Plain sshgo: pick from the list

Running sshgo with no arguments lists your hosts and lets you choose one. With a bare name it looks that name up in sshgo's own list only:

sshgo zg

If the name is not in the list, sshgo stops and names the three ways out, instead of quietly dialling zg as a hostname the way ssh would:

Host "zg" not found.
  Import your SSH config:  sshgo config import
  Add it manually:         sshgo config set zg hostname <address>
  Or connect directly:     sshgo user@host or sshgo host:port

That message is the contract: internal/host/resolve.go builds it, and internal/cli/cli_test.go:TestResolutionErrorText pins the text.

Anything containing @ or : is a direct address, sshgo's own syntax, and is always dialled as given. Port defaults to 22, user to root.

Where the host list comes from

sshgo config import reads ~/.ssh/config once and writes the hosts it describes into ~/.sshgo_config. From then on sshgo reads only its own file. Editing the system config does not change what sshgo does; that is the point, and it is also the cost.

The import is a snapshot, not a sync:

  • Values are the effective ones, after Host * defaults are folded in.
  • Wildcard and negated patterns (Host *, Host *.example.com) are skipped. sshgo connects to concrete hosts.
  • Re-running the import refreshes connection details and keeps your aliases, notes, tags and connection priorities.
  • A host you delete from ~/.ssh/config is not deleted here. Use sshgo config remove <host>.

The consequence is worth stating plainly: the two lists drift apart, and syncing them back is a manual act.

Metadata is edited by hand too:

sshgo config list                       # everything sshgo knows
sshgo config find --tag prod            # filter by tag
sshgo config set db1 notes "primary pg"   # keys: see the list below
sshgo config get db1 hostname
sshgo config unset db1 tags
sshgo config remove db1

config set and config get take exactly these keys: alias, notes, tags, connection-priority, hostname, port, user, identity-file, proxy-jump. Any other key is rejected, and the error prints that same list.

Reaching a host through a jump host

A jump host is an ordinary host in sshgo's list. Point the target at it:

sshgo config set bastion hostname 203.0.113.9
sshgo config set bastion user ops
sshgo config set db1 hostname 10.0.0.5
sshgo config set db1 proxy-jump bastion

sshgo exec db1 "pg_isready"    # dialled through bastion
sshgo info db1                 # Jump: bastion

Chains nest to any depth. If inner is only reachable from bastion:

sshgo config set inner proxy-jump bastion
sshgo config set db1 proxy-jump inner
sshgo info db1                 # Jump: bastion -> inner

Credentials never cross hops. A hop uses its own user, port, IdentityFile and saved password; nothing is inherited from the target and nothing is handed back to it. The SSH agent is the one thing shared across a chain, because it belongs to you rather than to a host.

The whole chain is resolved before anything is dialled. A missing hop, a hop with no address, or a loop fails immediately with the path spelled out, so a cycle reads as db1 -> bastion -> db1 rather than as a timeout. Forward references are legal: create the target first and its bastion later.

proxy-jump takes an sshgo host name, an alias, a direct address, or none for a direct connection. ssh's spellings are not accepted, because sshgo has exactly one addressing grammar.

Three files, one master password

~/.sshgo_config holds host metadata as plain JSON.

~/.sshgo_passwords holds credentials, encrypted with AES-GCM. The key is derived from your master password with argon2id (time 1, memory 64 MiB, threads 4, a 16-byte random salt), and the file is written with 0600 permissions. The master password is not stored anywhere and cannot be recovered; forget it and the saved credentials are gone with it.

~/.sshgo_session is the Unlock Session: the state sshgo unlock creates so that a caller with no terminal can use saved passwords. It holds the argon2id-derived store key, a fingerprint of the store it belongs to, and an absolute expiry. It is not the master password, and it holds no plaintext host password — only the derived key that decrypts the store. It is written 0600; on Linux and macOS that is a file mode, on Windows the protection is the ACL inherited from your user profile.

The window is bounded and explicit:

  • sshgo unlock asks for the master password, proves it by decrypting the store, and writes the session. It requires a terminal: unlocking is an act by a person.
  • The default lifetime is 12 hours. --ttl <duration> sets another one, with a floor of 1m and no infinite option. The expiry is absolute; it does not slide forward on use.
  • sshgo lock deletes the session immediately. If there is none it says so and still exits 0, because there was nothing to revoke.
  • An expired, corrupt, or foreign session is treated as no session and deleted on read, so a store copied from another machine reads as locked rather than as a wrong master password.

Read both halves of that. The password file is protected; the host list is not. Whoever can read your home directory can see which machines you connect to, under which account, on which ports, and which of them are reachable only through another. And while a session is valid, any process running as the same user can read your saved SSH and sudo passwords without the master password — that is the price of unattended password authentication, stated here rather than buried. If either cost is too high, wait for known_hosts verification and encrypted metadata to land, or keep the list somewhere you trust.

Within one invocation the store is opened at most once, so a chain that needs credentials on several hops does not ask you for the master password several times.

Install and update

curl -fsSL https://github.com/jswh/sshgo/raw/main/install.sh | sh    # Linux / macOS
irm https://github.com/jswh/sshgo/raw/main/install.ps1 | iex          # Windows PowerShell
go install github.com/jswh/sshgo@latest                               # Go toolchain

The install scripts place the latest release binary for your OS and architecture in ~/.local/bin. Building from source is go build -o sshgo in a clone of this repository.

sshgo update

update fetches the latest release from GitHub, compares its tag with the version compiled into the running binary, and replaces the binary if there is a newer one. If the binary sits in a system directory it falls back to sudo mv.

What sshgo does not do

These are current gaps, not roadmap hedges.

  • Host keys are not verified. sshgo uses InsecureIgnoreHostKey() on every hop. A jump chain does not change that: a compromised bastion can impersonate the target. known_hosts verification is a separate feature.
  • ssh's jump syntax is not supported. ssh:// URIs and comma-separated multi-hop values (bastion,inner) are rejected. Write the chain with proxy-jump per host instead.
  • A jump host must be an sshgo host or a direct address. A name sshgo does not manage cannot be a hop, because sshgo never reads ~/.ssh/config at runtime.
  • A valid Unlock Session is readable by any process running as you. For its lifetime (12 hours by default, or whatever --ttl set) saved SSH and sudo passwords can be decrypted without the master password by anything running under the same user account. sshgo lock ends that window early.
  • On Windows, the session's protection is an ACL, not a file mode. 0600 holds on Linux and macOS; on Windows the file inherits the permissions of your user profile.
  • IPv6 is not supported, neither in a host address nor in a jump value.

Commands

Command What it does
sshgo List hosts and pick one
sshgo <name> Connect to a host from sshgo's list
sshgo user@host[:port] Connect to an address given on the command line
sshgo exec [--raw] [--sudo] <host> <cmd> Run a command, get JSON or raw streams back
sshgo info [--json] <host> Show resolved connection details and the jump chain
sshgo unlock [--ttl <duration>] Unlock the password store for a bounded window
sshgo lock Revoke the current Unlock Session
sshgo config import Snapshot hosts out of ~/.ssh/config, once
sshgo config set|get|unset|remove Edit one host's metadata
sshgo config list / find --tag See and filter the list
sshgo config set-password / set-sudo-password Store credentials for a host
sshgo update Replace the binary with the latest release

Full flags are in sshgo <subcommand> --help.

The first argument wins: exec, info, config, unlock, lock and update are always subcommands, never host names. A host that happens to be called unlock is reachable only through an Alias. This is deliberate, and sshgo --help lists the same set.

Why it is built this way

Four decisions explain most of the surface:

  • ADR-0001: host metadata and credentials live in separate files, so listing your hosts does not need a password.
  • ADR-0002: sshgo never reads ~/.ssh/config at runtime; the import is the only touch point.
  • ADR-0003: a jump host is an sshgo host, which is what makes chains nestable and per-hop credentials possible.
  • ADR-0004: a bounded Unlock Session is the only way an unattended caller reaches saved passwords, and the ADR names the cost it adds to ADR-0001's security model.

CONTEXT.md is the glossary those documents use: Host, Alias, Direct Address, Jump Host, Jump Chain, hop, Credential Key. The names in the CLI are the names in that file.

Contributions are welcome. LICENSE is MIT.

About

An SSH client that keeps its own host list and returns remote output as structured JSON — with jump chains, encrypted password storage, and runtime independence from ~/.ssh/config

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages