SSH is an encrypted transport method. Using it is highly secure, safe, and sometimes a huge pain in the ass to set up. Luckily, one of the kind citizens of the Interwebs created the ssh2 library for node, which makes setting up an ssh (v2) server easy as pie.

First install the ssh2 library:

npm install ssh2 --save  

Adding --save to your script adds the library declaration to your package.json file.

Once you have ssh2 installed, start an instance of the server and pass it your private key. If you don't know where your private key is or how to generate one, read this

ssh2 = require 'ssh2'  
fs = require 'fs'

myPrivateKey = fs.readFileSync './id_rsa'

new ssh2.Server  
    privateKey: myPrivateKey
, (client) ->
    # Stuff

The client object is an instance of the ssh2.Client object that is used to pass messages between the server and the client instance. The first step to using your server is authentication.

Nodejs being an event driven system, we must listen for an authentication event:

client.on 'authentication', (clientContext) ->  
     # Stuff

Then we need to set ourselves up to start doing stuff once the instance finishes its internal initialization routines. When the client is ready it emits a ready event:

client.on 'ready', -> # run client code here  

Now according to the ssh2 docs, the ssh2 client is capable of emitting several events. The one we care about today is the session event:

client.on 'session', (accept, reject) -> accept()  

The session event callback has an accept function, which when called enables the rest of the session to proceed.

So here you have it! Check out the full example below for an interactive shell session:

fs = require 'fs'  
inspect = require('util').inspect  
ssh2 = require 'ssh2'

new ssh2.Server  
    privateKey: fs.readFileSync './id_rsa'
    port: 57037
, (client) ->
    console.log 'Client connected'

    client.on 'authentication', (ctx) ->
    .on 'ready', ->
        console.log 'Client authenticated'
        client.on 'session', (accept, reject) ->
            session = accept()
            session.on 'pty', (accept, reject, info) ->
                console.log info.command
                session.on 'shell', (accept, reject) ->
                    stream = accept()
                    stream.setEncoding 'utf8'
                    stream.stderr.write 'Error!'
                    stream.write 'Just kidding'
                    stream.exit 0
                session.on 'error', ->
                    console.log 'error'

    .on 'end', ->
        console.log 'Client disconnected'
.listen 57037, '', ->
    console.log 'Listening on port', @address().port