1

I have config file under ~/.ssh/config contains :

Host jumbHost
   HostName x.domain.com

Host host1
   HostName y1.domain.com
   ProxyJump  jumbHost

How could I use JSch to tunnel to x.domain.com server by host1 in order to execute commands ?

Note: y1.domain.com doesn't have public IP

I tried to use jsch.setConfigRepository(config_file_path) but it seems there are more config I need to set or methods to use.

It will be very useful if described using example.

EDIT: So I can ssh to x.domain.com server when I get session using jsch.getSession(user,"host1"); or something like that

Thanks in advance

user
  • 21
  • 3
  • 1
    Unless JSch .54 made enhancements, it does not support the ProxyJump configuration. You can manually set the proxy (see the [JumpHosts example](http://www.jcraft.com/jsch/examples/JumpHosts.java.html)), and there was a github project that I cannot find that attempted to automate the process (though it didn't solve my use case, if you can find that project, it might help). Also, see the discussion here: https://stackoverflow.com/questions/21567031/how-to-use-jsch-with-proxycommands-for-portforwarding – KevinO Mar 20 '19 at 00:59
  • Ah, here was the github project: https://github.com/cronn-de/ssh-proxy – KevinO Mar 20 '19 at 01:00
  • See also https://stackoverflow.com/q/48403041/850848 – Martin Prikryl Mar 20 '19 at 07:03

1 Answers1

1

Thanks KevinO and Martin Prikryl for your value tips and help

Based on your descriptions and links I was able to apply needed config to enable access to x.domain.com using host1 name and then execute commands on y1.domain.com

For record, this is how I end up with

public class Main {
public static void main(String args[]) throws Exception {
    try {
        String user = "user_name";
        String host = "host1";
        String command = "hostname";

        JSch jsch = new JSch();
        ConfigRepository configRepository = OpenSSHConfig.parseFile(System.getProperty("user.home") + "/.ssh/config");
        jsch.setConfigRepository(configRepository);
        jsch.addIdentity(new File(System.getProperty("user.home") + "/.ssh/id_rsa").getAbsolutePath());

        Session sessionProxy = jsch.getSession(user,configRepository.getConfig(host).getValue("ProxyJump"),22);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        sessionProxy.setConfig(config);
        sessionProxy.connect();

        int assignedPort = sessionProxy.setPortForwardingL(0, configRepository.getConfig(host).getHostname(), 22);

        Session sessionTunnel = jsch.getSession(user, "127.0.0.1", assignedPort);
        sessionTunnel.connect();

        Channel channel = sessionTunnel.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);
        channel.setInputStream(null);
        ((ChannelExec) channel).setErrStream(System.err);
        InputStream input = channel.getInputStream();
        channel.connect();

        try {
            InputStreamReader inputReader = new InputStreamReader(input);
            BufferedReader bufferedReader = new BufferedReader(inputReader);
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(">>> output: " + line);
            }
            bufferedReader.close();
            inputReader.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        channel.disconnect();
        sessionTunnel.disconnect();
        sessionProxy.disconnect();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
} 
}
user
  • 21
  • 3