Is there any open source command line library? [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this questionI want to add command line interface support to a server program.
So each time I want to check server's status, I can 开发者_运维百科telnet to the server's control port, and input command to check the server's status.
Is there any open source library implements such functionality so I need not write it from scratch?
Further more, if the library can provides more features such like cisco's command line interface (tab key hint, command query, vi mode, or even symbol based command), that's would be great.
libcli sounds like exactly the thing you are looking for. It includes Cisco-like functionality and a telnet server. I've never used it, but came across it a while back when I was looking for a similar CLI.
A telnet
server is almost trivial to write from scratch, the only thing easier is the obsolete rlogin
protocol, which is still accessible from ssh. You won't have any trouble making a few socket calls to implement such a thing.
You are about to be told, I am sure, not to use telnet but rather ssh. It's good advice, but you have two good choices here: implement telnet in open code locally and connect via an ssh tunnel, or, use an ssh server to log in via a secure channel.
See here: for SSH servers. That won't help you directly, although apache has a Java implementation that embeds easily. If you just google for "telnet server" you will get more links than I can possibly quote here, in a variety of languages, many containing example code for an easy implementation. It's also possible that the Stevens books treat this topic directly as well.
If you don't want to deal with sockets or shared memory segments, its rather easy to have your daemon update a SQLite3 database so that a client can get its stats with a simple query, however that can become problematic as you'd need to use the host OS's user/group management to grant underprivileged users access.
In all reality, its usually better to just use a simple socket connection. In Linux, its quite easy to figure out (locally) what user ID is calling and permit only a few.
You can use the socket
library to listen to a TCP port, receive commands there and output back your status. There's not much code required to do that; an example can be found here: http://www.paulgriffiths.net/program/c/echoserv.php.
(The example program just returns the command that has been sent; clearly, you would want to replace Writeline(...);
with something more specific to your application.)
Is readline
(3) what you are looking for?
readline will read a line from the terminal and return it, using prompt as a prompt. If prompt is NULL or the empty string, no prompt is issued. The line returned is allocated with malloc(3); the caller must free it when finished. The line returned has the final new‐ line removed, so only the text of the line remains.
It was unclear from the question if you are looking to implement the connection of the user interaction. This answers, the UI part.
This is the software I used before. It provides almost the same CLI features with Cisco CLI interface.
All you need to do is porting the source code under lib folder.
Quagga Routing Suite https://www.nongnu.org/quagga/
精彩评论