coder
coder

Reputation: 1079

Accessing JMX in production via SSH tunnel using JSch

I am trying to automate a step of doing some things with JMX.

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

Earlier, i used putty or ssh to create tunnel and run my java program locally. Since we used 1-2 host, it was easier. now it became upto 10 host. now that i dont want to create tunnel every time and disconnect and run the program.

what i wanted to do is, automatically create SSH tunnel using JSch and connect JMX with the java program. i tried to do this but its not working.

I am getting java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: java.net.ConnectException: Connection refused: connect

it is possible to do this?

Upvotes: 3

Views: 1213

Answers (1)

thejh
thejh

Reputation: 45568

Why don't you use Runtime.exec() to start ssh? 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();
  }
}

Upvotes: 1

Related Questions