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

Getting powershell.exe and cmd.exe works on ssh session #118

Open
SakiiR opened this issue Jul 31, 2019 · 1 comment
Open

Getting powershell.exe and cmd.exe works on ssh session #118

SakiiR opened this issue Jul 31, 2019 · 1 comment

Comments

@SakiiR
Copy link

SakiiR commented Jul 31, 2019

The following code does not work either with powershell.exe and cmd.exe.

I can't type anything when the session starts ..

func GiveShell(s ssh.Session) {
	_, _, isPty := s.Pty()
	if isPty {
		logger.Get().Info("PTY requested")

		cmd := exec.Command("cmd")
		stdin, err := cmd.StdinPipe()
		if err != nil {
			logger.Get().Warn("Error occured: %s")
			return
		}
		stdout, err := cmd.StdoutPipe()
		if err != nil {
			logger.Get().Warn("Error occured: %s", err)
			return
		}
		stderr, err := cmd.StderrPipe()
		if err != nil {
			logger.Get().Warn("Error occured: %s", err)
			return
		}

		go func() {
			io.Copy(stdin, s)
		}()
		go func() {
			io.Copy(s, stdout)
		}()
		go func() {
			io.Copy(s, stderr)
		}()

		err = cmd.Run()
		if err == nil {
			logger.Get().Info("Session ended normally")
			s.Exit(0)
		} else {
			logger.Get().Info("Session ended with an error: %v\n", err)

			exitCode := 1
			if exitError, ok := err.(*exec.ExitError); ok {
				exitCode = exitError.ExitCode()
				logger.Get().Info("exit code: %d\n", exitCode)
			}

			s.Exit(exitCode)
		}
	} else {
		msg := "No PTY requested."
		io.WriteString(s, msg)
		logger.Get().Warn(msg)
	}
}
@abakum
Copy link

abakum commented Nov 14, 2023

#214

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