DEV Community

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

Posted on β€’ Edited on

1 2

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

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.

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay