Can you run commands on a container programmatically?

Hello Community,
This is yet another question about the undocumented API. Would it be possible too programmatically use the console for a project? This would mean that from my machine, I would be able to run a command on the container. I did some looking around, and it appears that you are using wetty and socket.io. That leads me to have a look at the source code for the wetty backend, and it takes in commands as a message gets sent through the WebSocket. I would use this ability for a ***, and a ***. Contact me for more details if you are interested. If anyone can tell me if it is possible (preferably glitch developers), it would be much appreciated.

I believe that I have found a solution, what I can do is use the socket.io-client module to connect to wetty then send commands, and receive them by acting as a client. Does anyone else have thoughts on this?

That’s one way you could do it. Another way would be to write your own websocket listener that accepts commands from the client. Keep in mind that anyone who can connect to this and send commands will be able to do anything they want to your project, so you would need to add some kind of authentication.

@Tim Can you explain that a bit more?

I was thinking of something like this. It will run the command this way, but if you wanted to return the output of the command to the client, that would be trickier.

// app is your express app
var expressWs = require('express-ws')(app);

app.ws('/cmd', function(ws, req) {
  // websocket connections to /cmd come in here
  ws.loggedIn = false;

  ws.on('message', function(msg) {
    if (msg === "login:" + process.env.PASSWORD) {
      ws.loggedIn = true;
    }

    if (!ws.loggedIn) {
      console.log("User wasn't logged in and tried to: " + msg);
      return;
    }

    // Use child_process.spawn here to run the command in msg

    ws.send(msg);
  });
});
1 Like

Is their a way for me to communicate with the container without taking the only public port?

Would it be a challenge to add the ability to run commands on a container and get the output from the Glitch API? This would allow for users to use their own web terminals and some other good stuff with extensions.

If you do it this way, the port can be used for HTTP and the websocket.

Adding this kind of thing to the API wouldn’t be particularly hard, but it’s a question of prioritization - we have lots of other things we want to do, too. This is pretty low on the list because the console is already available.

Any ideas for the simplest way to implement this for the user, since I want them to just run a command and it will set it up for them?