简体   繁体   English

使用Java的多级SSH连接

[英]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. 我正在尝试使用本地计算机(linux)上的swing GUI在远程计算机(linux)上运行一组脚本,并将命令输出(正在运行)读回到文本区域。

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. 典型的bash语法类似于:“ ssh -t user1 @ host1 ssh -t user2 @ host2”最初,我尝试使用runtime()直接调用上述命令,但似乎根本不起作用,或者我想念一些东西。 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. 当我使用诸如“ ls”之类的简单内容运行上述命令时,我得到了预期的结果。 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. 我怀疑ssh需要一些“特殊的”处理,因此最近几天我一直在与第三方库打交道。 JSch and ganymed. JSch和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. 我看到jSch中有一种方法可以直接打开一个通道来“模拟”控制台。 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. 在我的GUI后面弹出一个控制台也是不理想的。

So to reiterate, I want to: 因此,我想重申一下:

  1. Establish an ssh connection to a machine behind another server. 建立与另一台服务器后面的计算机的ssh连接。
  2. Run a script on that server 在该服务器上运行脚本
  3. Pipe the stdout of that script back to my gui. 将那个脚本的标准输出通过管道传回我的GUI。

Advice on #1 would be appreciated. #1的建议将不胜感激。

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; 随时向我发送链接,但如果链接位于Google的前5页中; 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 如何将Jsch与ProxyCommands一起使用以进行端口转发

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. 我不知道我是否缺少任何东西,但是我发现至少有50个关于此问题的线程没有令人满意的解决方案。

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 直接解决方案: http : //www.jcraft.com/jsch/examples/JumpHosts.java.html

Now to find a method to do this in Windows. 现在找到在Windows中执行此操作的方法。 Preferably without forcing the user to have an ssh client installed. 最好不要强迫用户安装ssh客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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