Files
ssh-netbox-wrapper/cmd/netssh/export.go
Sebastian UnterschützandClaude Opus 5.5 1660bb3098
Test / test (pull_request) Successful in 4m15s
feat: NetBox insights, host tools and configurable TUI
- 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>
2026-11-18 12:33:59 +01:00

524 lines
16 KiB
Go

package main
import (
"fmt"
"io"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/spf13/cobra"
"go.yaml.in/yaml/v3"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/cache"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/storage"
)
// exportFilter selects the cached hosts to export.
type exportFilter struct {
tag, kind, instance, status string
noStatic bool
output string
}
func (f *exportFilter) register(cmd *cobra.Command) {
cmd.Flags().StringVar(&f.tag, "tag", "", "Only hosts with this tag")
cmd.Flags().StringVar(&f.kind, "kind", "", "Only this kind: device, vm, or static")
cmd.Flags().StringVar(&f.instance, "instance", "", "Only hosts of this NetBox instance")
cmd.Flags().StringVar(&f.status, "status", "", "Only hosts with this NetBox status, e.g. active")
cmd.Flags().BoolVar(&f.noStatic, "no-static", false, "Leave out static hosts (hosts:)")
cmd.Flags().StringVarP(&f.output, "output", "o", "", "Write to this file instead of stdout")
_ = cmd.RegisterFlagCompletionFunc("kind", cobra.FixedCompletions([]cobra.Completion{"device", "vm", "static"}, cobra.ShellCompDirectiveNoFileComp))
}
func (f exportFilter) matches(e cache.Entry) bool {
instance, _ := netbox.SplitTarget(e.Name)
switch {
case f.noStatic && e.Static,
f.kind != "" && !strings.EqualFold(e.Kind, f.kind),
f.instance != "" && !strings.EqualFold(instance, f.instance),
f.status != "" && !strings.EqualFold(e.Status, f.status),
f.tag != "" && !hasTag(e.Tags, f.tag):
return false
}
return true
}
// exportHost is one host with the connection settings netssh would use.
type exportHost struct {
entry cache.Entry
name string // unique inventory / Host name
address string
user string
port int
opts config.SSHOptions
}
// exportHosts loads the filtered cache entries. Hostnames that exist in
// several instances get the instance as prefix ("prod-web01"), others stay bare.
func exportHosts(cfg *config.Config, f exportFilter) ([]exportHost, error) {
c := newCache(cfg)
if err := c.Load(); err != nil {
return nil, err
}
var entries []cache.Entry
counts := map[string]int{}
for _, e := range c.All() {
if !f.matches(e) {
continue
}
entries = append(entries, e)
_, name := netbox.SplitTarget(e.Name)
counts[strings.ToLower(name)]++
}
sort.Slice(entries, func(i, j int) bool { return entries[i].Name < entries[j].Name })
out := make([]exportHost, 0, len(entries))
for _, e := range entries {
instance, name := netbox.SplitTarget(e.Name)
if counts[strings.ToLower(name)] > 1 && instance != "" {
name = instance + "-" + name
}
h := hostFromEntry(e, "cache")
user, port := hostDefaults(cfg, h)
out = append(out, exportHost{entry: e, name: name, address: e.IP, user: user, port: port, opts: hostOptions(cfg, h)})
}
if len(out) == 0 {
return nil, fmt.Errorf("no cached hosts match; run netssh cache refresh first")
}
return out, nil
}
// writeExport writes data to the --output file, or stdout.
func writeExport(f exportFilter, data []byte) error {
if f.output == "" || f.output == "-" {
_, err := os.Stdout.Write(data)
return err
}
if err := storage.WriteFile(f.output, data, 0o600); err != nil {
return err
}
fmt.Fprintf(os.Stderr, "Written → %s\n", f.output)
return nil
}
func exportCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "export",
Short: "Export cached hosts as Ansible inventory or ssh_config",
}
cmd.AddCommand(exportAnsibleCmd(), exportSSHConfigCmd())
return cmd
}
// ansibleGroupKeys are the attributes --group-by accepts.
var ansibleGroupKeys = []string{"tag", "site", "location", "rack", "role", "platform", "tenant", "kind", "instance", "cluster", "status"}
func exportAnsibleCmd() *cobra.Command {
var f exportFilter
var format, groupBy string
var noVars bool
cmd := &cobra.Command{
Use: "ansible",
Short: "Print an Ansible inventory (YAML or INI) of the cached hosts",
Long: `Print an Ansible inventory of the cached hosts. Every host gets ansible_host
(the address netssh connects to), ansible_user, ansible_port and, with a jump
host, ansible_ssh_common_args. NetBox attributes are added as netbox_* host
variables (disable with --no-vars).
Groups are formed from host attributes, named like tag_prod, site_fsn1,
role_router or platform_junos. Choose them with --group-by (comma-separated:
` + strings.Join(ansibleGroupKeys, ", ") + `, or none).`,
Example: ` netssh export ansible > inventory.yml
netssh export ansible --format ini --group-by site,role -o hosts.ini
netssh export ansible --tag prod --status active`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.Load()
if err != nil {
return err
}
keys, err := parseGroupBy(groupBy)
if err != nil {
return err
}
hosts, err := exportHosts(cfg, f)
if err != nil {
return err
}
inv := buildInventory(hosts, keys, !noVars)
var data []byte
switch strings.ToLower(format) {
case "yaml", "yml":
data, err = inv.yaml()
case "ini":
data = inv.ini()
default:
return fmt.Errorf("--format must be yaml or ini")
}
if err != nil {
return err
}
return writeExport(f, data)
},
}
f.register(cmd)
cmd.Flags().StringVar(&format, "format", "yaml", "Inventory format: yaml or ini")
cmd.Flags().StringVar(&groupBy, "group-by", "tag,site,role,platform,kind", "Attributes to group hosts by, or none")
cmd.Flags().BoolVar(&noVars, "no-vars", false, "Leave out the netbox_* host variables")
_ = cmd.RegisterFlagCompletionFunc("format", cobra.FixedCompletions([]cobra.Completion{"yaml", "ini"}, cobra.ShellCompDirectiveNoFileComp))
return cmd
}
func parseGroupBy(value string) ([]string, error) {
if strings.EqualFold(strings.TrimSpace(value), "none") || strings.TrimSpace(value) == "" {
return nil, nil
}
var keys []string
for _, key := range strings.Split(value, ",") {
key = strings.ToLower(strings.TrimSpace(key))
valid := false
for _, known := range ansibleGroupKeys {
valid = valid || key == known
}
if !valid {
return nil, fmt.Errorf("--group-by: unknown attribute %q; use %s, or none", key, strings.Join(ansibleGroupKeys, ", "))
}
keys = append(keys, key)
}
return keys, nil
}
// inventoryVar is a host variable; list values stay lists in YAML.
type inventoryVar struct {
key string
value any // string, int, or []string
}
type inventory struct {
hosts []string
vars map[string][]inventoryVar
groups map[string][]string
}
var groupInvalid = regexp.MustCompile(`[^a-z0-9_]+`)
// groupName builds a valid Ansible group name such as site_fsn1.
func groupName(prefix, value string) string {
name := strings.Trim(groupInvalid.ReplaceAllString(strings.ToLower(value), "_"), "_")
if name == "" {
return ""
}
return prefix + "_" + name
}
func buildInventory(hosts []exportHost, groupBy []string, netboxVars bool) inventory {
inv := inventory{vars: map[string][]inventoryVar{}, groups: map[string][]string{}}
for _, h := range hosts {
e := h.entry
inv.hosts = append(inv.hosts, h.name)
vars := []inventoryVar{{"ansible_host", h.address}}
if h.user != "" {
vars = append(vars, inventoryVar{"ansible_user", h.user})
}
if h.port > 0 && h.port != 22 {
vars = append(vars, inventoryVar{"ansible_port", h.port})
}
if jump := h.opts.ProxyJump; jump != "" && !strings.EqualFold(jump, "none") {
vars = append(vars, inventoryVar{"ansible_ssh_common_args", "-o ProxyJump=" + jump})
}
instance, _ := netbox.SplitTarget(e.Name)
if netboxVars && !e.Static {
for _, pair := range [][2]string{
{"netbox_instance", instance}, {"netbox_kind", e.Kind}, {"netbox_status", e.Status},
{"netbox_site", cache.RefOf(e.Site).Name}, {"netbox_role", cache.RefOf(e.Role).Name},
{"netbox_platform", cache.RefOf(e.Platform).Name}, {"netbox_tenant", cache.RefOf(e.Tenant).Name},
{"netbox_cluster", e.Cluster}, {"netbox_location", locationNames(e.Location)},
{"netbox_rack", e.Rack}, {"netbox_position", e.Position},
} {
if pair[1] != "" {
vars = append(vars, inventoryVar{pair[0], pair[1]})
}
}
if e.ID > 0 {
vars = append(vars, inventoryVar{"netbox_id", e.ID})
}
if len(e.Tags) > 0 {
vars = append(vars, inventoryVar{"netbox_tags", append([]string(nil), e.Tags...)})
}
}
inv.vars[h.name] = vars
for _, key := range groupBy {
var values []string
switch key {
case "tag":
values = e.Tags
case "site":
values = []string{cache.RefOf(e.Site).Key()}
case "location":
// Every level, so a play can target a building or a single room.
for _, location := range e.Location {
values = append(values, location.Key())
}
case "rack":
values = []string{e.Rack}
case "role":
values = []string{cache.RefOf(e.Role).Key()}
case "platform":
values = []string{cache.RefOf(e.Platform).Key()}
case "tenant":
values = []string{cache.RefOf(e.Tenant).Key()}
case "kind":
values = []string{e.Kind}
case "instance":
values = []string{instance}
case "cluster":
values = []string{e.Cluster}
case "status":
values = []string{e.Status}
}
for _, value := range values {
if group := groupName(key, value); group != "" {
inv.groups[group] = append(inv.groups[group], h.name)
}
}
}
}
return inv
}
func (inv inventory) groupNames() []string {
names := make([]string, 0, len(inv.groups))
for name := range inv.groups {
names = append(names, name)
}
sort.Strings(names)
return names
}
func exportHeader(comment string) string {
return fmt.Sprintf("%s Generated by netssh export on %s — changes are overwritten on the next export.\n", comment, time.Now().Format(time.RFC3339))
}
// yaml renders the inventory with host variables under all.hosts and the
// groups as children of all, listing their member hosts.
func (inv inventory) yaml() ([]byte, error) {
node := func(kind yaml.Kind, value string) *yaml.Node {
n := &yaml.Node{Kind: kind, Value: value}
if kind == yaml.ScalarNode {
n.Tag = "!!str"
}
return n
}
mapping := func() *yaml.Node { return &yaml.Node{Kind: yaml.MappingNode} }
add := func(m *yaml.Node, key string, value *yaml.Node) {
m.Content = append(m.Content, node(yaml.ScalarNode, key), value)
}
hosts := mapping()
for _, name := range inv.hosts {
vars := mapping()
for _, v := range inv.vars[name] {
var value *yaml.Node
switch typed := v.value.(type) {
case int:
value = &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!int", Value: strconv.Itoa(typed)}
case []string:
value = &yaml.Node{Kind: yaml.SequenceNode}
for _, item := range typed {
value.Content = append(value.Content, node(yaml.ScalarNode, item))
}
default:
value = node(yaml.ScalarNode, fmt.Sprint(typed))
}
add(vars, v.key, value)
}
add(hosts, name, vars)
}
all := mapping()
add(all, "hosts", hosts)
if len(inv.groups) > 0 {
children := mapping()
for _, group := range inv.groupNames() {
members := mapping()
for _, host := range inv.groups[group] {
add(members, host, &yaml.Node{Kind: yaml.MappingNode, Style: yaml.FlowStyle})
}
g := mapping()
add(g, "hosts", members)
add(children, group, g)
}
add(all, "children", children)
}
root := mapping()
add(root, "all", all)
var b strings.Builder
b.WriteString(exportHeader("#"))
enc := yaml.NewEncoder(&b)
enc.SetIndent(2)
if err := enc.Encode(&yaml.Node{Kind: yaml.DocumentNode, Content: []*yaml.Node{root}}); err != nil {
return nil, err
}
if err := enc.Close(); err != nil {
return nil, err
}
return []byte(b.String()), nil
}
// ini renders host variables in [all] and one section per group. Lists such
// as netbox_tags become comma-separated strings, since INI has no lists.
func (inv inventory) ini() []byte {
var b strings.Builder
b.WriteString(exportHeader(";"))
b.WriteString("[all]\n")
for _, name := range inv.hosts {
b.WriteString(name)
for _, v := range inv.vars[name] {
value := fmt.Sprint(v.value)
if list, ok := v.value.([]string); ok {
value = strings.Join(list, ",")
}
fmt.Fprintf(&b, " %s=%s", v.key, iniQuote(value))
}
b.WriteString("\n")
}
for _, group := range inv.groupNames() {
fmt.Fprintf(&b, "\n[%s]\n", group)
for _, host := range inv.groups[group] {
b.WriteString(host + "\n")
}
}
return []byte(b.String())
}
// iniQuote quotes values that Ansible's INI parser (shlex) would split.
func iniQuote(value string) string {
if value != "" && !strings.ContainsAny(value, " \t\"'\\#;=") {
return value
}
return `"` + strings.NewReplacer(`\`, `\\`, `"`, `\"`).Replace(value) + `"`
}
func exportSSHConfigCmd() *cobra.Command {
var f exportFilter
var noAliases bool
cmd := &cobra.Command{
Use: "ssh-config",
Short: "Print ssh_config Host blocks for the cached hosts",
Long: `Print one ssh_config Host block per cached host with the address, user, port,
jump host and SSH options netssh would use. Tools that run ssh themselves
(VS Code Remote, rsync, git, Ansible) can then reach the hosts by name.
Include the file from ~/.ssh/config:
netssh export ssh-config -o ~/.ssh/netssh.conf
echo 'Include ~/.ssh/netssh.conf' >> ~/.ssh/config # once
Shortcut aliases are added as extra Host names (disable with --no-aliases).
extra_args, forwards and hooks have no ssh_config equivalent and are skipped.`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.Load()
if err != nil {
return err
}
hosts, err := exportHosts(cfg, f)
if err != nil {
return err
}
aliases := map[string]string{}
if !noAliases {
c := newCache(cfg)
_ = c.Load()
aliases = shortcutAliases(c.All(), *cfg)
}
var b strings.Builder
writeSSHConfig(&b, hosts, aliases)
return writeExport(f, []byte(b.String()))
},
}
f.register(cmd)
cmd.Flags().BoolVar(&noAliases, "no-aliases", false, "Do not add shortcut aliases as Host names")
return cmd
}
var sshConfigUnsafe = regexp.MustCompile(`[\s"#*?!,]`)
func writeSSHConfig(w io.Writer, hosts []exportHost, aliases map[string]string) {
fmt.Fprint(w, exportHeader("#"))
used := map[string]bool{}
for _, h := range hosts {
used[strings.ToLower(h.name)] = true
}
for _, h := range hosts {
if sshConfigUnsafe.MatchString(h.name) {
fmt.Fprintf(w, "\n# skipped %q: not usable as a Host name\n", h.name)
continue
}
names := []string{h.name}
if alias := aliases[strings.ToLower(h.entry.Name)]; alias != "" && !used[strings.ToLower(alias)] && !sshConfigUnsafe.MatchString(alias) {
used[strings.ToLower(alias)] = true
names = append(names, alias)
}
fmt.Fprintf(w, "\nHost %s\n", strings.Join(names, " "))
_, bare := netbox.SplitTarget(h.entry.Name)
hostName := h.address
if h.opts.Target == "hostname" && plainName(bare) {
hostName = bare
}
line := func(key, value string) {
if value != "" {
fmt.Fprintf(w, " %s %s\n", key, value)
}
}
line("HostName", hostName)
if h.opts.Target == "name" && plainName(bare) {
line("HostKeyAlias", bare)
}
line("User", h.user)
if h.port > 0 && h.port != 22 {
line("Port", strconv.Itoa(h.port))
}
if jump := h.opts.ProxyJump; jump != "" {
line("ProxyJump", jump)
}
switch h.opts.HostKeyChecking {
case "strict":
line("StrictHostKeyChecking", "yes")
case "accept-new":
line("StrictHostKeyChecking", "accept-new")
case "off":
line("StrictHostKeyChecking", "no")
line("UserKnownHostsFile", "/dev/null")
}
line("AddressFamily", h.opts.AddressFamily)
keys := make([]string, 0, len(h.opts.Options))
for key := range h.opts.Options {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
line(key, h.opts.Options[key])
}
if len(h.opts.ExtraArgs) > 0 {
fmt.Fprintf(w, " # extra_args not exported: %s\n", strings.Join(h.opts.ExtraArgs, " "))
}
}
}
var plainNamePattern = regexp.MustCompile(`^[A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?$`)
func plainName(name string) bool { return plainNamePattern.MatchString(name) }
// locationNames joins a location chain as "Building A/Room 2.04".
func locationNames(chain []config.Ref) string {
names := make([]string, 0, len(chain))
for _, location := range chain {
names = append(names, location.Name)
}
return strings.Join(names, "/")
}