Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JRuby + Apache MINA SSHD
# First, download a binary distribution from
# http://mina.apache.org/sshd/sshd-070.html
# and copy lib/*.jar to your cwd
# Load all the jar files
require 'java'
Dir['./*.jar'].each {|jar| require jar }
# Import some Java classes
java_import java.util.EnumSet
java_import org.apache.sshd.SshServer
java_import org.apache.sshd.common.compression.CompressionZlib
java_import org.apache.sshd.server.PasswordAuthenticator
java_import org.apache.sshd.server.PublickeyAuthenticator
java_import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider
java_import org.apache.sshd.server.shell.ProcessShellFactory
PORT = 5000
class CustomPasswordAuthenticator
include PasswordAuthenticator
def authenticate(username, password, session)
# Your password authentication magic here
# Return true for auth success and false for failure
true
end
end
class CustomPublickeyAuthenticator
include PublickeyAuthenticator
def authenticate(username, key, session)
# Your public key authentication magic here
# Return true for auth success and false for failure
true
end
end
# Set up the server with sensible defaults for ciphers, etc.
sshd = SshServer.setUpDefaultServer
# Set the port
sshd.setPort(PORT)
# Create a new host key or read an existing one
sshd.setKeyPairProvider(SimpleGeneratorHostKeyProvider.new('key.ser'))
# Enable zlib compression (optional, omit to reduce CPU usage)
sshd.setCompressionFactories([CompressionZlib::Factory.new])
# Install your custom authenticator(s)
sshd.setPasswordAuthenticator(CustomPasswordAuthenticator.new)
sshd.setPublickeyAuthenticator(CustomPublickeyAuthenticator.new)
# Set the shell to run in each new session
# The user's name will be available in the $USER environment variable
sshd.setShellFactory(
ProcessShellFactory.new(
['/bin/sh', '-i', '-l'].to_java(:string),
EnumSet.of(
ProcessShellFactory::TtyOptions::ONlCr,
ProcessShellFactory::TtyOptions::ICrNl)))
# Start the server
sshd.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.