Skip to content

os/exec: StdinPipe on Windows is not successful #19452

@liwux

Description

@liwux

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

Go 1.8

My code:

package main
import (

	"os"
	"os/exec"
	"io"
)
func main() {
	command :="openconnect"
	param := []string{"https://192.168.0.100:1443","-u","test","--passwd-on-stdin"}
	cmd :=exec.Command(command,param...)
	cmd.Stdout = os.Stdout
	input, _ := cmd.StdinPipe()
	cmd.Start()
	io.WriteString(input,"123\n")
	cmd.Wait()
}

can not automatically standard input when the command starts.

I changed a kind of writing

the code

package main

import (
	"fmt"
	"io"
	"log"
	"os/exec"
	"os"
)

func main() {
	command :="openconnect"
	param := []string{"https://192.168.0.100:1443","-u","test","--passwd-on-stdin"}
	cmd :=exec.Command(command,param...)
	stdin, err := cmd.StdinPipe()
	if err != nil {
		log.Fatal(err)
	}
	go func() {
		defer stdin.Close()
		io.WriteString(stdin, "123\n")
	}()
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	if err = cmd.Start(); err != nil { //Use start, not run
		fmt.Println("An error occured: ", err) //replace with logger, or anything you want
	}
	cmd.Wait()

}

The two are not successful .
when I input "123" by Keyboard ,is ok.
who can help me,thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions