问题
I have a server which opens a connections for a telnet client, like
for example:
I run the server ./server
and in another window I run telnet client as telnet localhost 9999,
as I run the telnet client, I will get new CLI prompt as CLI>>.
From this prompt I need custom tab completion, but many of the blog says we can really dont have readline feature implemented on the telnet side, if so we have go for our own client.
How do I achieve it? Any related help would be greatly appreciated. I am using linux(Ubuntu) and C language.
回答1:
You can implement this either on the client or on the server.
For the client-side implementation there are two ways (which are basically the same):
When the client connects, the server sends a list of commands and their arguments, and that is cached in the client. When a user presses the TAB key the client searches this cached data.
When the client notices the TAB key being pressed, it asks the server for a list of possible completions. For speed this list should be cached on the client side.
So the basic solution here is: Server sends data to client, client shows data.
For a server-side implementation, you have to use telnet negotiation to tell the client to send raw uncooked characters and keys without any interference to the server. Then the server can check for the TAB key and perform completion.
The problem here is that then you have to add all command line editing and prompting in the server code, and can't rely on the client be anything but a "dumb terminal".
Having all processing done server-side has the upside that you can use almost any telnet client (as long as it can handle the telnet negotiations) and don't have to make your own custom client. The drawback is that you then have to implement the whole command-line editing features yourself in the server, and that the latencies for key-presses can be high as each key has to be sent to the server and then echoed back from the server.
Having a custom client has the upside that there are libraries which can easily handle command-line editing and help with the completion. The major downside here is that you have to make a custom client.
回答2:
After you telnet host, input ctrl + ]
to go into telnet command prompt, then execute mode character
. After that, the telnet client goes into "character at a time" mode, and tab completion should work fine.
sunnogo@a3e420:~$ telnet 192.168.193.88 10015
Trying 192.168.193.88...
Connected to 192.168.193.88.
Escape character is '^]'.
my_prompt>
my_prompt>
my_prompt>
my_prompt>^]
telnet> mod character
my_prompt��
my_prompt>
my_prompt>
回答3:
You have to put the telnet client in char mode, so it sends each key directly to the server when it's typed (instead of waiting for the return key to send a complete line). Then, whenever you receive a '\t', check what you received before that '\t', and depending on whether you can complete the line, send back the complete line or a list of possibilities. Basically, you're implementing readline() in the server.
来源:https://stackoverflow.com/questions/20467444/how-to-implement-tab-completion-on-the-telnet-client-side