wish icon indicating copy to clipboard operation
wish copied to clipboard

wish.Command() : bash script is displayed in the server console, not the client console.

Open Buanderie opened this issue 6 months ago • 2 comments

Describe the bug Bash script using "dialog" is displayed on server side (on the console that launched my wish app)

Setup Please complete the following information along with version numbers, if applicable.

  • OS: WSL2
  • Shell: bash

To Reproduce Use my app like so: ./myapp -i script.sh

This bash script works:

#!/bin/bash

echo "Hello !"
while(true)
do
    echo -n "."
    sleep 1
done

While this one does not (displayed on the server side, instead...)

#!/bin/bash

SCRIPT_DIR="$( dirname "${BASH_SOURCE[0]}" )"

# Function to display the main menu
show_menu() {
    whiptail --clear --backtitle "" --title "Main Menu" \
    --menu "Choose a task:" 15 50 4 \
    1 "Go to command line"
}

show_menu

Source Code

package main

import (
	"flag"
	"strconv"
	"log"
	// "os/exec"
	"github.com/charmbracelet/ssh"
	"github.com/charmbracelet/wish"
	"github.com/charmbracelet/wish/logging"
	"github.com/charmbracelet/wish/activeterm"
)

func main() {

	// Define command-line flags
	serverPort := flag.Int("p", 2222, "SSH Server port")
	scriptPath := flag.String("i", "", "Input script to be executed")
	flag.Parse()

	// Create the SSH server with a custom server configuration
	portStr := strconv.Itoa(*serverPort)
	s, err := wish.NewServer(
		wish.WithAddress("0.0.0.0:"+portStr),
		ssh.AllocatePty(),
		wish.WithMiddleware(
			logging.Middleware(),
			activeterm.Middleware(),
		),
		wish.WithPasswordAuth(func(ctx ssh.Context, password string) bool {
			// Implement your password authentication logic here
			// Example: only allow a specific username and password
			return ctx.User() == "root" && password == "root"
		}),

		wish.WithMiddleware(
			func(next ssh.Handler) ssh.Handler {
				return func(sess ssh.Session) {
					pty, _, _ := sess.Pty()

					wish.Printf(sess, "Term: %s\r\n", pty.Term)
					wish.Printf(sess, "PTY: %s\r\n", pty.Slave.Name())
					wish.Printf(sess, "FD: %d\r\n", pty.Slave.Fd())

					// wish.Println(sess, "Hello, world!")

					cmd := wish.Command( sess, "/bin/bash", *scriptPath)

					// cmd := exec.Command("/bin/bash", *scriptPath)
					// cmd.Stdout = sess
					// cmd.Stderr = sess
					// cmd.Stdin = pty.Slave

					cmd.Run()

					next(sess)
				}
			},

			// The last item in the chain is the first to be called.
			logging.Middleware(),
		),
	)
	if err != nil {
		log.Fatalln(err)
	}

	// Start the SSH server
	// log.Println("Starting SSH server on port 2222...")
	log.Printf( "Serving script %s on SSH server [port=%d]\n", *scriptPath, *serverPort)
	log.Fatal(s.ListenAndServe())

}

Expected behavior The script is displayed on the console of the client

Buanderie avatar Aug 06 '24 14:08 Buanderie