使用 Jsch 发出 Java 命令时遇到问题

发布于 2024-11-25 06:12:16 字数 4734 浏览 1 评论 0原文

我在通过 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 文件的直接路径。

I'm having an issue running a java commend via jsch. This java command works fine if I execute it in a SSH session via putty, but when executed in my code returns an exit status of 127.

The command is this:

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

With the sendCommand function being a derivative of the exec example. Here is the entire SSHCommManager I wrote:

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;
    }
}

...any thoughts or help would be appreciated as I cannot figure out why that java command won't work. I've tried everything, including putting in the direct paths for both java and the .jar file I'm trying to execute.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言 2024-12-02 06:12:16

以下程序可以访问远程 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();       
}       
}

Following program work to access remote SSH server

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();       
}       
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文