Debian Codegraph @

README.md

raw symbols SCIP
1 # ssh_config
2
3 This is a Go parser for `ssh_config` files. Importantly, this parser attempts
4 to preserve comments in a given file, so you can manipulate a `ssh_config` file
5 from a program, if your heart desires.
6
7 It's designed to be used with the excellent
8 [x/crypto/ssh](https://golang.org/x/crypto/ssh) package, which handles SSH
9 negotiation but isn't very easy to configure.
10
11 The `ssh_config` `Get()` and `GetStrict()` functions will attempt to read values
12 from `$HOME/.ssh/config` and fall back to `/etc/ssh/ssh_config`. The first
13 argument is the host name to match on, and the second argument is the key you
14 want to retrieve.
15
16 ```go
17 port := ssh_config.Get("myhost", "Port")
18 ```
19
20 Certain directives can occur multiple times for a host (such as `IdentityFile`),
21 so you should use the `GetAll` or `GetAllStrict` directive to retrieve those
22 instead.
23
24 ```go
25 files := ssh_config.GetAll("myhost", "IdentityFile")
26 ```
27
28 You can also load a config file and read values from it.
29
30 ```go
31 var config = `
32 Host *.test
33 Compression yes
34 `
35
36 cfg, err := ssh_config.Decode(strings.NewReader(config))
37 fmt.Println(cfg.Get("example.test", "Port"))
38 ```
39
40 Some SSH arguments have default values - for example, the default value for
41 `KeyboardAuthentication` is `"yes"`. If you call Get(), and no value for the
42 given Host/keyword pair exists in the config, we'll return a default for the
43 keyword if one exists.
44
45 ### Manipulating SSH config files
46
47 Here's how you can manipulate an SSH config file, and then write it back to
48 disk.
49
50 ```go
51 f, _ := os.Open(filepath.Join(os.Getenv("HOME"), ".ssh", "config"))
52 cfg, _ := ssh_config.Decode(f)
53 for _, host := range cfg.Hosts {
54 fmt.Println("patterns:", host.Patterns)
55 for _, node := range host.Nodes {
56 // Manipulate the nodes as you see fit, or use a type switch to
57 // distinguish between Empty, KV, and Include nodes.
58 fmt.Println(node.String())
59 }
60 }
61
62 // Print the config to stdout:
63 fmt.Println(cfg.String())
64 ```
65
66 ## Spec compliance
67
68 Wherever possible we try to implement the specification as documented in
69 the `ssh_config` manpage. Unimplemented features should be present in the
70 [issues][issues] list.
71
72 Notably, the `Match` directive is currently unsupported.
73
74 [issues]: https://github.com/iamFrancescoFerro/ssh_config/issues
75
76 ## Errata
77
78 This is the second [comment-preserving configuration parser][blog] I've written, after
79 [an /etc/hosts parser][hostsfile]. Eventually, I will write one for every Linux
80 file format.
81
82 [blog]: https://kev.inburke.com/kevin/more-comment-preserving-configuration-parsers/
83 [hostsfile]: https://github.com/iamFrancescoFerro/hostsfile
84
85 ## Sponsorships
86
87 Thank you very much to Tailscale and Indeed for sponsoring development of this
88 library. [Sponsors][sponsors] will get their names featured in the README.
89
90 You can also reach out about a consulting engagement: https://burke.services
91
92 [sponsors]: https://github.com/sponsors/kevinburke