Run program in remote computer over ssh via Java (JSch)

Viewed 130

I am writing a block of Java code that allows me to connect to a remote computer and execute a program on it. So far I am able to connect to the program and run it. I can see the program's output on my console. However, the program stops executing and my java code exists with exit code = 0. Here is my connect method. I call it in my main with the correct arguments.

    public void listFolderStructure(String username,
                                       String host, int port, String command) throws Exception {

    Session session = null;
    ChannelExec channel = null;

    try {
        JSch jsch = new JSch();
        session = jsch.getSession(username, host, port);
        session.setConfig("PreferredAuthentications", "publickey");
        jsch.addIdentity("~/.ssh/id_rsa");
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect();

        channel = (ChannelExec) session.openChannel("exec");


        channel.setXForwarding(true);
        channel.setCommand(command);
        ByteArrayOutputStream responseStream = new ByteArrayOutputStream();
        channel.setOutputStream(responseStream);
        channel.connect();

        while (channel.isConnected()) {
            Thread.sleep(100);
        }

        String responseString = new String(responseStream.toByteArray());
        System.out.println(responseString);
    } finally {
        if (session != null) {
            session.disconnect();
        }
        if (channel != null) {
            channel.disconnect();
        }
    }
}

And this is part the output of the program on my console. Note: Connecting to the remote via ssh and running the program from a terminal gives the same output but the program's gui window displays on my computer and it runs as per usual

2021-04-03 20:36:42 [INFO]: Loaded plugin: 
"/home/usr/adtf3.7/bin/adtf_kernel.adtfplugin" [runtime.cpp(1887)]
2021-04-03 20:36:42 [INFO]:     Registered class "kernel.service.adtf.cid". ( 14 ) 
[runtime.cpp(2213)]
2021-04-03 20:36:42 [INFO]: Try to load 
"/home/usr/adtf3.7/bin/adtf_playback.adtfplugin" [runtime.cpp(1784)]
2021-04-03 20:36:42 [INFO]: Loaded plugin: 
"/home/usr/adtf3.7/bin/adtf_playback.adtfplugin" [runtime.cpp(1887)]
2021-04-03 20:36:42 [INFO]:     Registered class "playback.service.adtf.cid". ( 15 ) [

 Process finished with exit code 0

Is there a way to connect to the remote via Jsch and have the program's ui display as it normally would without it exiting? (code 0) Any help would be greatly appreciated

EDIT: Thank you @Martin Prikryl; one of his suggestions helped out and I can now run the program on the remote. The only issue I'm facing now is getting the UI to display on my machine instead of the remote machine

Method that can run programs on the remote:

    public void run_command(String command) throws JSchException {
    String username = this.user;
    String host = this.host;
    int port = this.port;

    JSch jsch = new JSch();
    jsch.addIdentity("~/.ssh/id_rsa");

    this.sshSession = jsch.getSession(username, host, port);
    this.sshSession.setConfig("PreferredAuthentications", "publickey");
    this.sshSession.setConfig("StrictHostKeyChecking", "no");
    this.sshSession.connect();


    this.sshChannel = this.sshSession.openChannel("shell");
    this.sshChannel.setXForwarding(true);

    ByteArrayInputStream reader = new ByteArrayInputStream(("export DISPLAY=:0; "+command + " \n").getBytes());
    this.sshChannel.setInputStream(reader);
    this.sshChannel.setOutputStream(System.out);
    this.sshChannel.connect();
    try {
        Thread.sleep(1000); // give GUI time to come up
    } catch (InterruptedException ex) {
        // print message
    }
    this.sshChannel.disconnect();
}

alternatively this can be used to connect to a remote without Jsch. Extending to command however; i.e: ..this.host;gedit makes the terminal instantly dissapear.

public void open_remote_terminal() {
    try {
        String command ="gnome-terminal -x ssh -x "+this.user+"@"+this.host;
        Runtime.getRuntime().exec(command);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
0 Answers
Related