- Notifications
You must be signed in to change notification settings - Fork68
Go parser for ssh_config files
License
kevinburke/ssh_config
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This is a Go parser forssh_config
files. Importantly, this parser attemptsto preserve comments in a given file, so you can manipulate assh_config
filefrom a program, if your heart desires.
It's designed to be used with the excellentx/crypto/ssh package, which handles SSHnegotiation but isn't very easy to configure.
Thessh_config
Get()
andGetStrict()
functions will attempt to read valuesfrom$HOME/.ssh/config
and fall back to/etc/ssh/ssh_config
. The firstargument is the host name to match on, and the second argument is the key youwant to retrieve.
port:=ssh_config.Get("myhost","Port")
Certain directives can occur multiple times for a host (such asIdentityFile
),so you should use theGetAll
orGetAllStrict
directive to retrieve thoseinstead.
files:=ssh_config.GetAll("myhost","IdentityFile")
You can also load a config file and read values from it.
varconfig=`Host *.test Compression yes`cfg,err:=ssh_config.Decode(strings.NewReader(config))fmt.Println(cfg.Get("example.test","Port"))
Some SSH arguments have default values - for example, the default value forKeyboardAuthentication
is"yes"
. If you call Get(), and no value for thegiven Host/keyword pair exists in the config, we'll return a default for thekeyword if one exists.
Here's how you can manipulate an SSH config file, and then write it back todisk.
f,_:=os.Open(filepath.Join(os.Getenv("HOME"),".ssh","config"))cfg,_:=ssh_config.Decode(f)for_,host:=rangecfg.Hosts {fmt.Println("patterns:",host.Patterns)for_,node:=rangehost.Nodes {// Manipulate the nodes as you see fit, or use a type switch to// distinguish between Empty, KV, and Include nodes.fmt.Println(node.String()) }}// Print the config to stdout:fmt.Println(cfg.String())
Wherever possible we try to implement the specification as documented inthessh_config
manpage. Unimplemented features should be present in theissues list.
Notably, theMatch
directive is currently unsupported.
This is the secondcomment-preserving configuration parser I've written, afteran /etc/hosts parser. Eventually, I will write one for every Linuxfile format.
Thank you very much to Tailscale and Indeed for sponsoring development of thislibrary.Sponsors will get their names featured in the README.
You can also reach out about a consulting engagement:https://burke.services
About
Go parser for ssh_config files