0

私は Web アプリケーション (weblogic サーバーにデプロイ) を開発しました。solaris サーバーに接続し、特定の UNIX ユーザーでシェルスクリプトを実行したいと考えています。現在、スクリプトは wls ユーザーで実行されます。ここに私のコードの一部があります:

String CLA="-d";
out.println("Stopping ASAP for the changes to reflect ...");
                        ProcessBuilder processBuilder = new ProcessBuilder("/bin/ksh","/apps/vpn/asap/scripts/stop_asap_sys_tool"+" "+CLA);
                        process = processBuilder.start();
                        InputStream is = process.getInputStream();
                        InputStream isErr = process.getErrorStream();
                        InputStreamReader isr = new InputStreamReader(is);
                        InputStreamReader isrErr = new InputStreamReader(isErr);
                        BufferedReader br = new BufferedReader(isr);
                        BufferedReader brErr = new BufferedReader(isrErr);
                        String line;
                        String lineErr;

                        while ((line = br.readLine()) != null) {
                            System.out.println(line);
                        }
                        while ((lineErr = brErr.readLine()) != null) {
                            System.out.println(lineErr);
                        }

私の検索結果は、Jsch の使用を提案しています。Jschを使用した私の実装に関する例を教えてください。またはそれを行う他の方法?!

ありがとう、バビン

4

2 に答える 2

1

Jschは良い方法です。あなたがやろうとしていることを支援するものがあります:

  • リモート実行をカバーするメイン サイトの例
    [クリック]
  • また、StackOverflow ですでに作成されているコードもここにあります[クリック]

アドバイスとして、スクリプトを実行し、Windows で作成したり、Windows で開いたりした場合は、ファイルに対して dos2unix を実行する必要があります (Linux で実行する場合)。そうしないと、リモート実行がひどく失敗します。

于 2013-01-25T07:52:26.463 に答える
0

私はこれがあなたを助けることができると思います

/**
 * This method allows you to send and execute *nix command through SSH
 * to specified removed host and returns command output, in case incorrect
 * command will return command output error
 * 
 * @param user - ssh user name for login
 * @param password - ssh password for login
 * @param host - ip or domain with ssh server
 * @param command - command to execute 
 * @return string with command output or output error
 * @author Roman Kukharuk
 */

public String execNixComAndGetRez(String user, String password, String host,
        String command) {

    int port = 22;
    String rez = "+!";

    try {
        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, port);
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no");

        // System.out.println("Establishing Connection...");
        session.connect();

        // System.out.println("Connection established.");
        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command); //setting command

        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;
                // System.out.print(new String(tmp, 0, i));
                rez = new String(tmp, 0, i);
            }
            if (channel.isClosed()) {
                // System.out.println("exit-status: "+channel.getExitStatus());
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                rez = e.toString();
            }
        }
        channel.disconnect();
        session.disconnect();
    }

    catch (Exception e) {
        rez = e.toString();
    }
    return rez;
}
于 2013-11-14T09:52:40.130 に答える