Access JMX in production through an SSH tunnel using JSch

I am trying to automate the execution step of some actions using JMX.

It works for a development environment. but when it comes to Production, which is protected behind a firewall, I need to create an SSH tunnel, and then only I can access the JMX console.

I used to use putty or ssh to create a tunnel and run my java program locally. Since we used 1-2 hosts, it was easier. Now it has become up to 10 hosts. Now that I don’t want to create a tunnel every time and turn off and start the program.

what I wanted to do was automatically create an SSH tunnel using JSch and connect JMX using a java program. I tried to do this, but did not work.

I get java.rmi.ConnectException: connection refused host: localhost; Nested exception: java.net.ConnectException: connection rejected: connect

It can be done?

+3
source share
1 answer

Why aren't you using Runtime.exec()ssh to run? Example:

public static void main(String[] args) {
  String[][] data = new String[][]{new String[]{"user@server1", "2000:server1:30"},
          new String[]{"user2@server4", "2000:server4:30"}};
  Process[] processes = new Process[data.length];
  for (int i=0; i<data.length; i++) {
    processes[i] = Runtime.getRuntime().exec("ssh", data[i][0], "-L", data[i][1], "-N");
  }
  //do something else, for example, wait for user interaction here
  for (int i=0; i<data.length; i++) {
    processes[i].destroy();
  }
}
+1
source

Source: https://habr.com/ru/post/1772509/


All Articles