Go: Emulate ssh remote execution of local script with arguments in golang

I’m trying to execute anytype of local script(sh/bash/python/ruby) or executable with arguments to a remote machine using go.
ssh command: ssh user@10.10.10.10 "python3" - < ./test.py "arg1"

package main
import (
    "log"
    "os"

    "golang.org/x/crypto/ssh"
)

func main()  {
    user := "user"
    hostport := "10.10.10.10:22"
    script, _ := os.OpenFile("test.py", os.O_RDWR|os.O_CREATE, 0755)
    interpreter := "python3"
    client, session, err := connectToHost(user, hostport) 
    session.Stdin = script
    session.Stdout = os.Stdout
    err = session.Run(interpreter)
    if err != nil {
        log.Fatal(err)
    }
    client.Close()
    defer session.Close()
}

The example ssh command is working fine and I’m able to execute the local test.py in the remote server too, but I can’t think of any efficient way which will help me to pass arguments(ex. arg1) as well.
Thanks for the help.

If I understood it right then https://golang.org/pkg/flag/ is your best call. There might be more advanced ones (easier/full-featured) out there, but I found Go’s approach extremely simple to use (unless you have an extremely complicated args relation).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.