1

I am developing a JSP web application that will be used to monitor the system parameters of different HP-ux and Linux servers in my office. I am using jsch to execute commands on the remote systems. My problem is jsch is returnig null for commands like ntpq -p.

I am trying to get the server to which the system is synced using the command ntpq -p | tail -1 | awk '{ print $1 }'. But its returning null. But commands like mpstat | tail -1 | awk '{ print $12 }' is working fine and returnig result. Can anyone please tell me what i am doing wrong and any work around possible if any...?

This is the code which I am using for this purpose!

package com.bpcl.sysmon;
import java.io.IOException;
import java.io.InputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;

public class Sys 
{
String line;
String host="**.**.**.***";         
String user="******";         
String password="***********";         
String result = null;


public String getdata(String command1) throws IOException
{


    try
    {      
        result = null;
        java.util.Properties config = new java.util.Properties();              
        config.put("StrictHostKeyChecking", "no");             
        JSch jsch = new JSch();             
        com.jcraft.jsch.Session session=jsch.getSession(user, host, 22);             
        session.setPassword(password);             
        session.setConfig(config);             
        session.connect();             

        Channel channel=session.openChannel("exec");   
        ((ChannelExec) channel).setCommand(command1);       
        channel.setInputStream(null);             
        ((ChannelExec)channel).setErrStream(System.err);                           
        InputStream in=channel.getInputStream();             
        channel.connect();             
        byte[] tmp=new byte[1024];             
        while(true)
        {               
            while(in.available()>0){                 
                int i=in.read(tmp, 0, 1024);                 
                if(i<0)break;                 
                result = (new String(tmp, 0, i));               
                }              
            if(channel.isClosed())
            {                
                System.out.println("exit-status: "+channel.getExitStatus());                
                break;               
                }               
            try
            {
                Thread.sleep(1000);}catch(Exception ee){}            
                }             
        channel.disconnect();             
        session.disconnect();             
        System.out.println("DONE");         
        }
    catch(Exception e)
    {             
        e.printStackTrace();         
    } 

    return result;

 } 



 public String getcpu(boolean linux)
{  

    String res1 = null;
    try {
        if(linux)
        res1 = getdata("mpstat | tail -1 | awk '{ print $12 }'");

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return res1;
}

public String getntp(boolean linux)
{  
    String res1 = null;
    try {
        if(linux)
        res1 = getdata("ntpq -p | tail -1 | awk '{ print $1}'");

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return res1;

}
Cruncher
  • 7,641
  • 1
  • 31
  • 65
varun
  • 341
  • 2
  • 17
  • Can you just specify what is returning null ? (variable) please. – Alexandre Lavoie Dec 13 '12 at 05:23
  • the function getntp(boolean ) is returning null. It is getting the value from the function getdata(string) where variable "result" is initialised to "null". It is returning null since the command is not executed properly by jsch. – varun Dec 13 '12 at 08:37
  • I've compared your code with mine that I'm currently using and tested your commands and they are working. Your code seem almost exactly the same. You can verify in the ssh log that it is properly connecting, also test for example "whoami". – Alexandre Lavoie Dec 13 '12 at 14:03
  • any light on what i might be doing wrong..? I also checked the ssh logs and its getting connected properly...! – varun Dec 14 '12 at 08:32

3 Answers3

0

Time Synchronization is done in 2 ways. Slam or the Slew. Then you slam you set the time on your time client based on what it receives from the server. But some applications cant handle sudden time changes. So you need to slew (gradually change it to correction).

To slam you need to use ntpdate as command.

To slew you need to use a daemon called ntpd (or xntpd). To run this you need to configure ntp.conf, and have server dns/ip in the conf file. And then load ntpd (or xntpd).

When it is slewing it takes time to correct, so to trace the progress of the time correction you need to use ntpq command (ntp query). Dont use ntpq for synchronization.

ntpq is a command line prompt tool. To get the status you need to run a ntpq commands. For more guidance and details use here

I developed ntp for Netware, and the same commands hold good for all unix flavors too, since ntp has always been a open source project.

Siddharth
  • 9,349
  • 16
  • 86
  • 148
  • thank you sidharth for your reply. But i dont really want to synchronise the servers. i just want to get execute the command ntpq -p | tail -1 | awk '{ print $1 }' on the remote server and get the result. Currently its not working for me. Any work around for this..? – varun Dec 13 '12 at 08:41
  • -p lists the peers. every peer makes 8 sync calculations over 2-5 mins and finally uses the best samples to sync the time. So what are you trying to do here ? Are you trying to find out if the servers are in sync ? Whats your intent ? – Siddharth Dec 13 '12 at 17:24
  • yes...my intent is to find whether the server is in sync and if yes to which server it is synced to...! – varun Dec 14 '12 at 04:08
0

Here is the exact code I use since couple of years, you can get a try but I see no major differences that could make it working and yours not :

private static final String URL = "user@yourmachine.com";
private static final String PASSWORD = "password";

public static String getQueryShell(String p_sCommand)
{
    StringBuilder sbResponse = null;

    try
    {
        JSch jsch=new JSch();  

        String host = URL;
        String user=host.substring(0, host.indexOf('@'));
        host = host.substring(host.indexOf('@')+1);
        String password = PASSWORD;

        Session session=jsch.getSession(user,host,22);

        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking","no");
        session.setConfig(config);
        // username and password will be given via UserInfo interface.
        session.setPassword(password);
        session.connect();

        Channel channel=session.openChannel("exec");
        ((ChannelExec)channel).setCommand(p_sCommand);

        channel.setInputStream(null);

        ((ChannelExec)channel).setErrStream(System.err);

        InputStream in=channel.getInputStream();

        channel.connect();

        byte[] tmp=new byte[1024];

        sbResponse = new StringBuilder();

        while(true)
        {
            while(in.available()>0)
            {
                int i=in.read(tmp, 0, 1024);

                if(i<0)break;

                sbResponse.append(new String(tmp, 0, i));
            }
            if(channel.isClosed())
            {
                //System.out.println("exit-status: " + channel.getExitStatus());
                break;
            }

            try
            {
                Thread.sleep(1000);
            }
            catch(Exception ee)
            {

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

    return sbResponse.toString();
}
Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
  • Thank you Alexandre, i will try out your code. let us see if it makes any difference..:-) – varun Dec 19 '12 at 05:44
0

You may compare the output of "which ntpq" from your java application and by actually running "which ntpq" from known ssh client(like putty).

if both point to different binaries, you have to specify full path in your java application.

n32
  • 25
  • 1
  • 4