DEV Community

Cover image for How you can execute terminal command on server via UI from client-side?
Utsav Ladani
Utsav Ladani

Posted on • Updated on

How you can execute terminal command on server via UI from client-side?

Alt Text

Hey folks, What am I saying, is it possible to execute command on server via UI from client-side with few lines of code?
Yes, possible.

How?

Let me tell you.

  1. Make your own UI for enter command and send that command to server with suitable method and request.(use fetch, axios, etc.)

  2. Make a basic server with your favourite libraries, now add one another library with name execa.

What execa do?

execa is a js library for execute .exe file with one line of code.
like this

// your code ....

const execa = require('execa')
let output = execa.sync(command).stdout // pass your command as a args

// your code .... 
Enter fullscreen mode Exit fullscreen mode

Now send this output in res and you can get the output of terminal.

Cons

you can't execute all command, because execa is execute only one line command and then close the cmd. When you run command, execa open new cmd and execute command, then close cmd. So your 'cd dir' command run successfully but cmd closed and next time open new cmd, so we can get old path and cd dir goes vain.

here I give you link of simple project on github, learn and enjoy 😄

GitHub logo Utsav-Ladani / Html-and-Terminal

Type command in browser and run on server

Top comments (1)

Collapse
 
utsavladani profile image
Utsav Ladani

If you like to contribute, then go and contribute
OR
If have any suggestions, then plz write here.