feat: suggest the host behind unknown DNS names
A name neither in NetBox nor in the cache is resolved through DNS, using dns.domains for short names. The hosts owning the address in NetBox or the cache are offered together with a direct connection; without a terminal the suggestions are part of the error. Afterwards netssh offers to save the name as a link. info, open and whois use the same lookup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5c41a5eb6f
commit
793f095e80
5 files changed
+531
-9
No files matched your search
+20
-5
@@ -29,12 +29,15 @@ func hostCompletion(cmd *cobra.Command, args []string, toComplete string) ([]cob
|
||||
// netboxHost finds the NetBox record of a host given like on the command line
|
||||
// (shortcut, instance/host). The cache supplies the ID when it has one.
|
||||
func netboxHost(ctx context.Context, cfg *config.Config, target string) (netbox.HostEntry, *netbox.Client, resolvedHost, error) {
|
||||
h, err := resolveTargetE(cfg, target, false)
|
||||
h, err := resolveOrSuggest(cfg, target, false)
|
||||
if err != nil {
|
||||
return netbox.HostEntry{}, nil, h, err
|
||||
}
|
||||
if h.Kind == config.StaticKind {
|
||||
switch h.Kind {
|
||||
case config.StaticKind:
|
||||
return netbox.HostEntry{}, nil, h, fmt.Errorf("%s is a static host (hosts:) and has no NetBox record", h.Name)
|
||||
case "dns":
|
||||
return netbox.HostEntry{}, nil, h, fmt.Errorf("%s (%s) is not in NetBox", h.Name, h.IP)
|
||||
}
|
||||
client := newNetBoxClient(cfg)
|
||||
if client == nil {
|
||||
@@ -122,12 +125,12 @@ func infoCmd() *cobra.Command {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
h, err := resolveTargetE(cfg, args[0], false)
|
||||
h, err := resolveOrSuggest(cfg, args[0], false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
out := cmd.OutOrStdout()
|
||||
if h.Kind == config.StaticKind {
|
||||
if h.Kind == config.StaticKind || h.Kind == "dns" {
|
||||
return printStaticInfo(out, cfg, h, asJSON)
|
||||
}
|
||||
client := newNetBoxClient(cfg)
|
||||
@@ -164,7 +167,11 @@ func printStaticInfo(w io.Writer, cfg *config.Config, h resolvedHost, asJSON boo
|
||||
return json.NewEncoder(w).Encode(map[string]any{"host": map[string]any{"name": h.Name, "kind": h.Kind, "address": h.IP, "user": user, "port": port, "tags": h.Tags}})
|
||||
}
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
|
||||
fmt.Fprintf(tw, "%s\t(static host from hosts:)\n", h.Name)
|
||||
origin := "(static host from hosts:)"
|
||||
if h.Kind == "dns" {
|
||||
origin = "(DNS name, not in NetBox)"
|
||||
}
|
||||
fmt.Fprintf(tw, "%s\t%s\n", h.Name, origin)
|
||||
row(tw, "Address", h.IP)
|
||||
row(tw, "User", user)
|
||||
if port > 0 {
|
||||
@@ -290,6 +297,14 @@ prefixes containing it. A hostname is resolved through DNS first.`,
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
addresses, err := whoisAddresses(ctx, args[0])
|
||||
if err != nil {
|
||||
// A short name like mx123 may resolve with a shortcut domain.
|
||||
if names := dnsNames(cfg, args[0]); len(names) > 0 && names[0] != args[0] {
|
||||
if _, found := lookupDNS(ctx, names); len(found) > 0 {
|
||||
addresses, err = found, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user