gscp
is the ssh-config parser available in go.
gscp
is named as an abbreviation of go ssh config parser
.
import (
"github.com/harakeishi/gscp"
)
Visit the docs on GoDoc
If no arguments are passed to LoadConfig()
, ~/.ssh/config
will be read.
package main
import (
"fmt"
"github.com/harakeishi/gscp"
)
func main() {
// Reads a config file and gets it as a string
s, _ := gscp.LoadConfig()
// parse
r, _ := gscp.Parse(s)
fmt.Printf("%+v", r)
}
$ go run ./cmd/main.go
[{Name:testhost Options:[{Name:HostName Value:192.0.2.1} {Name:User Value:myuser} {Name:IdentityFile Value:~/.ssh/id_rsa} {Name:ServerAliveInterval Value:60}]}]
If you want to parse a config in a specific directory, pass the path as follows.
package main
import (
"fmt"
"github.com/harakeishi/gscp"
)
func main() {
// Reads a config file and gets it as a string
path := gscp.Path("./testData/test1_config")
s, _ := gscp.LoadConfig(path)
// parse
fmt.Println(gscp.Parse(s))
}
Obtains information on the specified host and the specified options.
package main
import (
"fmt"
"github.com/harakeishi/gscp"
)
func main() {
// Specify path
path := gscp.Path("./testData/test1_config")
// Load config.
s, _ := gscp.LoadConfig(path)
// Parse config.
hosts, _ := gscp.Parse(s)
// Obtain information on the specified host
host := hosts.FindHost("testhost")
fmt.Printf("%+v\n", host)
// Obtain information on specified options
hostname := host.FindOption("HostName").Value
fmt.Printf("%+v\n", hostname)
}
PS C:\src\smp> go run .\cmd\main.go
{Name:testhost Options:[{Name:HostName Value:192.0.2.1} {Name:User Value:myuser} {Name:IdentityFile Value:~/.ssh/id_rsa} {Name:ServerAliveInterval Value:60}]}
192.0.2.1
Copyright (c) 2023 harakeishi
Licensed under MIT