How to select network interface when connecting to SFTP with JSch

前端 未结 3 1829
心在旅途
心在旅途 2021-01-14 15:33

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:



        
3条回答
  •  执念已碎
    2021-01-14 16:24

    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...

提交回复
热议问题