1

I am building a website and I would like to show a terminal on my webpage which runs a script (python or bash) interactively.

Something like trinket.io but I would like to use the python interpreter or the bash I have on my server, so I could install pip packages and in general control every aspect of the script.

I was thinking at something like an interactive frame which shows the terminal and what's executed in it, obv with user interaction supported.

A good example is https://create.withcode.uk/, it's exactly what I want but I would like to host it on my own server with my own modules and ecosystem. This seems to be pretty good also on the security side.

Is there anything like that?

8
  • 1
    If you manage to do that, take special extra total care of security because anyone could execute arbitrary code on your machine ! Commented Jul 9, 2020 at 21:28
  • @smwhr sure indeed the goal is to run a script and nothing more, something like a fixed terminal with "python script.py" inside and nothing else Commented Jul 9, 2020 at 21:30
  • If you're looking for user-interactivity, I suggest something like flask or dash. Providing something like what you've described will quickly be converted into some bouncing-point for web attacks, compromises on the host server, and who knows what else. Commented Jul 9, 2020 at 22:20
  • @r2evans thanks I'll look at it, regarding the security concerns if the terminal thing is fixed on a single script how could it be dangerous? Maybe I'm missing something Commented Jul 9, 2020 at 22:32
  • Your mention of "interactive frame" and "shows the terminal" to me scream somebody being able to interact with the REPL of the interpreter. Even if the only thing running is your script that prompts the user for input ... I still find any kind of REPL interacting with a user to be a significant security risk. I believe the general "best practice" for using python (etc) in this fashion is to work in one of the dash/flask/shiny (R language) infrastructures, designed with HTML entities (e.g., input boxes, action buttons) around a reactive programming premise. Commented Jul 9, 2020 at 22:42

2 Answers 2

2

If I understand well you look for a mechanism, that allows you to display a terminal on a web server.

Then you want to run an interactive python script on that terminal, right.

So in the end the solution to share a terminal does not necessarily have to be written in python, right? (Though I must admit that I prefer python solutions if I find them, but sometimes being pragmatic isn't a bad idea)

You might google for http and terminal emulators.

Perhaps ttyd fits the bill. https://github.com/tsl0922/ttyd

Building on linux could be done with

sudo apt-get install build-essential cmake git libjson-c-dev libwebsockets-dev
git clone https://github.com/tsl0922/ttyd.git
cd ttyd && mkdir build && cd build
cmake ..
make && make install

Usage would be something like: ttyd -p 8888 yourpythonscript.py

and then you could connect with a web browser with http://hostip:8888

you might of course 'hide' this url behind a reverse proxy and add authentification to it or add options like --credential username:password to password protect the url.

Addendum: If you want to share multiple scripts with different people and the shareing is more a on the fly thing, then you might look at tty-share ( https://github.com/elisescu/tty-share ) and tty-server ( https://github.com/elisescu/tty-server )

tty-server can be run in a docker container. tty-share can be used to run a script on your machine on one of your terminals. It will output a url, that you can give to the person you want to share the specific session with) If you think that's interesting I might elaborate on this one

Sign up to request clarification or add additional context in comments.

3 Comments

Gosh this seems to be exactly what I would like to do, wonderful. I will look at it and mark it as answer as soon as I make it work. I wonder if I could fit it in an iframe too to use it for ex. In Wordpress or similar. Thanks!
I hope it works I didn't try it, I just googled. I used many years ago another solution, but didn't find it anymore
If it doesn't work, then you had to write your own web server with web sockets (e.g. django and django channels, but smaller solutions should exist as well), start your script in a pty and let your web server relay the pty to your server. on the browser side you could use Xterm.js ( xtermjs.org ) (the web front end being used by ttyd) to display the contents of the terminal
0

>> Insert security disclaimer here <<

Easiest most hacktastic way to do it is to create a div element where you'll store your output and an input element to enter commands. Then you can ajax POST the command to a back-end controller.

The controller would take the command and run it while capturing the output of the command and sending it back to the web page for it to render it in the div

In python I use this to capture command output:

from subprocess import Popen, STDOUT, PIPE

proc = Popen(['ls', '-l'], stdout=PIPE, stderr=STDOUT, cwd='/working/directory')
proc.wait()
return proc.stdout.read()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.