Hello SSH server

Accepts an anonymous SSH connection, writes one message, and closes it.

Port 30022Auth: none

Connect

Connect

ssh -p 30022 demo@modernssh-examples.manaf.ch
server.ts
import { once } from "node:events"
import { Server, SessionChannel } from "@bunkerch/modernssh"

const hostKey = process.env.SSH_HOST_KEY_PRIVATE_KEY
if (!hostKey) throw new Error("SSH_HOST_KEY_PRIVATE_KEY is required")

const server = new Server({ hostKeys: [hostKey] })

server.hooker.hook("noneAuthentication", (_hook, _context, decision) => {
  decision.allowLogin = true
})

server.hooker.hook("channelOpenRequest", (_hook, channel, decision) => {
  decision.allowOpen = channel instanceof SessionChannel
})

server.on("connection", (connection) => {
  connection.on("channel", (channel) => {
    if (!(channel instanceof SessionChannel)) return

    channel.hooker.hook("shellRequest", (_hook, decision) => {
      decision.success = true
    })

    channel.events.on("shell", (shell) => {
      void (async () => {
        await shell.writeStdout(
          "Hello! This is a simple ssh server using " +
            "https://www.npmjs.com/package/@bunkerch/modernssh! " +
            "Unfortunately, we do not provide shell access.\n",
        )
        const closed = once(shell, "close")
        shell.exit(0).close()
        await closed
        await connection.close()
      })().catch((error: unknown) => shell.destroy(error as Error))
    })
  })
})

server.listen({ host: "0.0.0.0", port: Number(process.env.PORT ?? 2222) })