How to select network interface when connecting to SFTP with JSch

二次信任 提交于 2019-12-01 07:04:07

问题


I am facing problem to create a session to a remote SFTP server by JSch:

The command i use to connect the sftp server through shell is:

sftp -o BindAddress=SOME_IP_ADDRRESS myUserName@HOST_IP_ADDR

and its working fine, but when I am trying with Java (JSch) then I am getting a timeout exception. The Java code is

/* KEY_FILE_NAME = is a file with rsa public key */
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(KEY_FILE_NAME).getFile());
JSch jsch = new JSch();
jsch.addIdentity(file.getAbsolutePath());
Properties hash = new Properties();
hash.put("StrictHostKeyChecking", "no");
logger.debug("SSh Server Host name >>" + SSH_SERVER_HOST_NAME + " || User Name >>" +  SSH_SERVER_USER_NAME);

session = jsch.getSession(SSH_SERVER_USER_NAME, SSH_SERVER_HOST_NAME);
UserInfo ui = new MyUserInfo();
session.setUserInfo(ui);
session.setConfig(hash);
session.setPort(22);
session.setTimeout(45000);
session.connect();

The exception I am getting is :

com.jcraft.jsch.JSchException: timeout: socket is not established

I guess its coming as I am not setting the BindAddress with -o option which I am using while I am using the shell command. If that is the case then how to set the -o BindAddress parameter in JSch? Or am I doing something wrong, please suggest.


回答1:


I do not think there's an explicit support for selecting a network interface to connect from in JSch.

But you can implement a SocketFactory interface, such as that it creates a Socket instance using the constructor overload with the localAddr:

public Socket(InetAddress address, int port, InetAddress localAddr, int localPort)

(use 0 for the localPort)

Then pass the factory to the Session.setSocketFactory() before calling the connect() and you should be good.




回答2:


I have implemented the solution , I added the following code before calling session.connect().

session.setSocketFactory(new SocketFactory() 
{
    InputStream in = null;
    OutputStream out = null;

    public InputStream getInputStream(Socket socket) throws IOException 
    {
        if (in == null)
            in = socket.getInputStream();
        return in;
    }

    public OutputStream getOutputStream(Socket socket) throws IOException 
    {
        if (out == null)
            out = socket.getOutputStream();
        return out;
    }

    public Socket createSocket(String host, int port) throws IOException, UnknownHostException 
    {
       // The IP Addresses are changed ....
       // using the original IP in my code
        byte[] remoteIpAddr = new byte[] { (byte) 11,(byte) 11, 11, 11 }; 
        byte[] localIpAddr = new byte[] { 55, 55, 55, 55 };

        InetAddress remoteIp = InetAddress.getByAddress(remoteIpAddr);
        InetAddress localIp = InetAddress.getByAddress(localIpAddr);

        logger.debug("remoteIp >>" + remoteIp.toString());
        logger.debug("localIp >>" + localIp.toString());
        Socket socket = new Socket(remoteIp, 22, localIp, 0);
        logger.debug("socket created >> " + socket.toString());
        return socket;
    }
});

And its creating the socket : checked the log file for >>

logger.debug("socket created >> " + socket.toString());

But now I am getting an exception :

Auth cancel

What may be the cause , The key which I have is a id_rsa.pub file, which starts with

ssh-rsa AXTYU....

and I have created the private key file from this file through keygen which I am using in my session.addIdentity() method, the format of the private key is :

-----BEGIN RSA PRIVATE KEY-----
KIOPU.....
-----END RSA PRIVATE KEY-----

Am i missing something , please suggest...




回答3:


This will allow you to bind to a specific address (and port, should to need to): https://stackoverflow.com/a/56120718/11173046



来源:https://stackoverflow.com/questions/26559957/how-to-select-network-interface-when-connecting-to-sftp-with-jsch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!