feat: add pre_connect hook and optional shortcut alias display
- ssh.pre_connect: local shell command run before every connection, layered like proxy_jump (global, instance/static host, rules; "none" disables). Host details are passed as NETSSH_* env vars, a non-zero exit aborts the connection, --dry-run only prints it. - shortcuts.show: show shortcut aliases in the TUI host list, an ALIAS column in `cache list` / `hosts list` (also via --aliases) and in Tab completion. - Both editable in `netssh configure`; README and CHANGELOG updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAuVpPncumMViLLc8N4
This commit is contained in:
1 parent
3804c33669
commit
419c682a71
15 files changed
+507
-23
No files matched your search
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox"
|
||||
)
|
||||
|
||||
// preConnectRunner runs the hook; tests replace it to avoid spawning a shell.
|
||||
var preConnectRunner = runPreConnectCommand
|
||||
|
||||
// preConnectEnv describes the host to the hook through environment variables.
|
||||
func preConnectEnv(host resolvedHost, clientName string) []string {
|
||||
instance, name := netbox.SplitTarget(host.Name)
|
||||
return []string{
|
||||
"NETSSH_HOST=" + name,
|
||||
"NETSSH_TARGET=" + host.Name,
|
||||
"NETSSH_INSTANCE=" + instance,
|
||||
"NETSSH_IP=" + host.IP,
|
||||
"NETSSH_KIND=" + host.Kind,
|
||||
"NETSSH_TAGS=" + strings.Join(host.Tags, ","),
|
||||
"NETSSH_CLIENT=" + clientName,
|
||||
}
|
||||
}
|
||||
|
||||
// runPreConnect runs the configured pre_connect hook for host. Its output goes
|
||||
// to stderr so it never mixes with the output of a remote command or scp.
|
||||
// A failing hook returns an error and the connection must not be opened.
|
||||
func runPreConnect(command string, host resolvedHost, clientName string, dryRun bool) error {
|
||||
if command == "" {
|
||||
return nil
|
||||
}
|
||||
if dryRun {
|
||||
fmt.Fprintf(os.Stderr, "# pre_connect (skipped in --dry-run): %s\n", command)
|
||||
return nil
|
||||
}
|
||||
if err := preConnectRunner(command, preConnectEnv(host, clientName), os.Stdin, os.Stderr); err != nil {
|
||||
return fmt.Errorf("pre_connect hook for %s failed: %w", host.Name, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func runPreConnectCommand(command string, env []string, stdin io.Reader, out io.Writer) error {
|
||||
cmd := exec.Command("sh", "-c", command)
|
||||
cmd.Env = append(os.Environ(), env...)
|
||||
cmd.Stdin = stdin
|
||||
cmd.Stdout = out
|
||||
cmd.Stderr = out
|
||||
return cmd.Run()
|
||||
}
|
||||
Reference in new issue
Block a user