1

オフィス内のさまざまな HP-ux および Linux サーバーのシステム パラメータを監視するために使用する JSP Web アプリケーションを開発しています。jsch を使用して、リモート システムでコマンドを実行しています。私の問題は、のようなコマンドに対して jsch が null を返すことですntpq -p

コマンドを使用して、システムが同期されているサーバーを取得しようとしていますntpq -p | tail -1 | awk '{ print $1 }'。しかし、それはnullを返します。しかし、次のようなコマンド mpstat | tail -1 | awk '{ print $12 }'は正常に機能し、結果を返します。誰かが私が間違っていることと、もしあれば可能な回避策を教えてください...?

これは私がこの目的のために使用しているコードです!

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;

}
4

3 に答える 3

0

時刻同期は 2 つの方法で行われます。スラムまたはスルー。次に、サーバーから受信したものに基づいて、タイム クライアントに時刻を設定します。ただし、一部のアプリケーションは突然の時間変更を処理できません。そのため、スルーする必要があります(徐々に修正に変更します)。

スラムするには、ntpdate をコマンドとして使用する必要があります。

解決するには、ntpd (または xntpd) と呼ばれるデーモンを使用する必要があります。これを実行するには、ntp.conf を構成server dns/ipし、conf ファイルに含める必要があります。そして、ntpd (または xntpd) をロードします。

旋回中は補正に時間がかかるため、時刻補正の経過をたどるにはntpqコマンド(ntp query)を利用する必要があります。同期に ntpq を使用しないでください。

ntpq はコマンド ライン プロンプト ツールです。ステータスを取得するには、ntpq コマンドを実行する必要があります。詳細なガイダンスと詳細については、こちらを使用してください

私は Netware 用に ntp を開発しましたが、ntp は常にオープン ソース プロジェクトであったため、同じコマンドがすべての UNIX フレーバーにも有効です。

于 2012-12-13T05:31:06.253 に答える
0

これは私が数年以来使用している正確なコードです。試してみることができますが、それを機能させる可能性のある大きな違いは見られません。

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();
}
于 2012-12-14T18:21:53.597 に答える
0

which ntpqJava アプリケーションからの " " の出力と、既知の ssh クライアント (パテなど) から" which ntpq " を実際に実行して比較することができます。

両方が異なるバイナリを指している場合は、Java アプリケーションでフル パスを指定する必要があります。

于 2014-07-25T14:01:59.407 に答える