【问题标题】:How do I disable echo from Mac terminal programmatically using GoLang如何使用 GoLang 以编程方式禁用 Mac 终端的回显
【发布时间】:2018-05-24 16:42:40
【问题描述】:

我正在尝试使用 GoLang 从我的 Mac 终端禁用回显。我尝试使用

exec.Command("stty", "-F", "/dev/tty", "-echo").Run()

它适用于 linux 终端,但不适用于 Mac 和 Windows。在 Mac 中,在 bash 和 zsh 中,我手动尝试使用

stty -echo
stty -echoctl

两者都不起作用。

有人可以帮忙吗?

谢谢!

【问题讨论】:

  • stty 在 mac 和 windows 上是否按预期工作,是否直接在终端上发布?不使用 golang 吗?
  • @ScottStensland .. 不, stty -echo 不能直接在终端上工作。我们需要更改一些设置吗?

标签: macos go terminal echo


【解决方案1】:

虽然这不能直接回答您的示例为什么不起作用的问题,但这里是如何从终端读取秘密而不回显它(这里是签名和文档的链接 - link) :

package main

import (
    "fmt"
    "syscall"

    "golang.org/x/crypto/ssh/terminal"
)

func main() {
    fmt.Println("Your password: ")
    bytepw, err := terminal.ReadPassword(int(syscall.Stdin))
    if err != nil {
        panic(err)
    }
    fmt.Println()
    fmt.Printf("Your password was '%s'\n", string(bytepw))
}

预期输出:

mac:~ jabbson$ go run testpass.go
Your password:

Your password was 'mypass'
mac:~ jabbson$

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-30
    • 2014-06-22
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多