Documentation
¶
Overview ¶
Package discover finds machines on the network that are not yet under management, and renders them as inventory entries.
Index ¶
- Constants
- func Annotate(results []Result, known KnownHosts)
- func AnnotateSeen(results []Result, seen map[string]bool)
- func EmitInventory(w io.Writer, results []Result, taken map[string]struct{}, command string, ...) error
- func ExpandTargets(specs []string, max int) ([]netip.Addr, error)
- func LocalSubnets() ([]string, error)
- func ParseBanner(banner string) (software, osHint string)
- type KnownHosts
- type Options
- type Result
- type Scanner
Constants ¶
const DefaultMaxTargets = 65536
DefaultMaxTargets caps an accidental scan of an enormous range. A /16 fits under it; anything larger has to be asked for explicitly.
Variables ¶
This section is empty.
Functions ¶
func Annotate ¶
func Annotate(results []Result, known KnownHosts)
Annotate marks results that already appear in the inventory, meaning hlab has credentials for them.
func AnnotateSeen ¶
AnnotateSeen marks results that a previous scan already recorded.
func EmitInventory ¶
func EmitInventory(w io.Writer, results []Result, taken map[string]struct{}, command string, now time.Time) error
EmitInventory writes a YAML fragment describing the given results, ready to review and merge into the config. It deliberately writes to a stream rather than editing the config in place: the operator decides what to keep, and what user each machine should be reached as.
taken is the set of host names already in use, so suggested names never collide with the existing inventory.
func ExpandTargets ¶
ExpandTargets turns scan specs into a de-duplicated address list. Each spec is one of:
192.0.2.0/24 a CIDR block 192.0.2.10 a single address 192.0.2.10-192.0.2.50 an inclusive range 192.0.2.10-50 an inclusive range, last octet shorthand
For IPv4 blocks of /30 or larger the network and broadcast addresses are skipped, since nothing listens on them.
func LocalSubnets ¶
LocalSubnets returns the IPv4 prefixes attached to this machine's up, non-loopback interfaces. It is what `hlab discover scan` uses when given no explicit range.
func ParseBanner ¶
ParseBanner extracts the software version and an OS hint from an SSH identification string such as:
SSH-2.0-OpenSSH_9.2p1 Debian-2+deb12u2
Types ¶
type KnownHosts ¶
KnownHosts maps an IP literal to the inventory host name that claims it.
func BuildKnownHosts ¶
func BuildKnownHosts(ctx context.Context, addressByName map[string]string, timeout time.Duration) KnownHosts
BuildKnownHosts resolves inventory addresses down to IP literals so a discovered machine is recognised even when the inventory refers to it by DNS name. Addresses that fail to resolve are still indexed verbatim, so an unresolvable name never causes a managed host to be reported as new.
nameByAddress maps inventory host name -> configured address.
type Options ¶
type Options struct {
// Ports to probe on every address. Defaults to 22.
Ports []int
// ProbeTimeout bounds each TCP connect. Dead addresses cost this much, so
// on a LAN it should stay short.
ProbeTimeout time.Duration
// BannerTimeout bounds the wait for an SSH identification string. It only
// applies to addresses that already accepted a connection.
BannerTimeout time.Duration
// Concurrency is how many probes run at once.
Concurrency int
// ResolveNames enables reverse DNS for every hit.
ResolveNames bool
// OnProgress, if set, is called periodically during long scans.
OnProgress func(scanned, total, found int)
// ProgressEvery controls how often OnProgress fires. Defaults to 3s.
ProgressEvery time.Duration
}
Options configure a scan.
type Result ¶
type Result struct {
Address string `json:"address" yaml:"address"`
Port int `json:"port" yaml:"port"`
Hostname string `json:"hostname,omitempty" yaml:"hostname,omitempty"`
Banner string `json:"banner,omitempty" yaml:"banner,omitempty"`
Software string `json:"software,omitempty" yaml:"software,omitempty"`
OS string `json:"os,omitempty" yaml:"os,omitempty"`
LatencyMs int64 `json:"latencyMs" yaml:"latencyMs"`
// Managed is set when the address already appears in the inventory, i.e.
// hlab has credentials for it.
Managed bool `json:"managed" yaml:"managed"`
ManagedAs string `json:"managedAs,omitempty" yaml:"managedAs,omitempty"`
Ignored bool `json:"ignored,omitempty" yaml:"ignored,omitempty"`
// SeenBefore is set when a previous scan already recorded this machine.
// It is independent of Managed: a machine can be seen repeatedly and still
// never be brought under management.
SeenBefore bool `json:"seenBefore" yaml:"seenBefore"`
// Change is how this sighting compared to the stored record: "new",
// "changed" or "unchanged". Empty when the scan was not recorded.
Change string `json:"change,omitempty" yaml:"change,omitempty"`
// Changes lists what differed, e.g. "ssh: OpenSSH_9.6p1 -> OpenSSH_10.0p2".
Changes []string `json:"changes,omitempty" yaml:"changes,omitempty"`
// contains filtered or unexported fields
}
Result is one reachable address:port found by a scan.
type Scanner ¶
type Scanner struct {
// contains filtered or unexported fields
}
Scanner probes a set of addresses for reachable TCP ports.
func NewScanner ¶
NewScanner returns a Scanner with defaults filled in for any unset option.