1

我在通过 jsch 运行 java 推荐时遇到问题。如果我通过 putty 在 SSH 会话中执行此 java 命令,它可以正常工作,但在我的代码中执行时返回退出状态 127。

命令是这样的:

sshCommManager.sendCommand("cmd /c java -Xms256M -Xmx1024M -jar FileCatalystTester.jar -basic /Y");

sendCommand 函数是 exec 示例的派生。这是我写的整个 SSHCommManager:

public class SSHCommManager extends Observable{

    private JSch jsch; //ssh library
    private static String user = "what";
    private static String password = "youwhat";
    //private static String host = "192.168.1.1";
    private static SSHCommManager sshCommManager;
    private Session session;
    private Channel channel;
    public boolean commsConnected = false;
    private int aPort = 22;

    private SSHCommManager() {

        this.addObserver(MainUI.getInstance());
    }

    public static SSHCommManager getInstance() {
        if (sshCommManager == null) {
            sshCommManager = new SSHCommManager();
        }
        return sshCommManager;
    }

    public void init(String aHost) {

        try {
            jsch = new JSch();

            //System.out.println("Getting ssh session...");
            session = jsch.getSession(user, aHost, aPort);
            session.setX11Host(aHost);
            session.setX11Port(aPort + 6000);

            //System.out.println("Getting user info...");
            session.setPassword(password);

            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);

            System.out.println("Connecting to ssh...");
            session.connect(30000);

            if (session.isConnected()) {
                commsConnected = true;
            } else {
                commsConnected = false;
            }

            System.out.println(commsConnected);

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

    }


    public void sendCommandTest(String aCommand) {
        try {
            Channel channel1=session.openChannel("shell");//only shell 
            System.out.println("Sending Test command: "+ aCommand);
            channel1.setOutputStream(System.out);
            PrintStream shellStream = new PrintStream(channel1.getOutputStream());  // printStream for convenience
            channel1.connect();
            shellStream.println(aCommand);
        } catch (Exception e) {
            System.out.println(e);
        }

        if (channel.isClosed()) {
            System.out.println("exit-status: "
            + channel.getExitStatus());
        }
    }

    public String sendCommand(String aCommand){

        InputStream in = null;
        OutputStream out = null;
        StringBuilder commandOut = new StringBuilder();

        try {

            channel = session.openChannel("exec");
            System.out.println("Sending command: " + aCommand);
            ((ChannelExec) channel).setCommand(aCommand);
            //channel.setInputStream(System.in);
            channel.setInputStream(null);
            //channel.setOutputStream(System.out);
            ((ChannelExec) channel).setErrStream(System.err);

            in = channel.getInputStream();
            //out = channel.getOutputStream();

            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));
                    //System.out.println(channel.getInputStream().toString());
                    commandOut.append(new String(tmp, 0, i));

                    //setChanged();
                    //notifyObservers(System.err.toString() + "\n");
                }
                if (channel.isClosed()) {
                    System.out.println("exit-status: "
                    + channel.getExitStatus());
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (Exception ee) {
                    throw new JSchException("Cannot execute remote command: " + aCommand + " : " + ee.getMessage());
                }
            }

            //channel.disconnect();
            //session.disconnect();

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

        return commandOut.toString();

    }

    public void cleanupSSH() {

        channel.disconnect();
        session.disconnect();
    }

    public boolean isCommsConnected() {
        return commsConnected;
    }
}

...任何想法或帮助将不胜感激,因为我无法弄清楚为什么该 java 命令不起作用。我已经尝试了一切,包括为我尝试执行的 java 和 .jar 文件放入直接路径。

4

1 回答 1

1

以下程序工作以访问远程 SSH 服务器

private String userName = "xxxx";
private String password ="xxxx";
private String hostName = "xxx.xxx.xxx.com";
private int port = 22;
private String sdstestCommand = "java -Xms256M -Xmx1024M -jar MyProgram";

public void testconnect () throws JSchException, IOException {

    JSch jsch = new JSch();
    Session session = jsch.getSession(userName, hostName, port);
    session.setConfig("StrictHostKeyChecking", "no");
    session.setPassword(password);
    session.connect();      
    ChannelExec channel = (ChannelExec) session.openChannel("exec");
    channel.setCommand(sdstestCommand);
    channel.setInputStream(null);
    ((ChannelExec) channel).setErrStream(System.err);
    InputStream in = channel.getInputStream();
    channel.connect();
    System.out.println("Unix system connected...");
    byte[] tmp = new byte[1024];
    while (true){
        while (in.available() > 0) {
            int i = in.read(tmp, 0, 1024);
            if (i < 0) {
                break;
            }
            String line = new String(tmp, 0, i);
            System.out.println("Unix system console output: " +line);
        }
        if (channel.isClosed()){
            break;
        }
        try {
            Thread.sleep(1000);
        } catch (Exception ee){
            //ignore
        }
    }
    channel.disconnect();
    session.disconnect();       
}       
}
于 2012-05-08T15:05:19.317 回答