In my last post, I wrote about the Everything API, a project that outputs the results of some scripts to a web page.

However, there is still a lot of room for improvement. For example, if only the UI could be nicer, and even interactive commands could be executed. Finally, on second thought, isn’t it a Terminal version of the Web?

And I was thinking about changing it, but think about it, if you’re going to end up with a Web version of Terminal, it’s already open source.

So I began to search, finally found a few good.

Web Terminal

  • Ttyd:github.com/tsl0922/tty… , a command line to the Web execution tool, based on C written.
  • Gotty: github.com/yudai/gotty, same as TTYD, only in Go, but last updated in 2017, probably in disrepair.
  • Wetty:github.com/butlerx/wet… , based on Node.js development, can also turn the command line to the Web execution, but need to be based on SSH login, in fact, is a Web version of SSH terminal.
  • Secure Shell (Chrome App) : a plug-in for Google Browser that can also provide web-based SSH terminals.
  • Tmate: tmate. IO /, modified from TMUx, supports Terminal sharing.

After a trial run, my first choice is TTYD, the others are sSH-based, don’t work well or are out of maintenance.

Here I will introduce the simple usage of TTYD.

The installation

The installation is actually very simple, I use a Mac, so I use HomeBrew to install directly:

brew install ttyd
Copy the code

If you are running Windows or Linux, it is still supported and can be installed at github.com/tsl0922/tty… Chapter.

use

Ttyd supports many function configurations, complete commands are as follows:

ttyd is a tool for sharing terminal over the web

USAGE:
    ttyd [options] <command> [<arguments...>]

VERSION:
    1.6.3

OPTIONS:
    -p, --port              Port to listen (default: 7681, use `0` for random port)
    -i, --interface         Network interface to bind (eg: eth0), or UNIX domain socket path (eg: /var/run/ttyd.sock)
    -c, --credential        Credential for Basic Authentication (format: username:password)
    -u, --uid               User id to run with
    -g, --gid               Group id to run with
    -s, --signal            Signal to send to the command when exit it (default: 1, SIGHUP)
    -a, --url-arg           Allow client to send command line arguments in URL (eg: http://localhost:7681? arg=foo&arg=bar)-R, --readonly          Do not allow clients to write to the TTY
    -t, --client-option     Send option to client (format: key=value), repeat to add more options
    -T, --terminal-type     Terminal type to report, default: xterm-256color
    -O, --check-origin      Do not allow websocket connection from different origin
    -m, --max-clients       Maximum clients to support (default: 0, no limit)
    -o, --once              Accept only one client and exit on disconnection
    -B, --browser           Open terminal with the default system browser
    -I, --index             Custom index.html path
    -b, --base-path         Expected base path for requests coming from a reverse proxy (eg: /mounted/here)
    -P, --ping-interval     Websocket ping interval(sec) (default: 300)
    - 6, --ipv6              Enable IPv6 support
    -S, --ssl               Enable SSL
    -C, --ssl-cert          SSL certificate file path
    -K, --ssl-key           SSL key file path
    -A, --ssl-ca            SSL CA file path for client certificate verification
    -d, --debug             Set log level (default: 7)
    -v, --version           Print the version and exit
    -h, --help              Print this text and exit

Visit https://github.com/tsl0922/ttyd to get more information and report bugs.
Copy the code

As you can see, you can use -p to specify the run port, -c to specify the login password, and so on.

The basic use

Let’s try it. The basic command is as follows:

ttyd bash
Copy the code

So you start a Web version of bash and run it as follows:

It is shown to be running on 7681, so we can open http://localhost:7681/ and run the command directly:

It’s very silky.

The transmission protocol is WebSocket, so the stability is guaranteed:

Of course, instead of bash, we can use our favorite Shell, such as ZSH, with the following command:

ttyd zsh
Copy the code

The Shell in the browser is ZSH:

Binding port

Of course, we can also change the port, such as 8000, you can use the following command:

ttyd -p 8000 zsh
Copy the code

So ttYD can run HTTP service on port 8000, we can open http://localhost:8000/ to execute the command.

Basic Auth

We can set Basic Auth and use -c to specify the username and password in the format of username:password. For example, if we specify the username and password as admin, we can run the following command:

ttyd -p 8000 -c admin:admin zsh
Copy the code

After opening http://localhost:8000/, you need to enter the username and password to log in:

Automatically open the browser

We can also use the -b command to make it open the browser automatically:

ttyd -p 8000 -B zsh
Copy the code

After this operation, the default browser will automatically open http://localhost:8000/, we do not have to type url, very convenient.

So, the above command can even be made an alias, for example:

alias webcmd="ttyd -p 8000 -B zsh";
Copy the code

Enter webcmd to easily open a Web command line.

Docker support

In addition, TTYD also provides Docker image, if you do not want to install, you can directly start Docker, such as this can be launched on 7681:

docker run -it --rm -p 7681:7681 tsl0922/ttyd
Copy the code

But this actually exposes the command line inside the container. To expose the command line of the host machine, you need to mount the disk:

SSH terminal

Ttyd also supports SSH terminals. The command is as follows:

ttyd login
Copy the code

In this case, after opening the browser, you need to log in using SSH and enter the correct SSH user name and password to use it.

SSL support

If you want to configure SSL support, that is, to support HTTPS, you can generate your own certificate and add the corresponding parameters to start TTYD, see github.com/tsl0922/tty… .

More and more

If you want to learn more about the above usage, you can refer to the Wiki at github.com/tsl0922/tty… .

Public exposure

Of course, if we want to expose its public network, we can also cooperate with Ngrok, for example, TTYD runs on port 8000, I can use Ngrok to expose it:

ngrok http 8000
Copy the code

The running results are as follows:

This allows me to access the terminal using the specified URL, such as 11b4-2404-f801-8050-3-bf-00-55.ngrok. IO / :

Very Nice!

conclusion

Well, the above is the basic use of TTYD, with it, we can easily turn the terminal on a machine to the Web to execute, or very convenient and useful.

For more exciting content, please pay attention to my public account “Attack Coder” and “Cui Qingcai | Jingmi”.