I am writing a Golang application which will run some bash scripts. In one of the commands the script will run as sudo and it would require me to enter a password manually.
May I know how to achieve sending of password automatically?
cmd := exec.Command("/bin/sh", "-c", "knife bootstrap xxx.xxx.xxx.xxx -x user_name --sudo -i ~/.ssh/id_rsa.pub --node-name node_name -P password -c ~/chef-repo/.chef/config.rb")
stdin, err := cmd.StdinPipe()
if err != nil {
log.Panic(err)
}
go func() {
defer stdin.Close()
io.WriteString(stdin, "password")
}()
I am currently stuck at the step when knife bootstrap will prompt the following for a password over at the chef client side.
user_name@xxx.xxx.xxx.xxx's password:
I couldn't use expect to capture this string and provide the password, it seems to be different from the usual strings that will output in the terminal.
I think you need use sudo -S
.
echo password | sudo -S vim a.txt