dougu4704 2014-10-27 19:28
浏览 841

在命令行上使用Go,如何隐藏用户终端上的命令行输入?

I'm writing a simple command line utility that requests a password from the user. I settled on using bufio for reading user input and can't figure out how to hide the password on the user's terminal. Is this functionality in Go's standard library, or do I need to use OS-level functionality to accomplish this? (This is probably only going to run on Linux, but would prefer something that works on Windows also.)

  • 写回答

2条回答 默认 最新

  • duanshan3065 2014-10-27 20:45
    关注

    What you want is turning off echoing. This is usually achieved by configuring the terminal driver, termios. Your best bet is to read Linux' documentation of that driver (termios(3)). As far as I am concerned, you need to turn off ECHO before reading a password. After reading the password, turn on ECHO again. Do not use bufio as you do not want to have buffering interfere with low-level terminal stuff. You can access the requires system calls from the syscall package.

    termios is available on every UNIX-like platform. This includes Linux, BSD, OS X, and Solaris but not Microsoft Windows.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog