8 ms·
Show HN: A library to add a command promp (and telnet) to your programs
- buserror 6y agoThis is a "C19 lockdown project" -- I always want that feature in long running processes: quick way to do introspection in a program without having to create a whole subsystem for it. Also, a way to 'connect' back in, especially for programs that don't log anything. Demo is in C, but it shouldnt be too hard to add bindings for other languages. This is very fresh paint, it had a demo of "one" in one of my other program, so I do expect the odd issue!
- ggerganov 6y agoNice little project! I did something similar [0], but instead of telnet, the lib opens a websocket and you connect with your browser. [0] - https://github.com/ggerganov/incppect https://github.com/ggerganov/incppect
- fomojola 6y agoNice: typically you can also embed a web interface that is only available on localhost, but the telnet-style interface can be handy too.
- p4bl0 6y agoThe first thing I thought of is "I could use this to telnet into long running computation processes and force them to flush those buffers to see where they're at and how it goes". This use case alone make this kind of thing worthy :).
- onion2k 6y agoIf you do it with telnet then so could I.. :)
- p4bl0 6y agoNot really if the socket is bound to localhost and the process runs on my machine :).
- jacobush 6y ago... But eBay could.
- kingosticks 6y agoHow? Ebay could send HTTP requests to the local server but what would that achieve?
- jacobush 6y agohttps://news.ycombinator.com/item?id=23436775 https://news.ycombinator.com/item?id=23436775
- kingosticks 6y agoUnless I am missing something, a port scan is not a telnet connection. I still don't see how eBay (or anyone) has access through the browser.
- dspillett 6y agoSome tools respond to a Unix signal (https://en.wikipedia.org/wiki/Signal_(IPC) https://en.wikipedia.org/wiki/Signal_(IPC)) this way, usually SIGUSR1 or SIGUSR2 though I've seen SIGCONT used too. SIGINT also, though that feels wrong to me as it circumvents the commonly expected response to ctrl-c.
- p4bl0 6y agoYes that's actually what I did for my last long running jobs: I trapped SIGUSR1 so that when my process receives it it flushes its buffers.
- skissane 6y ago> 'telnet' on linux doesn't handle UNIX sockets Someone should write a patch to fix that. There is no reason why the telnet protocol can't be used over UNIX domain sockets. As you note, telnet has some advantages over socat/netcat/etc, e.g. the Negotiate About Window Size (NAWS) option can be used to notify terminal resize events (SIGWINCH). One could also register and implement a telnet authentication type [1] to trigger authentication via SCM_CREDENTIALS. The server could use this to confirm the client has the expected UID/GID. [1] https://www.iana.org/assignments/telnet-options/telnet-options.xhtml#telnet-options-4 https://www.iana.org/assignments/telnet-options/telnet-optio...
- buserror 6y agoGood idea, altho I will have to check if busybox telnet for example supports it.
- skissane 6y agobusybox telnet and busybox telnetd both support Unix domain sockets. As in this example: sudo busybox telnetd -b local:/telnetd.sock busybox telnet local:/telnetd.sock Then you can log in over telnet over Unix domain socket. HOWEVER, this only works if busybox is built with CONFIG_FEATURE_UNIX_LOCAL=y. And normally, busybox isn't built with that enabled. It is one of the few features which "make defconfig" doesn't enable. I wonder if busybox devs could be convinced to change defconfig to set CONFIG_FEATURE_UNIX_LOCAL=y? Their FAQ [1] says defconfig "enables all functionality except special purpose things like selinux or debugging support which would reduce the portability of the resulting binary". I don't think AF_UNIX/AF_LOCAL is really a "special purpose thing". (CONFIG_FEATURE_UNIX_LOCAL turns on AF_UNIX support for all relevant busybox networking commands, it isn't telnet specific.) Of course, their telnet client doesn't support my SCM_CREDENTIALS as a telnet auth option idea either. The busybox code is reasonably clean so it wouldn't be hard to add it to their telnet client, even their telnetd just for testing (can't say the same for netkit-telnet, which is default telnet client/server on Debian/Ubuntu). Not sure if the busybox developers would be willing to accept such a random feature though. PS: telnet/telnetd in Alpine Linux are compiled with CONFIG_FEATURE_UNIX_LOCAL=y. However, the default busybox in Alpine 3.7 and above doesn't include telnet/telnetd. You can install them though with "apk add busybox-extras" PPS: on macOS, Homebrew's telnet also supports telnet to Unix domain sockets, e.g. telnet /tmp/telnetd.sock. [1] https://www.busybox.net/FAQ.html#configure https://www.busybox.net/FAQ.html#configure
- jbverschoor 6y agoOh my.. So we have COM, WCF, AppleScript/OSA, heck, we used to use two connections with FTP. One data, one control. Nevertheless. it's nice if it's easily implementable.
- michaelmcmillan 6y agoI thought we still used two connections with FTP?
- buserror 6y agoNot in PASV mode, which is the only mode supported these days.
- 0x0 6y agoPASV is still two connections, it just swaps around who's connecting to who.
- jbverschoor 6y agoI thought we don’t use fro anymore ;)
- cheez 6y agoI feel like this should be simpler: MISH_CMD_NAMES(set, "set"); MISH_CMD_HELP(set, "set 'cnt' variable", "test command for libmish!"); MISH_CMD_REGISTER(set, _test_set_cnt); Should be MISH_CMD(set, _test_set_cnt, "set 'cnt' variable", "test command for libmish!") Which can expand as needed.
- buserror 6y agoYes, I like the single macro, but that doesn't handle the multiple possible names, and multiple lines of help... but I agree, I probably will add a single alias for simple cases.
- amelius 6y agoBe aware that random websites can access localhost through your browser.
- sadfklsjlkjwt 6y agoCan they? I thought this recent issue was related only to websockets?
- ktpsns 6y agoFor all the people who complain about telnet: If it would use Unix domain sockets, the whole system would rebuild what you can do with ordinary stdin/stdout and GNU screen or tmux. I think the network transparency is a feature of this library.
- aargh_aargh 6y agoCan you please elaborate?
- ktpsns 6y agoGNU Screen and tmux are popular terminal multiplexers. One of the central features of such command line programs are to detach sessions from the actual user terminal emulators. The multiplexer is then running in the background, collecting stdout/stderr of the running shells and programs. When the user reconnects (this is typically not network transparent, but there are "extensions" such as https://tmate.io/ https://tmate.io/), she can immediately continue to interact with the programs.
- VadimPR 6y agoYou could then automate tasks in your program with a scriptable telnet client (https://github.com/mudlet/mudlet https://github.com/mudlet/mudlet) (disclosure, I'm on the dev team)
- dpenguin 6y agoCool! You could provide a read line-based command line interface with server or client driven completions to make it even more ready to use.
- jddj 6y agoAlong this line, there are some open source MUD clients around which are (naturally) entirely telnet focused and embed lua, regex, aliases and usually their own extensible UI frameworks.
- tn1 6y agoIf you want to access this remotely and telnet by itself won't do, remember that setting up SSH tunneling is very easy!
- neverartful 6y agoGood suggestion! Insecure protocols routed through secure protocols are always an option. Tip for those who do this in production - add sufficient documentation/training so that the new guy on the team knows about it.
- j1elo 6y agoGiven the explanation from the Security section (i.e. "libmish is really made for developement purpose"), it might make more sense that telnet access is disabled by default and only enabled with something like MISH_ON=1. Otherwise, this becomes yet another thing to disable upon deployment, chances are people will forget they used this during development and production code will get deployed without the variable to disable telnet.
- mishoo 6y agoNow add a Common Lisp compiler to it and you reinvented technology from the 80'es. :-)
- hnlmorg 6y agoIf you want to get pedantic then clear text consoles are the core concept behind time sharing systems, the methodology behind Multics and UNIX, and thus pre-dates LISP Machines by decades (we all stand on the shoulders of giants). But that doesn't mean there isn't still value in someone writing a new library and sharing it with the community.
- lispm 6y agoInteractive LISP consoles were used before UNIX or Multics existed.
- hnlmorg 6y agoIndeed however to be analogous with the submission those consoles would still need to be running on a time sharing system like I described. Time sharing systems also pre-date UNIX and Multics, which is why I exampled them as being operating systems that use the time sharing metaphor.
- lispm 6y agoRight, Time sharing wasn't implemented yet at that time, when Lisp was having its first interactive console in 1960. It was then also implemented on the first time sharing operating systems.
- hnlmorg 6y agook, let's put it another way, a LISP console is all well and good but systems in 1960 weren't yet multi-tasking (or at least multi-tasking systems in 1960 were still rare) so you couldn't have a LISP process running as one thread and query it's running state from another. The reason I keep hammering on about time sharing is because they largely came hand in hand, time sharing systems needed to support multi-tasking whereas single user systems didn't (you still see echoes of this in the 80s with CP/M, DOS and 8-bit micros running BASIC). In fact if you look at the history of multi-tasking it roughly follows the same time line and lineage as the uptake of time sharing systems (around mid '60s IIRC). Before then you'd have to stop the execution of one program before you could start execution on another. I know some "LISPians" like to think everything in computing eventually leads back to LISP but that's not always the case (and I say that as a big fan of the language myself).
- SmallPeePeeMan 6y agoNice job! But do you know about Prometheus?
- TedDoesntTalk 6y agoNice job, but do you know about Prometheus? https://prometheus.io/ https://prometheus.io/
- derision 6y agoprometheus is completely different
- andybak 6y agoShould the title not specify "to your own C programs" or similar? Or if it is more widely applicable then how would it work with non-C programs?
- msravi 6y agoI write automated trading algos for my own trading, and invariably put in a "command line interface" so I can control variables during trading without having to restart. I don't know if this is the normal thing to do, but I just read the variables from a mysql table at some logical point in the program loop, where changing it is "safe". The CLI is then simply about connecting to the DB and changing those variables using standard SQL statements... I guess the "right" way would be to build a proper (G)UI to handle this, but hey, it works.
- sk0g 6y agoIf you're using PostgreSQL, pgAdmin might be all the GUI you need, or whatever your database's equivalent program is.
- tyingq 6y agoIn the Java world, it's very common to use JMX for that, and there are several JMX clients to read/set variables, watch logs, etc.
- neverartful 6y agoI too thought of JMX when reading through comments. I've always thought that JMX was underrated.
- neverartful 6y agoI have an existing use case for this. One of the music players that I use a lot is command line based and I start it to run with shuffle-play (i.e., play songs randomly). Sometimes the song chosen isn't something I want to listen to at that time. However, I don't have a nice way of skipping/advancing the currently playing song. If I really want to skip the song I have to Ctrl-C out of it and restart it. I was going to add signal handlers for SIGUSR1, but I didn't know how to do this in Python. Adding a library such as this one, I could also enhance to alter queued up songs.
- ComodoHacker 6y agoTry mpd or VLC with telnet interface.
- neverartful 6y agoThanks for mpd suggestion. I had not heard of it before and it appears to be exactly what I want.
- jonahbenton 6y agoNice- obligatory mention that this is old hat in languages with REPLs. In Clojure, shipping to production with a REPL port (running over an HTTPS websocket, only accessible from a bastion host) is common practice and saves a lot of work making ad hoc admin tools and troubleshooting from logs.
- brockrockman 6y agoI feel like this hits Tcl's use case pretty well, too. Old but not forgotten.
- Quarrel 6y agoVery nice. Now just need a small wrapper around ptrace to inject the library and call mish_prepare(0), so can just dynamically add it as needed to running programs.
- jhallenworld 6y ago__attribute__((constructor,used)) This is very clever.. in my CLI I use a dedicated section to build a command table, but the linker script often has to be modified for this to work. The above avoids the need to change the linker script at all (but you do need an init function, which I don't..). It means you have to compile as C++, right?
- lunixbochs 6y agoconstructor works fine in C as with GCC/clang. It links down to .init/.init_array on ELF (though all the linker cares about is PT_DYNAMIC and DT_INIT / DT_INIT_ARRAY)
- antoineMoPa 6y agoCan't complain about the security; there is no security.
- eqvinox 6y agoFor comparison, FRRouting has 25 year old CLI code: https://github.com/FRRouting/frr/blob/master/doc/developer/cli.rst https://github.com/FRRouting/frr/blob/master/doc/developer/c... https://github.com/FRRouting/frr/blob/master/lib/command.c https://github.com/FRRouting/frr/blob/master/lib/command.c (+ other files nearby) looks like this: DEFUN (show_version, show_version_cmd, "show version", SHOW_STR "Displays zebra version\n") { ... } install_element(VIEW_NODE, &show_version_cmd); self-registering commands (__attribute__((constructor))) are on the TODO list. On the other hand we have automatic parameter parsing into proper C types (look for "DEFPY" in the code/docs.) disclaimer: I conceived and implemented DEFPY.