input password to command in golang


I need to use lxd-p2c( in golang code.

I try to pass password to the binary lxd-p2c built by the code above, which uses term.ReadPassword(0)( to read password in Linux.

I did some search on the internet and tried following code but they just did not work.

# 1
cmd := exec.Command(command, args...)
cmd.Stdin = strings.NewReader(password)

# 2
cmd := exec.Command(command, args...)
stdin, _ := cmd.StdinPipe()
io.WriteString(stdin, password)

Similar but simple code to test: (code from

build the binary and call it in go.


No workaround found and I removed term.ReadPassword(0) in the source code.


Checking the error in your playground displays inappropriate ioctl for device.

Searching for the error message I found this thread, which notes that non-terminal input is not supported for terminal.ReadPassword. My guess is that passing Stdin input this way makes it pass the input with a character device instead of with the necessary terminal device like tty or any such, making the read fail. lxd-p2c can’t read the password from such an input device.

Answered By – Fabian

Answer Checked By – Senaida (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.