dongzong3053 2016-11-23 00:41
浏览 92

如何将终端命令实时输出定向到在用golang编写的localhost上运行的网页?

I relatively new to golang, and I am currently running into an issue of redirecting my terminal output to my webpage running on localhost. In golang, I have written a webserver running on localhost with a html button on it. When clicked, the button will execute the following terminal command:

sudo nmap -F -sS X.X.X.0/24 --stats-every 2s | grep --line-buffered 'Ping Scan Timing' | awk '{print $5}'

This outputs the execution time of the command every 2s to my os.stdout terminal. The output looks something like this:

10%
15%
30%
.
.
.

But now I have modified my code because I am trying to redirect this output to my webpage, and render it in realtime, so that it I can see how much progress is left for the execution to be completed since this command can take a while to load.

The problem I am running into is that I cannot properly use the stdoutpipe function to redirect the output to my webpage. Here is a snippet of my code for the handler that gets triggered when I click the button in my webpage:

func viewHandler(w http.ResponseWriter, r *http.Request) {
    conn, ok := w.(http.Flusher)

    if !ok {
            http.Error(w, "Oops", http.StatusInternalServerError)
            return
    }

    w.Header().Set("Content-Type", "text/event-stream")
    w.WriteHeader(http.StatusOK)


    nmap_chain := "nmap -F -sS X.X.X.0/24 -oX output.xml --stats-every 2s | grep --line-buffered 'Ping Scan Timing' | awk '{print $5}'"
    cmd := exec.Command("bash","-c",nmap_chain)
    stdout, err := cmd.StdoutPipe()
    if err != nil {
            log.Fatal(err)
    }
    if err := cmd.Start(); err != nil {
            log.Fatal(err)
    }

    in := bufio.NewScanner(stdout)

    for in.Scan() {
            fmt.Fprintf(w,"%d

",in) 
            conn.Flush()
            time.Sleep(time.Second)
    }

    if err := in.Err(); err != nil {
            log.Printf("error: %s", err)
    }


}

My html file looks like the following as well:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>NMAP Demo</title>
    <link rel="stylesheet" type="text/css" href="layout/mystyle.css">
    <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        $("#exec").on("click", function() {
        $('#exec').prop('disabled', true);
                $.ajax({
            url: "/exec",
            method: "GET",
            success: function(data) {
            $("#response").html(data);
            $('#exec').prop('disabled', false);
            },
            });
        });
    });
    </script>
</head>
<body>
    <div id="headerBar"></div>
    <div>
        <button id="exec">Execute Command</button>
    </div>
    <div id="results">
        <textarea readonly id="response" rows="50" cols="50" style="resize"></textarea>
    </div>
</body>
</html>

As of right now, I cannot see any output in my html textarea when this handler is executed. I've also tried creating a basic function that would execute the nmap command mentioned, and redirect the output to a log instead of os.stdout using stdoutpipe. But still I cannot see any output.

Any suggestions would be helpful.

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化