在Scan函数中静默用户输入。

huangapple go评论86阅读模式
英文:

Silence user input in Scan function

问题

如何在终端中隐藏用户输入(密码字段),类似于bash中的read -s p "password" password命令中的-s选项?

package main

import (
	"fmt"
	"golang.org/x/term"
)

func main() {
	fmt.Println("password: ")
	password, _ := term.ReadPassword(0)
	fmt.Println("You entered:", string(password))
}

你可以使用golang.org/x/term包中的ReadPassword函数来隐藏用户输入。

英文:

How do I hide user input (password field) in terminal similar to the -s command in read -s p "password " password in bash. ?

var password string
fmt.Println("password: ")
fmt.Scan(&password)

http://play.golang.org/p/xAPDDPjKb4

答案1

得分: 17

最好的方法是使用终端包中的ReadPassword()函数。或者你也可以查看这个问题,了解更多实现方式。

代码示例:

package main

import "golang.org/x/crypto/ssh/terminal"
import "fmt"

func main() {
 fmt.Println("输入密码:")
 password, err := terminal.ReadPassword(0)
 if err == nil {
    fmt.Println("输入的密码:" + string(password))
 }
}
英文:

The best way would be to use ReadPassword() from the terminal package. Else you can also look in this question for more ways to do so.

Code example:

package main

import "golang.org/x/crypto/ssh/terminal"
import "fmt"

func main() {
 fmt.Println("Enter password: ")
 password, err := terminal.ReadPassword(0)
 if err == nil {
    fmt.Println("Password typed: " + string(password))
 }
}

答案2

得分: 4

我对go语言不太了解,所以不确定你是否可以调用其他程序。如果可以的话,只需调用stty -echo来隐藏输入,然后调用stty echo来再次显示。

否则,你可以尝试以下代码:

fmt.Println("password: ")
fmt.Println("3[8m") // 隐藏输入
fmt.Scan(&password)
fmt.Println("3[28m") // 显示输入
英文:

I have no idea about go, so I'm not sure if you can call other programs. If so, just call stty -echo to hide input and stty echo to show it again.

Otherwise you could try the following:

fmt.Println("password: ")
fmt.Println("3[8m") // Hide input
fmt.Scan(&password)
fmt.Println("3[28m") // Show input

答案3

得分: 4

为了支持Windows,如这里所提到的,请使用以下代码:

import "syscall"
import "golang.org/x/crypto/ssh/terminal"

passwd, err := terminal.ReadPassword(int(syscall.Stdin))
英文:

To support windows, as mentioned here, please use:

import "syscall"
import "golang.org/x/crypto/ssh/terminal"

passwd, err := terminal.ReadPassword(int(syscall.Stdin))

答案4

得分: 0

我尝试了几种不同的方法,并发现了一些问题(例如,如果程序正在等待密码,Ctrl+C无法中断程序)。

这是我最终让它正常工作的方法-在OSX中完美运行。我还没有在其他环境中尝试过:

import (
	"log"
	"code.google.com/p/go.crypto/ssh/terminal"
	"github.com/seehuhn/password"
)

func StringPrompt() (password string, err error) {
	state, err := terminal.MakeRaw(0)
	if err != nil {
		log.Fatal(err)
	}
	defer terminal.Restore(0, state)
	term := terminal.NewTerminal(os.Stdout, "")
	password, err = term.ReadLine()
	if err != nil {
		log.Fatal(err)
	}
	return
}
英文:

I tried a couple of different methods for this and found issues (e.g. Ctrl+C not breaking out of the program if it's waiting for a password).

This is how I got it working in the end - this works perfectly in OSX. Haven't tried in other environments:

import (

	"log"
	"code.google.com/p/go.crypto/ssh/terminal"
	"github.com/seehuhn/password"

)

func StringPrompt() (password string, err error) {
	state, err := terminal.MakeRaw(0)
	if err != nil {
		log.Fatal(err)
	}
	defer terminal.Restore(0, state)
	term := terminal.NewTerminal(os.Stdout, "")
	password, err = term.ReadLine()
	if err != nil {
		log.Fatal(err)
	}
	return
}

huangapple
  • 本文由 发表于 2015年5月21日 10:57:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/30363790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定