【问题标题】:How do I get input from a socket without it being echoed?如何在不回显的情况下从套接字获取输入?
【发布时间】:2019-04-05 08:15:26
【问题描述】:

我正在用 Ruby 创建一个 TCP 服务器,需要从用户那里获取密码。此密码不应回显。不幸的是,当我 telnet 到 8080 端口并输入:My password is echoed.

@server_socket = TCPServer.open(8080, localhost)
conn = @server_socket.accept
conn.write "Password: "
conn.gets.chomp # <-- This should not be visible

当我输入 telnet localhost 8080 时,我可以在屏幕上看到我的密码。

【问题讨论】:

  • password = conn.noecho(&:gets).chomp ##这也不起作用
  • 您的意思是当您远程登录并输入您的密码时,它会在您输入时出现在您的控制台中?因为这是您的客户端的功能,而不是服务器的功能。
  • @anothermh:不可能?当用户远程登录到登录屏幕时;当他输入他的密码时,它不会显示出来。这是服务器而不是客户端的功能? (
  • 我以为你必须自己实现回显,TCPServer真的回显吗?听起来像是普通 TCPServer 不想要的东西。
  • 看来你应该是一种使用IO#getpass方法的方法但是当我尝试时我得到Errno::ENXIO: Device not configured

标签: ruby


【解决方案1】:

你必须向客户端发送正确的字节序列,告诉它抑制本地回显。这些在RFC857 中有描述。

我看着通过a 987654323 of 987654325 on 987654327 to 987654329 this 987654331 it's 987654333 very简单。不过,这就是最终的工作:

require 'socket'

server = TCPServer.open(2000)
client = server.accept

# Send the IAC DONT ECHO byte sequence
client.write "#{255.chr}#{254.chr}#{1.chr}"
# Send the IAC WILL ECHO byte sequence
client.write "#{255.chr}#{251.chr}#{1.chr}"

client.write 'Enter any text and it will not echo locally: '
text = client.gets.chomp
client.write "\r\n"
client.write "The text entered while echo was suppressed was #{text}\r\n"

# Send the IAC DO ECHO byte sequence
client.write "#{255.chr}#{253.chr}#{1.chr}"
# Send the IAC WONT ECHO byte sequence
client.write "#{255.chr}#{252.chr}#{1.chr}"

client.write 'Local echo has been re-enabled, enter any text: '
client.gets.chomp

client.close

telnet客户端的输出是:

telnet localhost 2000
Trying ::1...
Connected to localhost.
Escape character is '^]'.
Enter any text and it will not echo locally:
The text entered while echo was suppressed was foo
Local echo has been re-enabled, enter any text: bar
Connection closed by foreign host.

此解决方案取决于支持这些模式的客户端 telnet 应用程序,并在请求时尊重它们。不能保证客户会尊重他们。

【讨论】:

  • 你用的是什么telnet客户端;我正在为 mac 使用一个?
  • 我使用 brew 在 macOS 上安装 telnet。 brew info telnet 使用公式 https://github.com/Homebrew/homebrew-core/blob/master/Formula/telnet.rb 显示 telnet: stable 60 (bottled)
  • @FlyingV 我很想知道这是否适合你。这是一个有趣的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-10
  • 1970-01-01
  • 2011-04-28
  • 2015-02-13
相关资源
最近更新 更多