0

I am trying to automate a process in Go. I have been able to implement threads and do the process accordingly however the output is mixed and matched.

I was wondering if there is a way to show the output as it is produced by the program and according to the program's process. So if task A completes before task B, we show A's output before B, or vice-versa.

package main

import (
    "fmt"
    "log"
    "os"
    "os/exec"
    "sync"
)

var url string
var wg sync.WaitGroup

func nikto() {
    cmd := exec.Command("nikto", "-h", url)
    cmd.Stdout = os.Stdout
    err := cmd.Run()
    if err != nil {
        log.Fatal(err)
    }
    wg.Done()
}

func whois() {

    cmd := exec.Command("whois", "google.co")
    cmd.Stdout = os.Stdout
    err := cmd.Run()
    if err != nil {
        log.Fatal(err)
    }
    wg.Done()
}
func main() {
    fmt.Printf("Please input URL")
    fmt.Scanln(&url)
    wg.Add(1)
    go nikto()
    wg.Add(1)
    go whois()
    wg.Wait()
}
Cosmic Ossifrage
  • 4,977
  • 29
  • 30

1 Answers1

0

In your process, you pass the os.Stdout file descriptor directly to the commands you invoke to run your child processes. This means the STDOUT pipe of the child processes will be connected directly to your Go program's standard output, and will likely be interleaved if both child processes write simultaneously.


The simplest way to fix this requires you to buffer the output from the STDOUT pipe of the child process in your Go program, so you can intercept the output and control when it is printed.

The Cmd type in the os/exec package provides a function call Output() which will invoke the child process and return the contents of STDOUT in a byte slice. Your code can be adapted with ease to implement this pattern and process the results, for example:

func whois() {
    cmd := exec.Command("whois", "google.co")
    out, err := cmd.Output()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(out)
    wg.Done()
}

Interleaving of output

If you use functions in the fmt package to print output, there is no guarantee that concurrent calls to fmt.Println will not be interleaved.

To prevent interleaving, you may choose to serialize access to STDOUT, or use a logger which is safe for concurrent use (such as the log package). Here is an example of serializing access to STDOUT in the Go process:

package main

import (
    "fmt"
    "log"
    "os/exec"
    "sync"
)

var url string

func nikto(outChan chan<- []byte) {
    cmd := exec.Command("nikto", "-h", url)
    bs, err := cmd.Output()
    if err != nil {
        log.Fatal(err)
    }
    outChan <- bs
}

func whois(outChan chan<- []byte) {
    cmd := exec.Command("whois", "google.com")
    bs, err := cmd.Output()
    if err != nil {
        log.Fatal(err)
    }
    outChan <- bs
}

func main() {
    outChan := make(chan []byte)

    fmt.Printf("Please input URL")
    fmt.Scanln(&url)
    go nikto(outChan)
    go whois(outChan)

    for i := 0; i < 2; i++ {
        bs := <-outChan
        fmt.Println(string(bs))
    }
}
Peter
  • 29,454
  • 5
  • 48
  • 60
Cosmic Ossifrage
  • 4,977
  • 29
  • 30