Skip to content

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

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

Closed
liwux opened this issue Mar 8, 2017 · 9 comments
Closed

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

liwux opened this issue Mar 8, 2017 · 9 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@liwux
Copy link

liwux commented Mar 8, 2017

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.

@mvdan
Copy link
Member

mvdan commented Mar 8, 2017

Did you find a specific bug in the standard library? If so, please provide a shorter piece of code to reproduce it.

Otherwise, please take it to https://github.com/golang/go/wiki/Questions as that's where you should go for chunks of code that don't work as you expected. It's likely an issue with your code.

@ianlancetaylor ianlancetaylor changed the title When run CMD. StdinPipe () on windows is not successful os/exec: StdinPipe on Windows is not successful Mar 8, 2017
@ianlancetaylor
Copy link
Contributor

You say "are not successful" but you don't say what happens. What precisely happens? Do you get an error? Does the program misbehave in some way?

I don't understand your program: you call StdinPipe but you don't write anything to the pipe. What are you really trying to do?

@liwux
Copy link
Author

liwux commented Mar 9, 2017

@ianlancetaylor I don't get any error , just don't write anything to the pipe
What's the problem with this :

stdin, err := cmd.StdinPipe()
io.WriteString(stdin, "123\n")

@ianlancetaylor
Copy link
Contributor

I apologize, I did miss the fact that you do write to the pipe.

I tried running this variant of your program on GNU/Linux, and it printed 123. I do not know why your program is not working. I don't know what the openconnect program is.

package main

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

func main() {
        command :="cat"
        param := []string{}
        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()

}

@ianlancetaylor
Copy link
Contributor

Two things you should certainly do are check the error return of io.WriteString and cmd.Wait.

@liwux
Copy link
Author

liwux commented Mar 9, 2017

@ianlancetaylor Run on GNU/Linux and OSX and it printed 123 ,but run on windows it printed nothing

@liwux
Copy link
Author

liwux commented Mar 9, 2017

@ianlancetaylor thanks...
it may be the openconnect program's problem

@jD91mZM2
Copy link

I'm an idiot, so don't trust anything I say.
But maybe... just maybe it's because you need \r\n on Windows?

@bradfitz bradfitz added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 21, 2017
@bradfitz
Copy link
Contributor

I'm going to close this for now until there's evidence pointing at it being Go's problem.

Feel free to reopen if you have more info.

@golang golang locked and limited conversation to collaborators Mar 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

6 participants