Subhadip Banerjee
Subhadip Banerjee

Reputation: 47

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 [email protected] "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.

Upvotes: 0

Views: 628

Answers (1)

irate_swami
irate_swami

Reputation: 71

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 [email protected] "python3" - < ./test.py <flag1> "arg1" <flag2> "arg2"

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

Upvotes: 0

Related Questions