【问题标题】:How to use JSch to establish a SSH connection with subsystem NETCONF如何使用JSch与子系统NETCONF建立SSH连接
【发布时间】:2018-08-30 01:20:40
【问题描述】:

我已成功使用 JSch 库创建到服务器的 SSH 连接,但我无法弄清楚如何将子系统 NETCONF 添加到 SSH 连接。

手动时,与sybsystem NETCONF建立SSH连接的命令行是ssh -p 4444 nerconf@myserver -s netconf

如何使用 JSch 将选项 -s netconf 添加到 SSH 连接? JSch 是否支持 NETCONF 的子系统?

【问题讨论】:

    标签: java ssh jsch ietf-netconf


    【解决方案1】:

    JSch 通常支持 SSH 子系统,但不实现任何特定于 NETCONF 的内容(这不是必需的)。

    您需要做的就是进行以下调用(伪代码):

    com.jcraft.jsch.JSch ssh = new com.jcraft.jsch.JSch();
    
    com.jcraft.jsch.Session session = ssh.getSession(username, host, port);
    
    session.setUserInfo(myUserInfo); // authentication
    
    session.connect(connectTimeout);
    
    // this opens up the proper subsystem for NETCONF
    com.jcraft.jsch.ChannelSubsystem subsystem = (com.jcraft.jsch.ChannelSubsystem) session.openChannel("subsystem");
    subsystem.setSubsystem("netconf");
    
    // at this point you may get your streams
    subsystem.getInputStream();
    subsystem.getErrStream();
    subsystem.getOutputStream();
    
    subsystem.connect();
    

    对于 NETCONF,子系统必须满足的唯一要求是正确的子系统名称。

    【讨论】:

      【解决方案2】:

      谢谢,普雷迪。

      这对我有用。 netconf-hello 完成了。

      session = new JSch().getSession("username", "remote-ip", netconf-port);
      session.setPassword("your-password");
      session.setConfig("StrictHostKeyChecking", "no");
      session.connect();
      
      channel = (ChannelSubsystem) session.openChannel("subsystem"); //necessary
      channel.setSubsystem("netconf"); //necessary
      channel.connect();
      System.out.println(channel.isConnected()); // debug use
      System.out.println(session.isConnected()); // debug use
      
      
      InputStream inputStream = channel.getInputStream(); // use this to read
      OutputStream outputStream = channel.getOutputStream();
      PrintStream printStream = new PrintStream(outputStream); // use this to send
      

      【讨论】:

      • 这与@predi 已经发布的代码相同。 + 永远不要建议任何人设置StrictHostKeyChecking=no,而不解释安全后果!
      猜你喜欢
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      • 2018-06-06
      • 1970-01-01
      • 2015-05-05
      相关资源
      最近更新 更多