Subhadip Banerjee
Subhadip Banerjee

Reputation: 47

run local script with flags to remote machine using ssh

I need to achieve similar functionality in a go program as below.

ssh user@host "python" - < ./test.py -f bar

I'm able to achieve the highlighted part now I just need to pass flags with the script file.

below is the code for highlighted part.

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()
}

Upvotes: 0

Views: 259

Answers (1)

shmsr
shmsr

Reputation: 4224

As you're using redirection, you could let bash handle it:

bash -c "<command>"

Pass this to session.Run(...)

Upvotes: 1

Related Questions