Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Go parser for ssh_config files

License

NotificationsYou must be signed in to change notification settings

kevinburke/ssh_config

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_configGet() 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.

Manipulating SSH config files

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())

Spec compliance

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.

Errata

This is the secondcomment-preserving configuration parser I've written, afteran /etc/hosts parser. Eventually, I will write one for every Linuxfile format.

Sponsorships

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


[8]ページ先頭

©2009-2025 Movatter.jp