sshconf-upsert

Upsert hosts into ~/.ssh/config file(s)

sshconf-upsert

Upsert hosts into your ~/.ssh/config file(s).

$ npm install sshconf-upsert

require('sshconf-upsert')(contents, hosts, callback)

var upsert = require('sshconf-upsert')
  , fs = require('fs')
 
var config = fs.readFileSync(process.env.HOME + '/.ssh/config', 'utf8')
 
upsert(config, [{
  Host: 'second',
  HostName: 'google.com'
}, {
  Host: 'third',
  HostName: '192.167.2.16'
}], function(errupserted) {
  fs.writeFileSync(process.env.HOME + '/.ssh/config', upserted)
})

Essentially, sshconf-upsert takes a config string and an array of hosts, returning an updated version. Existing entries will be modified, and missing ones added to the end. The above script should take this:

Host first
  HostName 192.168.2.1
 
# comments and formatting
# are preserved
 
Host second
  HostName 192.168.2.4

And turn it into this:

Host first
  HostName 192.168.2.1
 
# comments and formatting
# are preserved
 
Host second
  HostName google.com
 
Host third
  HostName 192.168.2.16