0

How can I integrate a terminal in browser and be able to execute commands in it, like for example the web terminal of glitch.com

1
  • Okay, i found Gritty Commented Jun 12, 2020 at 17:07

1 Answer 1

2

This is how to do it with keystrokes, adapted from https://github.com/mscdex/ssh2/issues/429 and using https://github.com/steelbrain/node-ssh

;(async () => {
    const {NodeSSH} = require('node-ssh')

    const ssh = new NodeSSH()
    await ssh.connect({
        host: 'yourhost',
        username: 'yourusername',
        agent: process.env.SSH_AUTH_SOCK,
        compress: true,
    })

    const pipeStream = stream => {
        const {stdin, stdout, stderr} = process
        const {isTTY} = stdout

        if (isTTY && stdin.setRawMode) stdin.setRawMode(true)

        stream.pipe(stdout)
        stream.stderr.pipe(stderr)
        stdin.pipe(stream)

        const onResize =
            isTTY && (() => stream.setWindow(stdout.rows, stdout.columns, null, null))
        if (isTTY) {
            stream.once('data', onResize)
            process.stdout.on('resize', onResize)
        }
        stream.on('close', () => {
            if (isTTY) process.stdout.removeListener('resize', onResize)
            stream.unpipe()
            stream.stderr.unpipe()
            stdin.unpipe()
            if (stdin.setRawMode) stdin.setRawMode(false)
            stdin.unref()
        })
    }

    await new Promise((resolve, reject) => {
        ssh.connection.shell({term: process.env.TERM || 'vt100'}, (err, stream) => {
            if (err) {
                reject(err)
                return
            }
            pipeStream(stream)
            stream.on('close', () => resolve(true))
        })
    })

    ssh.dispose()
})()

Now it would be fun to figure out how to make this work with rsync via --rsh='node ssh.js'

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.