Search code examples
javascriptterminalptyxtermjs

Click button and send command to execute on xterm.js terminal


I want to click a button and send a linux command such as "ls" to be executed on xterm.js terminal. Is there a way to do this?

Pty pseudo-terminal written in Go is running remotely and I'm using xterm.js as the front end component.


Solution

  • Something like this should take care of the case mentioned above

    In HTML

    <button (click)="runCmd('ls')>ls</button> 
    

    In app.component.ts

    Assuming there is a websocket server that is listening to the end point mentioned here...

    public runCmd(command){
      var url = "ws://<ip>:<port>/pty"
      var terminal = new Terminal()
      terminal.open(document.getElementById("terminalDiv");
      var socket = new WebSocket(url);
      var attachAddon = new AttachAddon(socket);
      terminal.loadAddon(attachAddon);
    
      //socket.send() will send the data passed to it. 
      socket.send(command);
    }