How to create web based terminal using xterm.js to ssh into a system on local network

元气小坏坏 提交于 2019-12-03 04:32:40

After few research here is working code.

Following Libraries you need understand how its work.

1) https://socket.io/

This library is used for transmit package from client to server.

2) https://github.com/staltz/xstream

This library is used for terminal view.

3) https://github.com/mscdex/ssh2

This is main library. which is used for establishment connection with your remote server.

Step 1 : Install this 3 library in your project folder

Setp 2 : Start from node side create a server.js file for open socket

Step 3 : Connection client socket to node server ( both are in local machine )

Tricky logic is just how to use socket and ssh2

on emit of socket you need to trigger ssh command using ssh2 library. on on response of ssh2 ( from server ) you need to transmit socket package to client that's it

Don't writing more here is the example code

https://github.com/ankitvadariya/ssh-client-connection

This like have following files & folders

server.js
package.json
src/
xtream/

first you need to config your server IP , user and password or cert file on server.js. and just hit node server.js

P.S : Don't forget to run npm install

Let me know if you have any question !

After some research later I came across this service : https://tmate.io/ which does the job perfectly. Though if you need a web-based terminal of tmate you have to use their ssh servers as a reverse proxy which ideally I was not comfortable with. However, they provide tmate-server which can be used to host your own reverse proxy server but lacks web UI. But to build a system where you have to access a client behind NAT over ssh on web, below are the steps.

  • Install and configure tmate-server on some cloud machine.
  • Install tmate on the client side and configure to connect to a cloud machine.
  • Create a nodejs application using xterm.js(easy because of WebSocket based communication) which connects to your tmate-server and pass commands to the respective client. (Beware of security issues of exposing this application, since you will be passing Linux commands ).
  • Depending on your use case you might need a small wrapper around tmate client on client-side to start/stop it automatically or via some UI/manual action.

Note: I wrote a small wrapper on client-side as well to start/stop and pass on the required information to an API server (written in nodejs) which then pass on the information to another API which connects the browser to the respective client session. Since we had written this application it included authentication as well as command restrictions of what can be run on terminal. You can customize it a lot.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!