Go 执行命令获取结果 golang os/exec StdoutPipe StdinPipe


本文摘自网络,作者,侵删。

go语言执行命令

go在执行Cmd/Shell命令时,跟一些语言是有区别的.
在不调用shell的情况下,需要自己对每个步骤手动写

我的目的是执行

netstat -aon | findstr :80   //Win
netstat -ano | grep :80  //Unix

因为中间加了管道| 所以很多资料都不能满足我的想法
所以自己手动找了很多资料实现了

过程

  • cmdNetstat
  • cmdGrep
  • netstatOutPutPip (输出管道)
  • grepInPutPip(输入管道)
  • cmdNetstat.Start
  • 得到输出管道中的数据
  • cmdGrep.Start
  • 将得到的数据写入另一个管道
  • 得到最后的结果数据

代码


func ShellCmdNetstat(port int) ([]byte, error) {
    cmdNetstat := exec.Command("netstat", "/a", "/n", "/o") // netstat -ano

    var cmdGrep *exec.Cmd
    if runtime.GOOS == "Windows/">windows" {
        cmdGrep = exec.Command("findstr", fmt.Sprintf(":%d ", port)) // win:    findstr ":80 "
    } else if runtime.GOOS == "linux" {
        cmdGrep = exec.Command("grep", fmt.Sprintf(":%d ", port)) // unix:  grep ":80"
    }

    netstatOutPutPip, _ := cmdNetstat.StdoutPipe() // netstat out pipe
    grepInPutPip, _ := cmdGrep.StdinPipe()         // grep in pipe

    if err := cmdNetstat.Start(); err != nil {
        return make([]byte, 0), err
    }

    data, _ := ioutil.ReadAll(netstatOutPutPip)

    if err := cmdNetstat.Wait(); err != nil {
        fmt.Println("cmdNetstat.Wait()", err)
        return make([]byte, 0), err
    }

    var grepOutBuffer bytes.Buffer
    cmdGrep.Stdout = &grepOutBuffer
    if err := cmdGrep.Start(); err != nil {
        fmt.Println(err)
        return make([]byte, 0), err
    }

    grepInPutPip.Write(data) // cmdNetstat out >> grepInPutPip in

    // 这行代码让你原地打转2天
    // The pipe will be closed automatically after Wait sees the command exit.
    // A caller need only call Close to force the pipe to close sooner.
    // For example, if the command being run will not exit until standard input
    // is closed, the caller must close the pipe.
    grepInPutPip.Close() //  This Is So Important!!!  命令在输入关闭后才会执行返回时需要显式关闭管道。

    if err := cmdGrep.Wait(); err != nil {
        return make([]byte, 0), err
    }
    return grepOutBuffer.Bytes(), nil
}





本文来自:简书

感谢作者:0xFFFFFFFE

查看原文:Go 执行命令获取结果 golang os/exec StdoutPipe StdinPipe

相关阅读 >>

cento8安装Golang及配置

Golang的堆栈怎么看

Golang可以开发android吗

Golang channel是什么

Go实现字符串的逆序

关于 Golang 字符串 格式化

zookeeper 的 Golang 客户端

Golang官方嵌入文件到可执行程序

Go语言适合做哪些开发

手撸Golang 基本数据结构与算法 图的搜索 深度优先/广度优先

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...