-1

In my application I have 2 requirements with 2 different remote server:

  1. I have to copy a file from localhost to Remote Server X
  2. copy a different file from Remote Server Y to localhost

I am aware that Jsch can be used like below:

JSch jsch = new JSch();

Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
jsch.addIdentity(privateKey, privateKeyPassphrase);
Session session = jsch.getSession(user, host, port);
session.setConfig(config);
session.connect(5000);

Using the above code I can connect a session for single host.How can I ge the different session to different hosts (in my case x and y) ?

Do I need the private key or public key of remote host whether I am copying to or from it ?

Regards

Manish
  • 1,274
  • 3
  • 22
  • 59
  • You should never have the private key of any other system. You normally should have the _public_ key of all hosts in your KnownHosts file, but since you have set StrictHostKeyChecking no you can get by without that. (Of course you may be connecting to fake hosts and either get falsified data or give your real data to crooks.) Since you are using a keypair to authenticate yourself, the public half of that keypair must be configured on both hosts, separately. – dave_thompson_085 Jun 06 '21 at 20:12
  • Just repeat your code with different `host`??? You are basically asking a question like, "I have a code to add `1 + 2`, but now I also want to add `2 + 3` – how do I do that?" – Martin Prikryl Jun 07 '21 at 06:53
  • @MartinPrikryl Ok I was thinking there would be a single place to configure all remote connections. anyway thanks – Manish Jun 07 '21 at 08:48

1 Answers1

0

You have to establish a connection (session) to Server X and Server Y. With JSch you can read and write in a session.

Stefan Fenn
  • 483
  • 5
  • 13
  • That's what I was asking, I know how to create session with single remote server. How to create/use multiple JSCH session – Manish Jun 07 '21 at 05:15
  • Use the following. Session session1 = jsch.getSession(user1, host1, port1); session1.setConfig(config1); session1.connect(5000); Session session2 = jsch.getSession(user2, host2, port2); session2.setConfig(config2); session2.connect(5000); – Stefan Fenn Jun 07 '21 at 06:15