如何使用读取系统调用以交互方式获取密码

how do I implement the following command line using system calls in golang?

read -s -p "Enter Password: " mypassword

that is, what additional options to set while reading the password to avoid the input to be echoed and force the input to be provided only interactively.

thanks.

You can use terminal.ReadPassword() from package terminal.

func ReadPassword(fd int) ([]byte, error)

ReadPassword reads a line of input from a terminal without local echo. This is commonly used for inputting passwords and other sensitive data. The slice returned does not include the .

Package terminal provides support functions for dealing with terminals, as commonly found on UNIX systems.

More on it:

You may use expect command for password interactively :

expect "Enter Password:" {

send -- "$PASSWORD_VALUE"

e.g - while using sftp use below script

sh expect.sh

PASSWORD_VALUE="password"

spawn -noecho sftp username@ip

expect "Enter Password:" {

send -- "$PASSWORD_VALUE"

#