Help us shape the future of Go! Take the 2026 Go Developer Survey

nsshconfig

package module
v0.0.0-...-a484302 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2019 License: MIT Imports: 15 Imported by: 1

README

OpenSSH config parser

Create, list, edit and delete entries

OpenSSH Reference

Examples

List

package main

import (
    "fmt"

    "github.com/nicolascb/nsshconfig"
)

func main() {
    // By default, this filepath is ~/.ssh/config, to change it use: 
    // nsshconfig.SetConfigPath(/path/to/config)
    err := nsshconfig.LoadConfig() 
    if err != nil {
        panic(err)
    }

    fmt.Printf("Found %d entries",nsshconfig.TotalEntries())
    for _, host := range nsshconfig.Hosts() {
       fmt.Printf("#######\n")
       fmt.Printf("Host: %s\n", host.Host)
       fmt.Printf("Options: \n")
       for option, value := range host.Options {
           fmt.Printf("%s=%s\n",option,value)
       }
    }
}

Delete

package main

import (
    "fmt"

    "github.com/nicolascb/nsshconfig"
)

func main() {
    // By default, this filepath is ~/.ssh/config, to change it use: 
    // nsshconfig.SetConfigPath(/path/to/config)
    err := nsshconfig.LoadConfig() 
    if err != nil {
        panic(err)
    }

    // Delete host server01
    err = nsshconfig.Delete("server01")
    if err != nil {
        panic(err)
    }

    // Write file
    err = nsshconfig.WriteConfig()
    if err != nil {
        panic(err)
    }
}

Edit

package main

import (
    "fmt"

    "github.com/nicolascb/nsshconfig"
)

func main() {
    // By default, this filepath is ~/.ssh/config, to change it use: 
    // nsshconfig.SetConfigPath(/path/to/config)
    err := nsshconfig.LoadConfig() 
    if err != nil {
        panic(err)
    }

    // Edit general entry (*)
    host, err := nsshconfig.GetEntryByHost("*")
    if err != nil {
        panic(err)
    }

    // Save
    host.Options["port"] = "5122"
    err = host.Save()
    if err != nil {
        panic(err)
    }

    // Write file
    err = nsshconfig.WriteConfig()
    if err != nil {
        panic(err)
    }
}

Create

package main

import (
    "fmt"

    "github.com/nicolascb/nsshconfig"
)

func main() {
    // By default, this filepath is ~/.ssh/config, to change it use: 
    // nsshconfig.SetConfigPath(/path/to/config)
    err := nsshconfig.LoadConfig() 
    if err != nil {
        panic(err)
    }

    // Options
    options := make(map[string]string)
    options["port"] = "5133"
    options["hostname"] = "gremio.net"
    
    // Add (update if exist)
    err = nsshconfig.New("gremio_server", options)
    if err != nil {
        panic(err)
    }
    
    // Write file
    err = nsshconfig.WriteConfig()
    if err != nil {
        panic(err)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clear

func Clear()

Clear entries

func CurrentUser

func CurrentUser() *user.User

CurrentUser get current user

func Delete

func Delete(host string) error

Delete host

func ExistHost

func ExistHost(host string) bool

ExistHost is a check if exist host

func GetPathConfig

func GetPathConfig() string

GetPathConfig return ~/.ssh/config

func LoadConfig

func LoadConfig() error

LoadConfig read and parse ~/.ssh/config

func New

func New(host string, options map[string]string) error

New entry

func Prop

func Prop(field string, asc bool) func(p1, p2 *Entry) bool

Thanks: https://stackoverflow.com/a/29000710

func SetConfigPath

func SetConfigPath(file string)

SetConfigPath set config file

func TotalEntries

func TotalEntries() int

TotalEntries get total entries

func WriteConfig

func WriteConfig() error

WriteConfig write config

Types

type By

type By func(p1, p2 *Entry) bool

func (By) Sort

func (by By) Sort(entries []*Entry)

type Entry

type Entry struct {
	Host    string
	Options map[string]string
}

Entry config

func GetEntryByHost

func GetEntryByHost(host string) (*Entry, error)

GetEntryByHost return entry by host

func Hosts

func Hosts() []*Entry

Hosts get hosts

func (*Entry) Decode

func (e *Entry) Decode() string

Decode entry

func (*Entry) Save

func (e *Entry) Save() error

Save host

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL