0

我开发了一个 web 应用程序(部署在 weblogic 服务器上),我想连接到 solaris 服务器并使用特定的 unix 用户执行 shell 脚本。目前,该脚本以 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 上编写或打开它们,你需要在文件上运行一个 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 回答