3 Star 7 Fork 5

seth.yang / dreamwork-sshd

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

dreamwork-sshd

介绍

使用 apache sshd 框架,提供一个可用的 sshd 服务器端半成品。快速开发基于 ssh 的 CLI应用程序。

软件架构

  • 底层采用 apache sshd 框架来提供 SSH 网络服务。

  • 内置一个实现了

    • RFC 318 (TELNET),
    • RFC 854 – RFC 861 (TELNET sub-options)
    • RFC 1073 (TELNET Window Size)

    telnet协议栈的 java 实现

  • 扩展 org.dreamwork.telnet.command.Command 以快速开发命令来实现功能

使用说明

最简单的例子:

package org.dreamwork.sshd.example;

import org.dreamwork.config.IConfiguration;
import org.dreamwork.config.PropertyConfiguration;
import org.dreamwork.network.sshd.Sshd;

import java.util.Properties;

public class BasicSshdApplication {
    public static void main (String[] args) throws Exception {
        IConfiguration conf = new PropertyConfiguration (new Properties ());

        Sshd sshd = new Sshd (conf);
        sshd.init (null);
        sshd.bind ();
    }
}

然后通过命令

ssh -p 50022 root@127.0.0.1

来登录服务,root的默认密码是123456。您可以在登录到sshd服务后使用passwd命令来修改root的密码

配置 sshd 服务

  • sshd 服务默认监听 50022 端口
  • ca 文件的默认存储位置是 ${user.home}/.ssh-server/known-hosts
  • sqlite 数据库文件的默认存储位置是 ${user.home}/.ssh-server/database.db

您可以通过配置来修改这些参数

  • 键值 service.sshd.port 用来修改监听端口
  • 键值 service.sshd.cert.file 用来修改 ca 文件的存放路径
  • 键值 database.file 用来修改 sqlite 数据库文件的存放路径
  • 键值 default.root.password 用来修改默认的root密码

以上所有键值都可通过添加系统属性来覆盖默认值

内置数据库

dreamwork sshd 采用数据库来存储 用户数据 及 系统配置参数

org.dreamwork.network.sshd.Sshd 通过 init (IDatabase) 方法传入一个数据库对象,当该方法传参为 null 时,内部将创建一个 sqlite3 文件数据库。当您希望 sshd-server 和您的应用共用一个数据库时,init方法的参数必须是一个 非空 的数据库对象。

命令

命令为sshd-server提供一组特定功能的人机接口,通过在 org.dreamwork.network.sshd.Sshd上注册 org.dreamwork.telnet.command.Command的实现类来提供服务。

内置命令

在上面的基础实现中,sshd-server 内置了一组命令,您可以登陆服务后通过命令 help来查看:

console> h
clear            clear the screen and home cursor
echo             shows something
env              show environment
exit             alias for command 'quit'
help        h    show this help list
history          show history
passwd           change current or spec user's password
quit        q    exit
set              setting console env
shutdown         shutdown the sshd server
unset            unset a console env
user             user management

扩展自定义命令

通过扩展 org.dreamwork.telnet.command.Command抽象类来自定义命令。

一个简单的自定义命令

我们来实现一个简单的命令:datetime,来实现显示当前时间

package org.dreamwork.sshd.example;

import org.dreamwork.telnet.Console;
import org.dreamwork.telnet.command.Command;

import java.io.IOException;
import java.text.SimpleDateFormat;

public class DateTimeCommand extends Command {
    private SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
    public DateTimeCommand () {
        super ("datetime", null, "show current date time");
    }

    /**
     * 执行命令
     *
     * @param console 当前控制台
     * @throws IOException io exception
     */
    @Override
    public void perform (Console console) throws IOException {
        console.println (sdf.format (System.currentTimeMillis ()));
    }
}
  • 构造函数:原型为 Command (String name, String alias, String description),其中:
    • name:指定命令的名称,以后在控制台输入的主命令就是这个
    • alias:命令的别名,可以为命令提供一个简短的别名
    • description:命令的简短描述,内置命令help将使用这个描述
  • 命令处理函数:签名为void perform (Console console) throws IOException,其中:
    • console:即控制台对象,可以打印命令处理结果

现在,我们可以将这个命令注册到sshd-server中了

public class BasicSshdApplication {
    public static void main (String[] args) throws Exception {
        IConfiguration conf = new PropertyConfiguration (new Properties ());

        Sshd sshd = new Sshd (conf);
        sshd.init (null);

        // 注册 DateTimeCommand
        sshd.registerCommands (new DateTimeCommand ());

        sshd.bind ();
    }
}

再次启动程序,并使用 ssh 命令登录到付,键入 help 命令,查看

console> h
clear            clear the screen and home cursor
datetime         show current date time
echo             shows something
env              show environment
exit             alias for command 'quit'
help        h    show this help list
history          show history
passwd           change current or spec user's password
quit        q    exit
set              setting console env
shutdown         shutdown the sshd server
unset            unset a console env
user             user management

多了一个命令

datetime         show current date time

执行命令

console> datetime
2020-02-25 19:37:41
console>

深入sshd-serverCommand

更高级/深入的内容请参见 "[sshd-server 使用手册]"

参与贡献

  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request
MIT License Copyright (c) 2020 seth.yang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

使用 apache sshd 框架,提供一个可用的 sshd 服务器端半成品。快速开发基于 ssh 的 CLI应用程序 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/seth_yang/dreamwork-sshd.git
git@gitee.com:seth_yang/dreamwork-sshd.git
seth_yang
dreamwork-sshd
dreamwork-sshd
master

搜索帮助

14c37bed 8189591 565d56ea 8189591