mirror of
https://github.com/fosrl/newt.git
synced 2026-08-31 19:21:28 +02:00
Pty to find its own shell
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user