2

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.

Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
vrtx54234
  • 2,196
  • 3
  • 30
  • 53
  • 1
    In C I'd use `getpass()`. I don't know if that helps, but [getpass golang](http://www.google.com/search?q=getpass+golang) gets a few nice-looking google hits. –  Jan 03 '14 at 13:11
  • Possible duplicate of [getpasswd functionality in Go?](https://stackoverflow.com/questions/2137357/getpasswd-functionality-in-go) – Duncan Jones Aug 02 '18 at 08:19

2 Answers2

4

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 \n.

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

More on it:

Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
-2

You may use expect command for password interactively :

expect "Enter Password:" {

send -- "$PASSWORD_VALUE\r"

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\r"

#
Mohit M
  • 769
  • 7
  • 15
  • 1
    That is completely wrong! I don't make -1, but please consider, that your answer has nothing to do with the question – Igor Chubin Jan 03 '14 at 13:49
  • Sincerely Apologies as not seen you are expected answer in golang. I had given answer as though process of linux commands. hope you get the answer. thanks – Mohit M Jan 04 '14 at 10:54