1

In our project, we need to get a file from external SFTP server using public key authentication. I have searched online but didn't any example for mentioning proxy server routing since it's required as per company's policy

We are using the following command on Unix server to get the file but we want to use java program to implement this functionality

sftp -v -oIdentityFile=/home/intusr/.ssh/id_rsa -oProxyCommand="/usr/bin/corkscrew 11.555.66.22 4444 %h %p" user@transmit.com:stage/filedir/

Your help will be appreciated. Best resource I have found so far is
https://kodehelp.com/java-program-for-uploading-file-to-sftp-server/

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Balaji
  • 13
  • 1
  • 3

1 Answers1

1

The most commonly used Java SSH library is JSch, which supports both public key authentication and HTTP proxy:

Combined, the code would be like:

JSch jsch = new JSch();
jsch.addIdentity("/path/to/private/key");
Session session = jsch.getSession("user", "host");
ProxyHTTP  proxy = new ProxyHTTP("proxy", proxyport)
proxy.setUserPasswd("proxyusername", "proxypassword");
session.setProxy(proxy);
session.connect();

For downloading a file, see:
How to retrieve a file from a server via SFTP?

You will have to verify server host key as well.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992