Friday, November 7, 2014

View Remote Server Console with using Java


View Remote Server Console with using Java

In the software development, the developer spend significant amount of time for debug the code and reproduce the bug for find the issues and solutions. In the JEE applications almost all the errors are identified through the servers console. The best practice, developers debug the code in their local server and try to reproduce the bug, in case if it is not possible, download the remote server logs local machines and identify the issues.

The following tutorial going to explain that, how we are going to view the remote server console in local machine with using the Java application. It may helps to developers to reduce the time for analysis.

Objective : The main objective of the tutorial is view the remote server console with using Java application.

Scope : Write a simple java code for view the server console which was run remotely.

Requirements : The below basic requirements we need to set it in order to achieve our objective.
  • Eclipse Java EE IDE 
  • jsch-0.1.50.jar. The jar you able to download from here.

Step 1 : Create a new Java project in Eclipse and name it as "ViewRemoteServerConsole".
The path of creating the new Java project is below mentioned.
                    File > New > Java Project

Step 2 : Create a "RemoteConnector" java class under the "com.findanidea.remote.connector" package.

The following class create the channel with using jsch along with given input parameters.

/ViewRemoteServerConsole/src/com/findanidea/remote/connector/RemoteConnector.java


package com.findanidea.remote.connector;

import java.io.InputStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class RemoteConnector {

 public static void main(String[] arg) {

  try {
   JSch jsch = new JSch();
   String host="",user="",passwd = null;

   if (arg.length == 3) {
    host = arg[0];
    user = arg[1];
    passwd = arg[2];
   }else{
    System.out.println("Wrong parameters... ");
    System.out.println("Please provide the host as first parameter...");
    System.out.println("Please provide the user as second parameter...");
    System.out.println("Please provide the password as third parameter...");
   }
   Session session = jsch.getSession(user, host, 22);
   session.setPassword(passwd);

   ServerDetails ui = new ServerDetails() {
    public boolean promptYesNo(String message) {
     return true;
    }
   };

   session.setUserInfo(ui);
   session.connect(3000); 

   Channel channel = session.openChannel("shell");
   channel.setInputStream(System.in);
   channel.setOutputStream(System.out);


   InputStream in = channel.getInputStream();
   channel.connect(3 * 1000);
   byte[] tmp = new byte[1024];
   while (true) {
    while (in.available() > 0) {
     int i = in.read(tmp, 0, 1024);
     if (i < 0)
      break;
     System.out.print(new String(tmp, 0, i));
    }
    if (channel.isClosed()) {
     System.out.println("Channel is Coluse : Status is : " + channel.getExitStatus());
     break;
    }
    try {
     Thread.sleep(1000);
    } catch (Exception ee) {
    }
   }

  } catch (Exception e) {
   System.out.println(e);
  }
 }

}

In the above class file expects the 3 run time input parameters. There are,
1. host (ex: 172.19.11.11)
2. username (ex : testuser)
3. password (ex : password)


Based on the above mentioned parameters the channel will create and connect to the remote server console. 

Step 3 : Create a "ServerDetails" class under the same "com.findanidea.remote.connector" package.



package com.findanidea.remote.connector;

import com.jcraft.jsch.UIKeyboardInteractive;
import com.jcraft.jsch.UserInfo;

public class ServerDetails implements UserInfo,UIKeyboardInteractive {

 @Override
 public String[] promptKeyboardInteractive(String arg0, String arg1, String arg2, String[] arg3, boolean[] arg4) {
  return null;
 }

 @Override
 public String getPassphrase() {
  return null;
 }

 @Override
 public String getPassword() {
  return null;
 }

 @Override
 public boolean promptPassphrase(String arg0) {
  return false;
 }

 @Override
 public boolean promptPassword(String arg0) {
  return false;
 }

 @Override
 public boolean promptYesNo(String arg0) {
  return false;
 }

 @Override
 public void showMessage(String arg0) {
 }

}

All right... We did it... Now the time to test the application... 


If you execute the "RemortConnector" class with the input parameters such as remote server host, username and password, you able to see the remote server console with in you application console. 



You can find here the way for set the run time parameters in eclipse.

Hope its help to you..

1 comment: