需要在windows下执行这个命令,
netstat -an |find /c "8888"
这样写不行的,还有办法吗?谢谢
if c, err = exec.Command("cmd", "/c", "netstat -an", "|", "find", "/c", "8888").CombinedOutput(); err != nil {
log.Fatal(err)
}
需要在windows下执行这个命令,
netstat -an |find /c "8888"
这样写不行的,还有办法吗?谢谢
if c, err = exec.Command("cmd", "/c", "netstat -an", "|", "find", "/c", "8888").CombinedOutput(); err != nil {
log.Fatal(err)
}
2 回答1.3k 阅读
2 回答1.1k 阅读
2 回答1.1k 阅读
2 回答868 阅读
3 回答684 阅读
1k 阅读
1 回答788 阅读
管道的本质是将程序的输出作为next的输入,程序前三个fd是stdin、stdout、stderr三个文件输入输出。
os/exec.Cmd定义里面就有这三个结构。
那么我们就将netstat的stdout转换成find的stdin即可。
后续go代码随便写的,未测试,不保证可靠。
但是os/exec库封装了管道了,可以直接拿到三个fd的管道,具体见godoc。