英文:
Golang exec .bat or .exe file but don't get output
问题
我想使用Golang执行.exe文件,并通过Web Socket发送输出值。
在Linux中,当我运行一个bash文件时,它工作得很好,但在Windows中,无法使用.exe或.bat文件(.bat文件使用一些参数运行.exe文件)。程序已经执行,但我从未看到和接收到脚本的输出。我的问题只是如何在Windows中获取脚本的输出。如果你能帮助我,我将非常感激。
谢谢
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)
}
}
谢谢你的帮助,我尝试了这个方法,但对我来说,脚本已经执行了,但我没有输出结果 :/
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/cyril.connan@gmail.com", "--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)
}
英文:
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/cyril.connan@gmail.com", "--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)
}
答案1
得分: 1
我创建了一个简单的hello world程序hello.exe
,它只是将"Hello World!"打印到stdout。然后下面的程序调用hello.exe
并打印"Hello World!"。
也许你的管道代码或扫描器有错误,请先尝试一个简单的示例,看看它在哪里失败。
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))
}
英文:
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))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论