0

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.

1 Answers1

0

Why not just pass the flags to the python file? By using the less-than symbol, you're literally inputting the output of test.py into the Go executable.

ssh user@10.10.10.10 "python3" - < ./test.py <flag1> "arg1" <flag2> "arg2"

Unless I'm fundamentally misunderstanding what you're trying to do?

  • See the ssh command which is executing python interpreter in the remote machine and redirecting a local script file to it while passing arguments is working fine. But I'm trying achive same redirection and execution of commands through go. Even if you check the code everything is working except the last part of the ssh command where I need to pass flags/arguments to the local script which will be redirected to the remote machine. I need solution for that part. – Subhadip Banerjee May 17 '20 at 23:15
  • go provides a handy flag library for being able to pass command line args, you could take those in and then parse them, creating the command on the fly https://golang.org/pkg/flag/ – irate_swami May 18 '20 at 21:01
  • 1
    Indeed and I'm planning to use those to get required input from my machine end, but how to pass those to the local script file I'm passing to remote machine which is supposed to be run on the remote machine. – Subhadip Banerjee May 20 '20 at 15:11