3

I have successfully use JSch library to create a SSH connection to a server, but I have trouble figuring out how to add the subsystem NETCONF to SSH connection.

When doing it manually, the command line that establishes SSH connection with sybsystem NETCONF is ssh -p 4444 nerconf@myserver -s netconf.

How do I add the option -s netconf to the SSH connection using JSch? Does JSch support subsystem for NETCONF?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992

2 Answers2

3

JSch supports SSH subsystems in general but does not implement anything NETCONF specific (this is not necessary).

All you need to do is to make the following calls (pseudo-code):

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();

For NETCONF, the only requirement that the subsystem has to fulfill is a proper subsystem name.

predi
  • 5,528
  • 32
  • 60
-3

Thanks, predi.

This is work for me. netconf-hello is done.

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
  • That's the same code @predi has posted already. + Do not ever suggest anyone to set `StrictHostKeyChecking=no`, without explaining the security consequences! – Martin Prikryl Mar 20 '21 at 08:49