MCPcopy Index your code
hub / github.com/cyjake/ssh-config

github.com/cyjake/ssh-config @v5.2.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v5.2.0 ↗ · + Follow
47 symbols 147 edges 16 files 6 documented · 13%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

SSH Config Parser & Stringifier

NPM Downloads NPM Version JSR Version Build Status Deno CI Node CI codecov

Usage

const SSHConfig = require('ssh-config')

const config = SSHConfig.parse(`
  IdentityFile ~/.ssh/id_rsa

  Host tahoe
    HostName tahoe.com

  Host walden
    HostName waldenlake.org

  Host *
    User keanu
    ForwardAgent true
`)

expect(config).to.eql(
  [ { "param": "IdentityFile",
      "value": "~/.ssh/id_rsa" },
    { "param": "Host",
      "value": "tahoe",
      "config":
        [ { "param": "HostName",
            "value": "tahoe.com" } ] },
    { "param": "Host",
      "value": "walden",
      "config":
        [ { "param": "HostName",
            "value": "waldenlake.org" } ] },
    { "param": "Host",
      "value": "*",
      "config":
        [ { "param": "User",
            "value": "keanu" },
          { "param": "ForwardAgent",
            "value": "true" } ] } ]
)

// Change the HostName in the Host walden section
const section = config.find({ Host: 'walden' })

for (const line of section.config) {
  if (line.param === 'HostName') {
    line.value = 'waldenlake.org'
    break
  }
}

// The original whitespaces and comments are preserved.
console.log(SSHConfig.stringify(config))
// console.log(config.toString())

Iterating over Sections

One needs to iterate over ssh configs mostly because of two reasons.

  • to .find the corresponding section and modify it, or
  • to .compute the ssh config about certain Host.

.compute Parameters by Host

You can use config.compute method to compute applied parameters of certain host.

expect(config.compute('walden')).to.eql({
  IdentityFile: [
    '~/.ssh/id_rsa'
  ],
  Host: 'walden',
  HostName: 'waldenlake.org',
  User: 'nil',
  ForwardAgent: 'true'
})

NOTICE According to ssh_config(5), the first obtained parameter value will be used. So we cannot override existing parameters. It is suggested that the general settings shall be at the end of your config file.

The IdentityFile parameter always contain an array to make possible multiple IdentityFile settings to be able to coexist.

Case-Insensitive Matching

OpenSSH treats configuration directives case-insensitively. By default, compute() preserves the original case from the config file. To normalize directive names to lowercase (matching OpenSSH behavior), use the ignoreCase option:

const config = SSHConfig.parse(`
  Host example
    hOsTnaME 1.2.3.4
    USER admin
`)

// Default - preserves original case
config.compute('example')
// => { hOsTnaME: '1.2.3.4', USER: 'admin' }

// With ignoreCase - lowercase to match OpenSSH
config.compute('example', { ignoreCase: true })
// => { hostname: '1.2.3.4', user: 'admin' }

#### Match Exec

`Match exec` criteria are evaluated by default to match OpenSSH behavior. If
the config text may be untrusted, pass `{ matchExec: false }` to skip
`Match exec` sections while still computing `Host` sections and non-exec
`Match` criteria:

```js
config.compute('walden', { matchExec: false })

.find sections by Host or Match

NOTICE: This method is provided to find the corresponding section in the parsed config for config manipulation. It is NOT intended to compute config of certain Host. For latter case, use .compute(host) instead.

To ditch boilerplate codes like the for loop shown earlier, we can use the .find(opts) available in the parsed config object.

config.find({ Host: 'example1' })
// or the ES2015 Array.prototype.find
config.find(line => line.param == 'Host' && line.value == 'example1')

.remove sections by Host / Match or function

To remove sections, we can pass the section to .remove(opts).

config.remove({ Host: 'example1' })
// or the ES2015 Array.prototype.find
config.remove(line => line.param == 'Host' && line.value == 'example1')

.append sections

Since the parsed config is a sub class of Array, you can append new sections with methods like .push or .concat.

config.push(...SSHConfig.parse(`
Host ness
  HostName lochness.com
  User dinosaur
`))

expect(config.find({ Host: '*' })).to.eql(
  { "param": "Host",
    "value": "ness",
    "config":
     [ { "param": "HostName",
         "value": "lochness.com" } ] }
)

If the section to append is vanilla JSON, .append is what you need.

const config = new SSHConfig()

config.append({
  Host: 'ness',
  HostName: 'lochness.com',
  User: 'dinosaur'
})

SSHConfig.stringify(config)
// =>
// Host ness
//   HostName lochness.com
//   User dinosaur

.prepend sections

But appending options to the end of the config isn't very effective if your config is organizated per the recommendations of ssh_config(5) that the generic options are at at the end of the config, such as:

Host ness
  HostName lochness.com
  User dinosaur

IdentityFile ~/.ssh/id_rsa

The config could get messy if you put new options after the line of IdentityFile. To work around this issue, it is recommended that .prepend should be used instead. For the example above, we can prepend new options at the beginning of the config:

config.prepend({
  Host: 'tahoe',
  HostName 'tahoe.com',
})

The result would be:

Host tahoe
  HostName tahoe.com

Host ness
  HostName lochness.com
  User dinosaur

IdentityFile ~/.ssh/id_rsa

If there are generic options at the beginning of the config, and you'd like the prepended section put before the first existing section, please turn on the second argument of .prepend:

config.prepend({
  Host: 'tahoe',
  HostName 'tahoe.com',
}, true)

The result would be like:

IdentityFile ~/.ssh/id_rsa

Host tahoe
  HostName tahoe.com

Host ness
  HostName lochness.com
  User dinosaur

References

Extension points exported contracts — how you extend this code

Directive (Interface)
(no doc)
src/ssh-config.ts
Section (Interface)
(no doc)
src/ssh-config.ts
Match (Interface)
(no doc)
src/ssh-config.ts
Comment (Interface)
(no doc)
src/ssh-config.ts
Empty (Interface)
(no doc)
src/ssh-config.ts

Core symbols most depended-on inside this repo

parse
called by 81
src/ssh-config.ts
parse
called by 80
src/ssh-config.ts
compute
called by 53
src/ssh-config.ts
glob
called by 47
src/glob.ts
find
called by 34
src/ssh-config.ts
stringify
called by 34
src/ssh-config.ts
toString
called by 26
src/ssh-config.ts
heredoc
called by 20
test/helpers.ts

Shape

Function 24
Interface 10
Method 10
Class 2
Enum 1

Languages

TypeScript100%

Modules by API surface

src/ssh-config.ts42 symbols
src/glob.ts3 symbols
test/helpers.ts2 symbols

For agents

$ claude mcp add ssh-config \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact