Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何从套接字获取输入而不进行回显?_Ruby - Fatal编程技术网

Ruby 如何从套接字获取输入而不进行回显?

Ruby 如何从套接字获取输入而不进行回显?,ruby,Ruby,我正在用Ruby创建一个TCP服务器,需要从用户那里获取密码。此密码不应回显。不幸的是,当我telnet到端口8080并键入时:我的密码被回显 @server_socket = TCPServer.open(8080, localhost) conn = @server_socket.accept conn.write "Password: " conn.gets.chomp # <-- This should not be visible @server\u socket=TCPSer

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

@server_socket = TCPServer.open(8080, localhost)
conn = @server_socket.accept
conn.write "Password: "
conn.gets.chomp # <-- This should not be visible
@server\u socket=TCPServer.open(8080,本地主机)
conn=@server\u socket.accept
conn.write“密码:”

conn.gets.chomp#您必须向客户端发送正确的字节序列,以告诉它抑制本地回音。这些在中进行了描述

我直截了当地看了一遍。然而,这就是最终起作用的原因:

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应用程序。无法保证客户端会尊重它们。

password=conn.noecho(&:get).chomp###这也不起作用。您的意思是,当您远程登录并键入密码时,它会在您键入密码时出现在控制台中吗?因为这是客户端的功能,而不是服务器的功能。@anothermh:不可能?当用户远程登录到登录屏幕时;当他输入密码时,密码不会显示出来。这是服务器的功能,而不是客户端?(我以为你必须自己实现回显,TCPServer真的会回显吗?听起来像是一个普通的TCPServer不想要的东西。看起来你应该使用
IO#getpass
方法,但当我尝试时,我得到了
Errno::ENXIO:Device not configured
你使用的是什么telnet客户端;我正在使用一个对于mac?我使用brew在macOS上安装telnet。
brew信息telnet
使用公式
https://github.com/Homebrew/homebrew-core/blob/master/Formula/telnet.rb
@FlyingV我很想知道这对你是否有效。这是一个有趣的问题。