How to SSH to server after another SSH server using JSch?

I need to be able to ssh from a Java program to a remote server, and from there SSH to another server. I have credentials for both servers on my client.

Commands will be automatically transmitted from the application in the form of regular strings (without user input). I need to be able to run these user commands on a second server and be able to decide which commands to execute at runtime based on the output and some simple logic.

Can I use JSch for this, and if so, where should I start learning? (Examples, information)

==================================================== ============

ADDED:

Exception in the thread "main" com.jcraft.jsch.JSchException: UnknownHostKey: host.net. RSA Key Fingerprint - "blahblahblah"

as until now, I solve this problem by modifying the known_hosts file and adding the host there manually. Can I get around this little problem with the settings somewhere, telling JSch to automatically press YES when this question is YES-NO?

+5
source share
3 answers

To connect to the second server behind the firewall, there are basically two options.

ssh ( exec), . JSch, JSch API , ssh.

TCP . JSch Wiki ProxySSH ( ), JSch JSch. ( : , JSch.)

, shell exec. (. Shell, Exec Wiki JSch , Javadocs .)


--:

( ) known_hosts. ( , , " ". , , .)

StrictHostKeyChecking - no -

JSch.setConfig("StrictHostKeyChecking", "no");

( , , . yes ask - MITM .)

( ) - UserInfo . (JSch Wiki Swing JOptionPanes, , .)

, JSch.setKnownHosts , InputStream - .

+10

SSH, , SSH/SFTP- B A.

Session sessionA = jsch.getSession("usernameA", "hostA");
// ...
sessionA.connect();

int forwardedPort = 2222; // any port number which is not in use on the local machine
sessionA.setPortForwardingL(forwardedPort, "hostB", 22);

Session sessionB = jsch.getSession("usernameB", "localhost", forwardedPort);
// ...
sessionB.connect();

// Use sessionB here for shell/exec/sftp
0

. :

 public static void sesionA(){
     try {
        Session sessionA = jSch.getSession(username, hostA);  
        Properties config = new Properties(); 
        config.put("StrictHostKeyChecking", "no");
        sessionA.setConfig(config);
        sessionA.setPassword(passwordA);
        sessionA.connect();


        if(sessionA.isConnected()) {
            System.out.println("Connected host A!");
            forwardedPort = 2222;
            sessionA.setPortForwardingL(forwardedPort, hostB, 22);      
        }

    } catch (JSchException e) {
        e.printStackTrace();
    }
 }

 public static void sesionB(){


        try {
            Session sessionB = jSch.getSession(username, "localhost", forwardedPort);

            Properties config = new Properties(); 
            config.put("StrictHostKeyChecking", "no");
            sessionB.setConfig(config);
            sessionB.setPassword(passwordB);
            sessionB.connect();

          if(sessionB.isConnected()) {
             System.out.println("Connected host B!");
          }
     }
 }
-1
source

All Articles