feat: NetBox insights, host tools and configurable TUI
Test / test (pull_request) Successful in 4m15s

- info/open/whois: NetBox details (site, nested locations, rack units,
  role, platform, tenant, IPs per interface), web page, reverse IP lookup
- cache status, site, role, platform, tenant, location chain and rack;
  ask before connecting to hosts in ssh.warn_status (--force skips)
- ssh rules and command restrictions match site/role/platform/tenant/status
- tui.columns selects what the TUI shows; optional TCP reachability column;
  detail view (Ctrl+L), NetBox page (Ctrl+G), status/location filters
- resolver.fallback tries other strategies' addresses when unreachable
- copy-id, known-hosts reset and address-change notices
- export ansible (YAML/INI) and export ssh-config
- token: keyring stores NetBox tokens in the system keyring
- fix stale menu number in testenv configure test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-18 12:33:59 +01:00
1 parent e5bde9c61f
commit 1660bb3098
39 files changed
+4868 -96

No files matched your search

+119 -34
View File
@@ -32,25 +32,32 @@ import (
// managedSubcommands are dispatched to cobra; everything else is treated as SSH wrapper mode.
var managedSubcommands = map[string]bool{
"doctor": true, "explain": true, "favorites": true, "history": true, "batch": true, "config": true,
"configure": true,
"notes": true,
"search": true,
"cache": true,
"alias": true,
"hook": true,
"shell-init": true,
"completion": true,
"__complete": true,
"help": true,
"version": true,
"uninstall": true,
"sftp": true,
"scp": true,
"tunnel": true,
"hosts": true,
"multi": true,
"sync": true,
"run": true,
"configure": true,
"notes": true,
"search": true,
"cache": true,
"alias": true,
"hook": true,
"shell-init": true,
"completion": true,
"__complete": true,
"help": true,
"version": true,
"uninstall": true,
"sftp": true,
"scp": true,
"tunnel": true,
"hosts": true,
"multi": true,
"sync": true,
"run": true,
"open": true,
"info": true,
"whois": true,
"copy-id": true,
"export": true,
"token": true,
"known-hosts": true,
}
func main() {
@@ -221,6 +228,49 @@ type resolvedHost struct {
Kind string
IP string
Tags []string
ID int // NetBox ID; 0 for static hosts and old cache entries
Status string
Site config.Ref
Role config.Ref
Platform config.Ref
Tenant config.Ref
Location []config.Ref
Rack string
Position string
// PreviousIP is the cached address a live lookup replaced, if it differs.
PreviousIP string
}
// hostFromEntry converts a cache entry into a resolved host.
func hostFromEntry(entry cache.Entry, origin string) resolvedHost {
return resolvedHost{
Name: entry.Name, Kind: entry.Kind, IP: entry.IP, Tags: entry.Tags, Origin: origin, Resolver: entry.Resolver, CachedAt: entry.CachedAt,
ID: entry.ID, Status: entry.Status, Site: cache.RefOf(entry.Site), Role: cache.RefOf(entry.Role), Platform: cache.RefOf(entry.Platform), Tenant: cache.RefOf(entry.Tenant),
Location: entry.Location, Rack: entry.Rack, Position: entry.Position,
}
}
// LocationPath renders site › locations › rack › U position.
func (h resolvedHost) LocationPath() string {
return config.LocationPath(h.Site, h.Location, h.Rack, h.Position)
}
// cacheEntry records a NetBox host and its resolved address in the cache.
func cacheEntry(target netbox.HostEntry, ip, strategy string) cache.Entry {
return cache.Entry{
Name: target.Target(), IP: ip, Kind: target.Kind, Tags: target.Tags, Cluster: target.Cluster, Resolver: strategy,
ID: target.ID, Status: target.Status, Site: cache.RefPtr(target.Site), Role: cache.RefPtr(target.Role), Platform: cache.RefPtr(target.Platform), Tenant: cache.RefPtr(target.Tenant),
Location: target.Location, Rack: target.Rack, Position: target.Position,
}
}
// upsertResolved caches a live result and reports the address it replaced.
func upsertResolved(c *cache.Cache, entry cache.Entry) (previous string) {
if old, _ := c.Get(entry.Name); old.Name != "" && !old.Static && old.IP != "" && old.IP != entry.IP {
previous = old.IP
}
c.Upsert(entry)
return previous
}
// resolveTarget finds host in the cache (also by shortcut) or NetBox and
@@ -250,7 +300,7 @@ func resolveTargetE(cfg *config.Config, host string, record bool) (resolvedHost,
if entry.Static {
origin = "static"
}
return resolvedHost{Name: entry.Name, Kind: entry.Kind, IP: entry.IP, Tags: entry.Tags, Origin: origin, Resolver: entry.Resolver, CachedAt: entry.CachedAt}
return hostFromEntry(entry, origin)
}
// Static hosts from the config win and need no NetBox.
@@ -328,10 +378,11 @@ func resolveTargetE(cfg *config.Config, host string, record bool) (resolvedHost,
return resolvedHost{}, fmt.Errorf("IP resolution for %q: %w", host, err)
}
entry := cache.Entry{Name: target.Target(), IP: ip, Kind: target.Kind, Tags: target.Tags, Cluster: target.Cluster, Resolver: strategy}
c.Upsert(entry)
entry := cacheEntry(target, ip, strategy)
previous := upsertResolved(c, entry)
result := used(entry)
result.Origin = "live"
result.PreviousIP = previous
return result, nil
}
@@ -375,6 +426,20 @@ func connectHost(cfg *config.Config, client internalssh.Client, flags internalss
}
fatalf("name a forward or a spec such as 8080:localhost:80; forwards for %s: %s", host.Name, available)
}
if flags.DryRun {
if cfg.SSH.WarnsStatus(host.Status) {
fmt.Fprintf(os.Stderr, "# warning: %s has NetBox status %q\n", host.Name, host.Status)
}
} else {
if err := confirmStatus(cfg, host, flags.Force); err != nil {
fatalf("%v", err)
}
port := connectPort(client, args, dests)
host = applyFallback(cfg, host, opts, flags, port)
if host.PreviousIP != "" {
offerKnownHostsCleanup(opts, host, port)
}
}
if err := runPreConnect(opts.PreConnectCommand(), host, client.Name, flags.DryRun); err != nil {
fatalf("%v", err)
}
@@ -401,8 +466,7 @@ func connectHost(cfg *config.Config, client internalssh.Client, flags internalss
// hostOptions merges the global, instance, and rule SSH options for host.
func hostOptions(cfg *config.Config, host resolvedHost) config.SSHOptions {
instance, name := netbox.SplitTarget(host.Name)
return cfg.SSHOptionsFor(config.SSHHost{Instance: instance, Name: name, Kind: host.Kind, Tags: host.Tags})
return cfg.SSHOptionsFor(commandHost(host))
}
// execClient replaces the process with the client, or prints the command for --dry-run.
@@ -466,9 +530,15 @@ func runTUI() {
}
return err
})
known := func(name, kind string, tags []string) resolvedHost {
if e, _ := c.Get(name); e.Name != "" && e.Kind == kind {
return hostFromEntry(e, "cache")
}
return resolvedHost{Name: name, Kind: kind, Tags: tags}
}
m.SetCommands(func(name, kind string, tags []string) []string {
var names []string
h := commandHost(resolvedHost{Name: name, Kind: kind, Tags: tags})
h := commandHost(known(name, kind, tags))
for name, c := range cfg.Commands {
if c.Allowed(h) {
names = append(names, name)
@@ -483,8 +553,9 @@ func runTUI() {
}
m.SetNotes(notes.New(cfg.NetBox.URL, nbClient))
m.SetForwards(func(name, kind string, tags []string) []string {
return forwardNames(hostOptions(cfg, resolvedHost{Name: name, Kind: kind, Tags: tags}))
return forwardNames(hostOptions(cfg, known(name, kind, tags)))
})
configureTUIExtras(m, cfg, nbClient, known)
p := tea.NewProgram(m)
final, err := p.Run()
if err != nil {
@@ -523,10 +594,9 @@ func runTUI() {
}
// Revalidate stale or live TUI results through the configured resolver chain.
var tags []string
var resolved resolvedHost
if cached, fresh := c.Get(host.Name); fresh && cached.Kind == host.Kind {
host.IP = cached.IP
tags = cached.Tags
resolved = hostFromEntry(cached, "cache")
} else {
if nbClient == nil {
fatalf("cannot resolve %q: NetBox is not configured", host.Name)
@@ -546,16 +616,19 @@ func runTUI() {
if err != nil {
fatalf("IP resolution: %v", err)
}
c.Upsert(cache.Entry{Name: target.Target(), IP: host.IP, Kind: target.Kind, Tags: target.Tags, Cluster: target.Cluster, Resolver: strategy})
tags = target.Tags
entry := cacheEntry(target, host.IP, strategy)
previous := upsertResolved(c, entry)
resolved = hostFromEntry(entry, "live")
resolved.PreviousIP = previous
}
host.IP = resolved.IP
fmt.Fprintf(os.Stderr, "Connecting to %s (%s)…\n", host.Name, host.IP)
c.MarkUsed(host.Name)
_ = c.Save()
client, flags, args := tuiCommand(cfg, host)
connectHost(cfg, client, flags, resolvedHost{Name: host.Name, Kind: host.Kind, IP: host.IP, Tags: tags}, []int{len(args) - 1}, args)
connectHost(cfg, client, flags, resolved, []int{len(args) - 1}, args)
}
// tuiCommand builds the client and arguments for the action chosen in the TUI.
@@ -632,7 +705,8 @@ func rootCmd() *cobra.Command {
},
}
root.AddCommand(doctorCmd(), explainCmd(), favoritesCmd(), historyCmd(), batchCmd(), configCmd(), runCmd(), notesCmd(), configureCmd(), searchCmd(), cacheCmd(), aliasCmd(), hookCmd(), shellInitCmd(), versionCmd(), uninstallCmd(), sftpCmd(), scpCmd(), tunnelCmd(), hostsCmd(), multiCmd(), syncCmd())
root.AddCommand(doctorCmd(), explainCmd(), favoritesCmd(), historyCmd(), batchCmd(), configCmd(), runCmd(), notesCmd(), configureCmd(), searchCmd(), cacheCmd(), aliasCmd(), hookCmd(), shellInitCmd(), versionCmd(), uninstallCmd(), sftpCmd(), scpCmd(), tunnelCmd(), hostsCmd(), multiCmd(), syncCmd(),
openCmd(), infoCmd(), whoisCmd(), copyIDCmd(), exportCmd(), tokenCmd(), knownHostsCmd())
// cobra builds the "completion" command lazily; force init so we can extend it.
root.InitDefaultCompletionCmd()
@@ -883,6 +957,7 @@ func cacheRefreshCmd() *cobra.Command {
return fmt.Errorf("resolver: %w", err)
}
written := 0
var changed []string
nameCounts := make(map[string]int)
for _, e := range entries {
nameCounts[strings.ToLower(e.Target())]++
@@ -906,7 +981,9 @@ func cacheRefreshCmd() *cobra.Command {
fmt.Fprintf(os.Stderr, "warning: skipping %s: %v\n", e.Name, err)
continue
}
c.Upsert(cache.Entry{Name: e.Target(), IP: ip, Kind: e.Kind, Tags: e.Tags, Cluster: e.Cluster, Resolver: strategy})
if previous := upsertResolved(c, cacheEntry(*e, ip, strategy)); previous != "" {
changed = append(changed, fmt.Sprintf("%s: %s → %s", e.Target(), previous, ip))
}
written++
}
@@ -924,6 +1001,14 @@ func cacheRefreshCmd() *cobra.Command {
}
}
fmt.Printf("%d entries written to cache.\n", written)
if len(changed) > 0 {
sort.Strings(changed)
fmt.Printf("%d hosts changed their address:\n", len(changed))
for _, line := range changed {
fmt.Printf(" %s\n", line)
}
fmt.Println("Remove outdated host keys with: netssh known-hosts reset HOST")
}
return nil
},
}