Cyril Connan
Cyril Connan

Reputation: 31

Golang exec .bat or .exe file but don't get output

I want to exec .exe file with golang and send the output value in web socket.

It's work great in linux, when I run an bash file but not working in windows with .exe or .bat file (.bat file run .exe file with some parameters). The program was executed but i never see and recive the output of script. My problem is just how to get the output of script in windows. If you can help me it's would be apreciated.

Thanks :)

cmd := exec.Command("cmd", "/C", "file.exe or file.bat", "parameters")
stdout, err := cmd.StdoutPipe()
if err != nil {
    panic(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
    panic(err)
}
err = cmd.Start()
if err != nil {
    panic(err)
}

scannerOut := bufio.NewScanner(stdout)
for scannerOut.Scan() {

    log.Printf("%s", scannerOut.Text())

    err = socket.Emit("test", map[string]interface{}{"user": user, "name": Name, "data": scannerOut.Text()})
    if err != nil {
        log.Println(err)
    }
}

thanks for your help i tried that but again for me the script was executed but i don't have the output :/

output, err := exec.Command("cmd", "/C", cmdPath).CombinedOutput()
//cmd := exec.Command("ping", "google.fr", "-t")
//cmd := exec.Command("cmd", "/C", "C:/Users/Cyril/Desktop/zec_miner/miner", "--server", "zec-eu1.nanopool.org", "--user", "t1cjM242nws6QjfGWApeASG9DNDpsrHWv8A.ccl3ouf/[email protected]", "--pass", "z", "--port", "6666")
/*stdout, err := cmd.StdoutPipe()*/
if err != nil {
    panic(err)
}

log.Printf("%s", string(output))

err = socket.Emit("test", map[string]interface{}{"user": user, "rig_name": rigName, "data": output})
if err != nil {
    log.Println(err)
}

Upvotes: 0

Views: 1501

Answers (1)

gonutz
gonutz

Reputation: 5582

I have created a little hello world program hello.exe that just prints "Hello World!" to stdout. Then the following program calls hello.exe and prints "Hello World!" as well.

Maybe there is an error in your piping code or the scanner stuff, try a simple example first and see where it fails.

package main

import (
    "fmt"
    "os/exec"
)

func main() {
    output, err := exec.Command("cmd", "/C", "hello.exe").CombinedOutput()
    if err != nil {
        panic(err)
    }
    fmt.Println(string(output))
}

Upvotes: 1

Related Questions