Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected Input Echo in Output When Using pty #204

Open
cuijixiong1 opened this issue Dec 16, 2024 · 1 comment
Open

Unexpected Input Echo in Output When Using pty #204

cuijixiong1 opened this issue Dec 16, 2024 · 1 comment

Comments

@cuijixiong1
Copy link

I encountered an issue when using the github.com/creack/pty library to create a pseudo-terminal. Below is the relevant code snippet:

cmd := exec.Command("/system/bin/sh")

// Allocate a pseudo-terminal for the subprocess
ptyMaster, err := pty.Start(cmd)
if err != nil {
return ErrPkg.NewErrD("Failed to start shell: " + err.Error())
}

When I send a command (e.g., ls followed by Enter) to the subprocess through the pseudo-terminal and then read the output, the output includes not only the command's execution result but also the command input itself. For example:

Input: ls
Output: ls\n + the actual command output

This behavior results in the output containing the input content, which is unexpected. I expect to receive only the command's execution result without the input echoed back.

Could you clarify if this behavior is intentional or if there's a way to avoid this echo in the output?

@creack
Copy link
Owner

creack commented Dec 16, 2024

This is indeed expected and has to do with the terminal, no directly the pty library.
If you take a look at the README, there is an example of a shell, the key part you are looking for is

        // Set stdin in raw mode.
        oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
        if err != nil {
                panic(err)
        }
        defer func() { _ = term.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort.

That will pass through the terminal management to the underlying process.

If you only want to disable the output showing the input you can just disable ECHO and/or ICANON termios

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants