duanlanqian9974 2017-10-13 05:09
浏览 238
已采纳

尝试将Shell命令的实时/实时输出作为字符串数据从函数返回到主函数

not sure how to explain this since I am using QT bindings in GO so I pasted a mini version of the program. I am trying to return from the run() func a live stream to the QT window.. I try this many ways... last one with channels(not succesfully) what will be the best way to pass real time output to the main() function so my QT Slot can update the window?

package main

import (
        "fmt"
        //      "github.com/therecipe/qt/core"
        "bufio"
        "github.com/therecipe/qt/widgets"
        "os"
        "os/exec"
)

func run(input string) string {

        stream := make(chan string)

        fmt.Printf("You Clicked The Push Button %s
", input)

        cmdName := "/usr/bin/nikto"
        cmdArgs := []string{"-host", input}

        cmd := exec.Command(cmdName, cmdArgs...)
        cmdReader, err := cmd.StdoutPipe()
        if err != nil {
                fmt.Fprintln(os.Stderr, "Error creating StdoutPipe for Cmd", err)
                os.Exit(1)
        }

        scanner := bufio.NewScanner(cmdReader)
        go func() {
                for scanner.Scan() {
                        fmt.Printf("%s
", scanner.Text())
                        stream <- scanner.Text()
                        //stream = fmt.Sprintf("%s
", scanner.Text())
                }
        }()

        err = cmd.Start()
        if err != nil {
                fmt.Fprintln(os.Stderr, "Error starting Cmd", err)
                os.Exit(1)
        }

        err = cmd.Wait()
        if err != nil {
                fmt.Fprintln(os.Stderr, "Error waiting for Cmd", err)
                os.Exit(1)
        }

        input = <-stream
        return string(input)
        //return go getOutput(scanner)

}

func main() {
        // Create application
        app := widgets.NewQApplication(len(os.Args), os.Args)

        // Create main window
        window := widgets.NewQMainWindow(nil, 0)
        window.SetWindowTitle("nikto front end")
        window.SetMinimumSize2(400, 400)

        // Create layout

        Layout := widgets.NewQVBoxLayout()

        TopLayout := widgets.NewQHBoxLayout()
        //topright := widgets.NewQHBoxLauout()

        RLayout := widgets.NewQVBoxLayout()
        LLayout := widgets.NewQVBoxLayout()

        Layout.AddLayout(TopLayout, 0)
        Layout.AddLayout(RLayout, 1)
        Layout.AddLayout(LLayout, 0)

        //Create main widget
        mainWidget := widgets.NewQWidget(nil, 0)
        mainWidget.SetLayout(Layout)

        // Create left widget
        LQWidget := widgets.NewQWidget(nil, 0)
        LQWidget.SetLayout(LLayout)

        // Create right widget

        RQWidget := widgets.NewQWidget(nil, 0)
        RQWidget.SetLayout(RLayout)

        // Create label
        urlLabel := widgets.NewQLabel(nil, 0)
        urlLabel.SetText("Target: ")
        TopLayout.AddWidget(urlLabel, 0, 0)

        // Create a line edit
        input := widgets.NewQLineEdit(nil)
        input.SetPlaceholderText("Enter target like http://127.0.0.1")
        TopLayout.AddWidget(input, 0, 0)

        // Create checkboxes
        checkBox1 := widgets.NewQCheckBox2("Default", nil)
        //checkBox1.SetWindowTitle("Check Box")
        LLayout.AddWidget(checkBox1, 0, 0)

        checkBox2 := widgets.NewQCheckBox2("SSL mode", nil)
        //checkBox2.SetWindowTitle("Check Box")
        LLayout.AddWidget(checkBox2, 0, 0)

        checkBox3 := widgets.NewQCheckBox2("no 404", nil)
        //checkBox3.SetWindowTitle("Check Box")
        LLayout.AddWidget(checkBox3, 0, 0)

        output := widgets.NewQTextEdit(nil)
        LLayout.AddWidget(output, 0, 0)

        // Create a button and add it to the layout
        button1 := widgets.NewQPushButton2("2. click me", nil)
        Layout.AddWidget(button1, 0, 0)

        button1.ConnectClicked(func(checked bool) {
                output.Append(run(input.Text()))
        })

        // Set main widget as the central widget of the window
        window.SetCentralWidget(mainWidget)

        // Show the window
        window.Show()

        // Execute app
        app.Exec()
}
  • 写回答

2条回答 默认 最新

  • doudengshen5591 2017-10-13 09:18
    关注

    Let run return the channel, then read it in main (and don't read from the channel in run):

    package main
    
    import (
        "bufio"
        "fmt"
        "os/exec"
    )
    
    func main() {
        // ...
    
        button1.ConnectClicked(func(checked bool) {
                stream := run(input.Text())
                go func() {
                        for line := range stream {
                                output.Append(line)
                        }
                }()
        })
    
        // ...
    }
    
    func run(host string) <-chan string {
    
        var cmd *exec.Cmd
        cmdReader, err := cmd.StdoutPipe()
        if err != nil {
                // ...
        }
    
        scanner := bufio.NewScanner(cmdReader)
        stream := make(chan string)
    
        go func() {
                defer close(stream)
    
                for scanner.Scan() {
                        fmt.Printf("%s
    ", scanner.Text())
                        stream <- scanner.Text()
                }
        }()
    
        go func() {
                if err := cmd.Run(); err != nil {
                        // ...
                }
        }()
    
        return stream
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。