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

execute only first entered in therminal #192

Closed
mykolq opened this issue Apr 19, 2024 · 3 comments
Closed

execute only first entered in therminal #192

mykolq opened this issue Apr 19, 2024 · 3 comments

Comments

@mykolq
Copy link

mykolq commented Apr 19, 2024

//go:build linux
// +build linux

package main

import (
	"io"
	"os"
	"os/exec"
	"time"

	"github.com/creack/pty"
)

func main() {

	cliPath := "/usr/bin/ssh"
	sshOpenSession := exec.Command(cliPath, "user@address", "-c", "aes128-cbc", "-o", "StrictHostKeyChecking=no")

	ptm, err := pty.Start(sshOpenSession)
	if err != nil {
		panic(err)
	}
	defer func() { _ = ptm.Close() }()
	time.Sleep(5 * time.Second)
	go func() {
		ptm.Write([]byte("password\n"))
		//time.Sleep(2 * time.Second)
		ptm.Write([]byte("test\n"))
		ptm.Write([]byte("exit\n"))
	}()
	io.Copy(os.Stdout, ptm)
	ptm.Close()
}

i start this code and it seems that code only entered password, but no startig test and exit commands. am i use it wrong?

@creack
Copy link
Owner

creack commented May 23, 2024

This is completely dependent on what cliPath is and how it behaves.
The code looks correct, it would write a few things to the terminal.
Make sure you set yourline discipline / termcaps as the cli (ssh) expects.

If you are trying to actually type the password from the tty, I would recommend to consider using a ssh key instead.

Not sure how the ssh password inputs works, but trying your code with a ssh key works as expected.

That would fall more under the golang.org/x/term library or similar as it is not a pty issue.

@creack creack closed this as completed May 23, 2024
@theclapp
Copy link

@mykolq - I believe ssh reads the pw from the process's "controlling tty", not from stdin and not necessarily the current tty. I don't know if this library sets the "controlling tty" setting by default, or if it even can. (If it does or can I'd love to hear about it, for my own app.)

I think there are arguments you can give to ps to show a process's controlling tty, and strace could be useful to see exactly what file handle a process is reading from.

I know that's handwavy and vague but I hope it points you in the right direction.

@mykolq
Copy link
Author

mykolq commented May 23, 2024

@theclapp I have decided to use python for this task. Thanks for answer

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

3 participants