-2
echo "test_metric:20|c" | nc -v -C -w 1 host.address port

I run this command through terminal, I get the desired result. But how can I do the same via Go code?

I tried this, after going through an answer here-

sh:= os.Getenv("SHELL")
cmd := exec.Command(sh, "-c ", `echo "test_metric:20|c" | nc -v -C -w 1 host.address port`)

cmd.Stdout = os.Stdout

cmd.Run()

But no luck.

Samik
  • 29
  • 2
  • 12
  • You have a trailing space after the `-c` argument. If fixing this does not solve your problem add the Stderr output to the question. – Peter Jul 21 '20 at 12:38
  • @Samik : Your approach would fail if the environment variable `SHELL` is not defined, or if it is set to a some shell which does not understand the command syntax or `-c` option. If you write this for POSIX shell, just use a literal `sh` and let it be the responsibility of the caller to ensure that `sh` is in the PATH. – user1934428 Jul 21 '20 at 13:50

1 Answers1

0

I do not see the point in calling the shell to do this:

package main

import (
    "bytes"
    "flag"
    "fmt"
    "io"
    "log"
    "net"
    "time"
)

var (
    host    string
    port    int
    timeout string
)

func init() {
    flag.StringVar(&host, "host", "localhost", "host to connect to")
    flag.IntVar(&port, "port", 10000, "port to connect to")
    flag.StringVar(&timeout, "timeout", "1s", "timeout for connection")
}

func main() {
    flag.Parse()

    // Fail early on nonsensical input.
    if port < 1 || port > 65535 {
        log.Fatalf("Illegal port %d: must be >=1 and <=65535", port)
    }

    var (
        // The timeout for the connection including name resolution
        to time.Duration

        // The ubiquitous err
        err error

        // The dial string
        addr = fmt.Sprintf("%s:%d", host, port)

        // The actual connection
        con net.Conn

        // Your playload. It should be easy enough to make this
        // non-static.
        payload = []byte("test_metric:20|c")
    )

    // Check the user has given a proper timeout.
    if to, err = time.ParseDuration(timeout); err != nil {
        log.Fatalf("parsing timeout: %s", err)
    }

    // You might want to implement a retry strategy here.
    // See https://stackoverflow.com/a/62909111/1296707 for details
    if con, err = net.DialTimeout("tcp", addr, to); err != nil {
        log.Fatalf("Error while dialing: %s", err)
    }
    defer con.Close()

    // This simulates about every input.
    // You can use a pipe of a command or whatever you want.
    dummyReader := bytes.NewBuffer(payload)

    if w, err := io.Copy(con, dummyReader); err != nil && w < int64(len(payload)) {
        log.Printf("Short write: written (%d) < payload (%d): %s", w, len(payload), err)
    } else if err != nil {
        // This should not happen, as usually an error is accompanied by a short write
        log.Println("Uuupsie!")
    }

}

Start a netcat listener on one shell:

$ nc -k -l 10000

run the code via

$ go run dacode.go

and you should see your payload on the output of the netcat listener.

In case you want to transfer the output of a program to the remote server, simply call the according command via os.Exec and use io.Copy on con and the io.Reader returned by your command's StdoutPipe().

Markus W Mahlberg
  • 19,711
  • 6
  • 65
  • 89