简体   繁体   中英

Multi-level ssh connections using java

I'm trying to run a set of scripts on a remote machine(linux) using a swing GUI on a local machine(linux) and read the commands output(as it's running) back into a text area.

The machines I'm trying to connect to are behind my schools firewall so i have to connect to that before I can connect to them.

Typical bash syntax would be something like: "ssh -t user1@host1 ssh -t user2@host2" At first I tried using the runtime() to directly evoke the above command but that doesn't seem to work at all, Or maybe I'm missing something. I get no output from the following program.

    Process dirReader = Runtime.getRuntime().exec("ssh -t user1@host1 ssh -t user2@host2");
    InputStream stdout = dirReader.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
    String line = reader.readLine();
    while(line != null){
        fileNames.add(line);
        line = reader.readLine();
    }
        reader.close();reader=null;
        stdout.close();stdout=null;
        dirReader.destroy();dirReader=null;

    } catch(IOException e){
        System.out.println("Exception: Error reading models directory");
        System.exit(1);
    }

When I run the above with something simple like "ls" I get the expected results. I suspect that some "special" treatment is necessary with ssh, so I've been spending the last few days messing around with the third party libraries. JSch and ganymed.

Single level connections are simple enough with either, but I'm afraid I'm a bit stumped on multi. You'd think there would be a simple solution, but I don't see it. A few people have suggested tunneling, but I haven't had much luck with that either.

I see there's a way in jSch to open a channel directly to "emulate" a console. That would work, but seems like overkill for what I want to do. Also having a console popped up behind my GUI is not desirable.

So to reiterate, I want to:

  1. Establish an ssh connection to a machine behind another server.
  2. Run a script on that server
  3. Pipe the stdout of that script back to my gui.

Advice on #1 would be appreciated.

I've been agonizing over this for several days. There's a couple similar threads but none really seem to answer my question. Feel free to send me links but if it's on the first 5 pages of Google; chances are I've seen it.

After many more hours of investigation I finally came across the following:

How to use jsch with ProxyCommands for portforwarding

I don't know if I'm missing something, but I've found at least 50 threads about this problem that don't have a fulfilling solution.

I guess the moral of the story is read all the examples before you start.

Direct solution: http://www.jcraft.com/jsch/examples/JumpHosts.java.html

Now to find a method to do this in Windows. Preferably without forcing the user to have an ssh client installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM