iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🚇

cmdw: Executing local machine commands via HTTP

に公開

I created a tool called cmdw to handle these kinds of situations easily.

  • I've created a useful command and want people to try it out.
  • However, it's difficult or tedious for others to execute that command.
  • Therefore, I want to let them access my machine via a browser to run the command.
  • But I don't want to make any changes to the command itself.

https://github.com/kentaro/cmdw

Specific Use Case

As I wrote in "ChatWP: Talking to WordPress by turning it into an AI", I created ChatWP. I want people in my company to experience "what it's like" by actually trying it, but executing this command is tedious for the following reasons:

  • You need to prepare an OpenAI API key.
  • You need to set up a Python execution environment.
  • You need to build a WordPress index, which takes time.
  • You need to have an account issued to access the API as a WordPress user.

So, it would be good to use cmdw to let them access your machine via HTTP and execute the command.

I'm sure tools like this already exist, but I couldn't find one and it seemed like something I could write quickly, so I made it myself.

Usage

I will explain based on the ChatWP case. This assumes that ChatWP itself is already prepared to run properly on your local machine.

After moving to the ChatWP repository, run the cmdw command as follows:

cd /path/to/chatwp
cmdw --command python --addr 0.0.0.0 --port 8082

A web server will start, so try accessing http://localhost:8082. When you pass arguments through the form and submit, the output of the command will be displayed (the output to the command's standard output will be shown).

Making it accessible from outside

You might sometimes want people who are not on the same LAN to try it out. Let's create a tunnel so it can be connected to from the outside. There are various ways to do this, but using cloudflared is simple.

In the case of a Mac, you can install it using Homebrew as follows:

brew install cloudflared

Running cloudflared as follows will make the previously started cmdw accessible globally. A URL will be issued, so please share it with the person you want to run the command (the URL in the command output below is masked).

cloudflared tunnel --url http://localhost:8082
2023-03-13T11:16:33Z INF Thank you for trying Cloudflare Tunnel. Doing so, without a Cloudflare account, is a quick way to experiment and try it out. However, be aware that these account-less Tunnels have no uptime guarantee. If you intend to use Tunnels in production you should use a pre-created named tunnel by following: https://developers.cloudflare.com/cloudflare-one/connections/connect-apps
2023-03-13T11:16:33Z INF Requesting new quick Tunnel on trycloudflare.com...
2023-03-13T11:16:34Z INF +--------------------------------------------------------------------------------------------+
2023-03-13T11:16:34Z INF |  Your quick Tunnel has been created! Visit it at (it may take some time to be reachable):  |
2023-03-13T11:16:34Z INF |  https://******************.trycloudflare.com                                     |
2023-03-13T11:16:34Z INF +--------------------------------------------------------------------------------------------+

I learned about this from the following tweet:

https://twitter.com/wesbos/status/1634310926219333642

Precautions

cmdw is a command intended for simple use among trusted individuals. Naturally, you should avoid using it in situations where it can be accessed by anyone.

Discussion