QQ972293570
2016-07-15 00:45
采纳率: 100%
浏览 1.7k
已采纳

com.jcraft.jsch 实现SSH的BindAddress功能

今天使用jsch实现ssh功能遇到一个问题,请各位大神帮忙看看,先在这里谢过了。问题如下:
之前能通过命令sftp username@ip连接对端服务器,java代码为

 com.jcraft.jsch.Session session = null;
JSch jsch = new JSch();
session = jsch.getSession(userName, ip, Integer.parseInt(port));
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
channel = session.openChannel("sftp");
channel.connect();

后面由于客户安全考虑使用了浮动ip,先前的命令无法连通,需用命令sftp -oBindAddress=ip_浮动 syncoss@ip。
发现使用session.setConfig("BindAddress", ip_浮动);//无效

继续查资料,试着使用代理

  ProxySOCKS4 proxysocket = new ProxySOCKS4(ip_浮动,22);
 session.setProxy(proxysocket);

依旧不行。。。。

求各位大神。指教

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • QQ972293570 2016-08-03 03:43
    已采纳

    SocketFactoryIns socketFactory = new SocketFactoryIns();
    socketFactory.setLocalAddressIp(bindAddress);
    session.setSocketFactory(socketFactory);

    
    

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;

    import com.jcraft.jsch.SocketFactory;
    import com.ztesoft.zsmart.uip.base.util.LogBase;

    public class SocketFactoryIns implements SocketFactory {
    private LogBase iplog = LogBase.getLogger(SocketFactoryIns.class);
    private InputStream in = null;
    private OutputStream out = null;
    private String localAddressIp = null;

    @Override
    public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
        // TODO Auto-generated method stub
        InetAddress remoteIp = InetAddress.getByName(host);
        InetAddress localIp = InetAddress.getByName(localAddressIp);
    
        iplog.info("remoteIp >>" + remoteIp.toString());
        iplog.info("localIp >>" + localIp.toString());
        Socket socket = new Socket(remoteIp, 22, localIp, 0);
        iplog.info("socket created >> " + socket.toString());
        return socket;
    
    }
    
    
    
    public String getLocalAddressIp() {
        return localAddressIp;
    }
    
    
    
    public void setLocalAddressIp(String localAddressIp) {
        this.localAddressIp = localAddressIp;
    }
    
    
    
    @Override
    public InputStream getInputStream(Socket socket) throws IOException {
        // TODO Auto-generated method stub
         if (in == null)
                in = socket.getInputStream();
        return in;
    }
    
    @Override
    public OutputStream getOutputStream(Socket socket) throws IOException {
        // TODO Auto-generated method stub
         if (out == null)
                out = socket.getOutputStream();
            return out;
    }
    public static void main(String[] args){
        SocketFactoryIns ins = new SocketFactoryIns();
        try {
            ins.createSocket("10.45.50.112", 22);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    

    }

    
    
    点赞 打赏 评论
  • QQ972293570 2016-08-03 03:45

    使用方法ession.setSocketFactory(socketFactory);
    代码如下:

     SocketFactoryIns socketFactory = new SocketFactoryIns();
        socketFactory.setLocalAddressIp(bindAddress);
        session.setSocketFactory(socketFactory);
    
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    import com.jcraft.jsch.SocketFactory;
    import com.ztesoft.zsmart.uip.base.util.LogBase;
    
    public class SocketFactoryIns implements SocketFactory {
        private LogBase iplog = LogBase.getLogger(SocketFactoryIns.class);
        private InputStream in = null;
        private OutputStream out = null;
        private String localAddressIp = null;
    
        @Override
        public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
            // TODO Auto-generated method stub
            InetAddress remoteIp = InetAddress.getByName(host);
            InetAddress localIp = InetAddress.getByName(localAddressIp);
    
            iplog.info("remoteIp >>" + remoteIp.toString());
            iplog.info("localIp >>" + localIp.toString());
            Socket socket = new Socket(remoteIp, 22, localIp, 0);
            iplog.info("socket created >> " + socket.toString());
            return socket;
    
        }
    
    
    
        public String getLocalAddressIp() {
            return localAddressIp;
        }
    
    
    
        public void setLocalAddressIp(String localAddressIp) {
            this.localAddressIp = localAddressIp;
        }
    
    
    
        @Override
        public InputStream getInputStream(Socket socket) throws IOException {
            // TODO Auto-generated method stub
             if (in == null)
                    in = socket.getInputStream();
            return in;
        }
    
        @Override
        public OutputStream getOutputStream(Socket socket) throws IOException {
            // TODO Auto-generated method stub
             if (out == null)
                    out = socket.getOutputStream();
                return out;
        }
        public static void main(String[] args){
            SocketFactoryIns ins = new SocketFactoryIns();
            try {
                ins.createSocket("10.45.50.112", 22);
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
    
    点赞 打赏 评论

相关推荐