Pty to find its own shell

This commit is contained in:
Owen
2026-05-21 18:23:11 -07:00
parent e0d65d8125
commit 133311f1c4
2 changed files with 15 additions and 5 deletions

View File

@@ -34,7 +34,7 @@ func serveNativeSSHSession(ctx context.Context, ws *websocket.Conn, cfg NativeSS
log.Printf("SSH native: spawning shell")
sess, err := nativessh.NewPTYSession(cfg.Shell)
sess, err := nativessh.NewPTYSession()
if err != nil {
sendSSHError(ctx, ws, fmt.Sprintf("Failed to spawn shell: %v", err))
return fmt.Errorf("pty session: %w", err)

View File

@@ -15,11 +15,21 @@ type PTYSession struct {
cmd *exec.Cmd
}
// NewPTYSession spawns shell in a PTY. If shell is empty, /bin/sh is used.
func NewPTYSession(shell string) (*PTYSession, error) {
if shell == "" {
shell = "/bin/sh"
// findShell returns the path to the best available interactive shell by
// checking preferred shells in order, falling back to /bin/sh.
func findShell() string {
preferred := []string{"zsh", "bash", "fish", "ksh", "sh"}
for _, name := range preferred {
if path, err := exec.LookPath(name); err == nil {
return path
}
}
return "/bin/sh"
}
// NewPTYSession spawns the best available shell in a PTY.
func NewPTYSession() (*PTYSession, error) {
shell := findShell()
cmd := exec.Command(shell)
cmd.Env = append(os.Environ(), "TERM=xterm-256color")
ptmx, err := pty.Start(cmd)