BSD License, 需要 JDK 6.0 或者 JRE 6.0 + OpenJDK Javac 运行. 附件为代码包 (请到 http://sjsh.dev.java.net 下载最新代码包, 此处附件以后不再更新)
参照代码包中 demo/src 目录下的示例, 就可以很简单的写成包装自己应用对象的 SSH Server 了
启动演示 SSH Shell Server
D:\workspace\SecureJSH>ant demo Buildfile: build.xml
build:
compile-demo:
run-java-demo: [java] SSH-2.0-SJSHD-1.0: Generating an authorized key for user 'sjsh'... [java] SSH-2.0-SJSHD-1.0: New authorized private key for user 'sjsh' stored to D:\workspace\SecureJSH\SJSH-Demo-Root\home\sjsh.ssh\id_rsa [java] SSH-2.0-SJSHD-1.0: Updated authorized public keys for user 'sjsh' stored to D:\workspace\SecureJSH\SJSH-Demo-Root\home\sjsh.ssh\authorized_keys [java] SSH-2.0-SJSHD-1.0(@localhost/127.0.0.1:22022): Starting... [java] SSH-2.0-SJSHD-1.0: Generating Host DSA Key... [java] SSH-2.0-SJSHD-1.0: Host DSA Key Stored to: D:\workspace\SecureJSH\SJSH-Demo-Root\etc\ssh\ssh_host_dsa_key [java] SSH-2.0-SJSHD-1.0: Host DSA Public Key Stored to: D:\workspace\SecureJSH\SJSH-Demo-Root\etc\ssh\ssh_host_dsa_key.pub [java] SSH-2.0-SJSHD-1.0: Generating Host RSA Key... [java] SSH-2.0-SJSHD-1.0: Host RSA Key Stored to: D:\workspace\SecureJSH\SJSH-Demo-Root\etc\ssh\ssh_host_rsa_key [java] SSH-2.0-SJSHD-1.0: Host RSA Public Key Stored to: D:\workspace\SecureJSH\SJSH-Demo-Root\etc\ssh\ssh_host_rsa_key.pub [java] SSH-2.0-SJSHD-1.0(@localhost/127.0.0.1:22022): Started. [java] Now you can login using an SSH client, with: [java] User Name: sjsh [java] Private Key: D:\workspace\SecureJSH\SJSH-Demo-Root\home\sjsh.ssh\id_rsa [java] [java] Type in anything to stop:
然后通过任意SSH客户端连接上去: (注意OpenSSH客户端需要更改 SJSH-Root\home\sjsh.ssh\id_rsa 的权限属性为 600, 其他客户端不支持PEM格式私钥的需要转换其格式, PuTTY 带的 PUTTYGEN 工具转换比较好用)
Using username "sjsh". Authenticating with public key "imported-openssh-key"
This is an interactive Java(TM) shell, type in Java(TM) statements to get them executed.
Type in Ctrl^D to logout.
Type in ? or help for a list of available commands.
Type in to complete commands.
Use UP/DOWN arrow keys for command history.
Here you are in the demo shell.
There are two demo built-in commands: 'msg' and 'msgs', and one built-in field: 'msgs', those are added by this demo.
Try them out and write your own shell similarly.
SecueJSH Java(TM) Compiler Ready.
[jsh ]$ ? Built-in commands:
class + Start defining a new class in current package def [ [ | -] | - ] Define a field that persists across statements feedback [ on | off ] Control display of feedbacks fields Show all fields currently defined import .* | static .* Add an import imports Show all imports in current import list lineNo [ on | off ] Control display of line numbers msg | + Leave a message msgs Show all messages in demo shell 'msgs' list package [ ] Specify current package preview [ on | off ] Control display of previews redef [ [ | -] | - ] Redefine a field that persists across statements undef Undefine an existing field unimport | * Remove specified import
Type in: help | ? [] for more details of a built-in command.
The following built-in methods: void javap(Class c); void javap(Class c,int depth); void javap(Object o); void javap(Object o,int depth); can be used to examine the structure of any Java class (of an Java object), where specifies how many classes up the inheritance hierarchy should be printed, 0 for infinite.
Type in: javap(this); to see all built-in methods and fields listed.
[jsh ]$ fields // builtin protected java.util.Map<java.lang.String, java.lang.String> env; //= {USER=sjsh, SSH_CLIENT=127 ... // builtin protected java.io.PrintWriter out; //= java.io.PrintWriter@cfad31 // builtin protected java.io.PrintWriter err; //= java.io.PrintWriter@73a5d3 // All Messages protected java.util.Vector<java.lang.String> msgs; //= []
[jsh ]$ javap(this); public class $JSH$ --- Constructors:
- public $JSH$() --- Methods:
- public void run() --- Fields:
- protected java.util.Vector<java.lang.String> msgs extends public abstract class sjsh.jsh.JSHRunner implements java.lang.Runnable --- Constructors:
- public JSHRunner() --- Methods:
- public java.lang.String getEnv(java.lang.String)
- public void javap(java.lang.Object,int)
- public void javap(java.lang.Class)
- public void javap(java.lang.Object)
- public void javap(java.lang.Class,int)
- public void println(java.lang.Object)
- public void println()
- public void print(java.lang.Object)
- public java.io.PrintWriter printf(java.lang.String,java.lang.Object[]) --- Fields:
- protected java.util.Map<java.lang.String, java.lang.String> env
- protected java.io.PrintWriter out
- protected java.io.PrintWriter err
[jsh ]$ for(Map.Entry<Object,Object> en : System.getProperties().entrySet()) 002: printf("%s = %s\n", en.getKey(), en.getValue()); java.runtime.name = Java(TM) SE Runtime Environment sun.boot.library.path = C:\Java\jdk1.6.0_02\jre\bin java.vm.version = 1.6.0_02-b05 ant.library.dir = D:\apps\apache-ant-1.7.0\lib java.vm.vendor = Sun Microsystems Inc. ..... java.vendor.url.bug = http://java.sun.com/cgi-bin/bugreport.cgi sun.io.unicode.encoding = UnicodeLittle sun.cpu.endian = little sun.desktop = windows sun.cpu.isalist =
[jsh ]$