ssh-config

WebJar for ssh-config

License

License

MIT
Categories

Categories

config Application Layer Libs Configuration
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

ssh-config
Last Version

Last Version

1.1.5
Release Date

Release Date

Type

Type

jar
Description

Description

ssh-config
WebJar for ssh-config
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/cyjake/ssh-config

Download ssh-config

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/ssh-config/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>ssh-config</artifactId>
    <version>1.1.5</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/ssh-config/
implementation 'org.webjars.npm:ssh-config:1.1.5'
// https://jarcasting.com/artifacts/org.webjars.npm/ssh-config/
implementation ("org.webjars.npm:ssh-config:1.1.5")
'org.webjars.npm:ssh-config:jar:1.1.5'
<dependency org="org.webjars.npm" name="ssh-config" rev="1.1.5">
  <artifact name="ssh-config" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='ssh-config', version='1.1.5')
)
libraryDependencies += "org.webjars.npm" % "ssh-config" % "1.1.5"
[org.webjars.npm/ssh-config "1.1.5"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

SSH Config Parser & Stringifier

NPM Downloads NPM Version Build Status Coverage Status

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.

.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 or other criteria

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

config.remove({ Host: '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

References

Versions

Version
1.1.5