Make SSH host key utilities publicly accessible

pull/32/head
Hidde Beydals 5 years ago
parent 6017946144
commit 43876b5ab9

@ -14,10 +14,10 @@ import (
// Any errors (e.g. authentication failures) are ignored, except if // Any errors (e.g. authentication failures) are ignored, except if
// no key could be collected from the host. // no key could be collected from the host.
func ScanHostKey(host string, timeout time.Duration) ([]byte, error) { func ScanHostKey(host string, timeout time.Duration) ([]byte, error) {
col := &collector{} col := &HostKeyCollector{}
config := &ssh.ClientConfig{ config := &ssh.ClientConfig{
HostKeyCallback: col.StoreKey(), HostKeyCallback: col.StoreKey(),
Timeout: timeout, Timeout: timeout,
} }
client, err := ssh.Dial("tcp", host, config) client, err := ssh.Dial("tcp", host, config)
if err == nil { if err == nil {
@ -29,7 +29,9 @@ func ScanHostKey(host string, timeout time.Duration) ([]byte, error) {
return col.knownKeys, err return col.knownKeys, err
} }
type collector struct { // HostKeyCollector offers a StoreKey method which provides an
// HostKeyCallBack to collect public keys from an SSH server.
type HostKeyCollector struct {
knownKeys []byte knownKeys []byte
} }
@ -37,7 +39,7 @@ type collector struct {
// To collect multiple public key types from the host, multiple // To collect multiple public key types from the host, multiple
// SSH dials need with the ClientConfig HostKeyAlgorithms set to // SSH dials need with the ClientConfig HostKeyAlgorithms set to
// the algorithm you want to collect. // the algorithm you want to collect.
func (c *collector) StoreKey() ssh.HostKeyCallback { func (c *HostKeyCollector) StoreKey() ssh.HostKeyCallback {
return func(hostname string, remote net.Addr, key ssh.PublicKey) error { return func(hostname string, remote net.Addr, key ssh.PublicKey) error {
c.knownKeys = append( c.knownKeys = append(
c.knownKeys, c.knownKeys,
@ -46,3 +48,8 @@ func (c *collector) StoreKey() ssh.HostKeyCallback {
return nil return nil
} }
} }
// GetKnownKeys returns the collected public keys in bytes.
func (c *HostKeyCollector) GetKnownKeys() []byte {
return c.knownKeys
}
Loading…
Cancel
Save