Answer to password shell prompt in golang

Viewed 5041

I am searching for a way to answer to a shell password prompt in golang.

like :

bussiere@kus:~/Workspace/rteest$ ./passwordtest.sh
Password : 

I would like to enter the password automatically with my token in golang after launching a shell command / script ...

I've made some script that get a one time token with mfa if everything is ok (in golang). So i need to enter the tempory token to a linux password prompt.

I know that there is the expect command but i would like to compile my program to embed it and have minimal depency.

Thanks and regards

edit thks to @nevermore i've tried this (but it doesn't work) : https://play.golang.org/p/Ffm3q5h636

package main

import (
    "os/exec"
    "fmt"
    "log"
    "io"
)

func main() {
    cmdb := "git"
    args := "clone https://bb@gitlab.com/bb/fzgs.git"
cmd := exec.Command(cmdb, args)
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}

go func() {
defer stdin.Close()
io.WriteString(stdin, "QSRDFGHJfZERTYU")
}()

out, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}

fmt.Printf("%s\n", out)

}

it gives me this :

2017/05/12 20:42:36 exit status 1
exit status 1

edit bis :

    cmdb := "git"
    args := "https://bb@gitlab.com/bb/fzgs.git"
cmd := exec.Command(cmdb, "clone", args)

it asks for the password :

Password for 'https://bb@gitlab.com':

2 Answers
Related