拓展sshxcute使之支持SOCKS5代理

sshxcute是对JSCH的封装,通过使用这个框架我们能很快的上手远程操作ssh。但是这个设计之初没有实现对proxy socks5的支持,通过修改源码使之支持。

  • 参考及源码下载地址:https://www.ibm.com/developerworks/cn/opensource/os-sshxcute/#ibm-pcon
  • 拓展ConnBean,添加ProxyCredential变量
public class ConnBean {
 private ProxyCredential proxyCredential;
 ......
 public static class ProxyCredential {
        private String host;
        private int port;
        private String username;
        private String password;

        public ProxyCredential() {
            this(null, 0, null, null);
        }

        public ProxyCredential(String host, int port, String username, String password) {
            this.host = host;
            this.port = port;
            this.username = username;
            this.password = password;
        }

        public String getHost() {
            return host;
        }

        public void setHost(String host) {
            this.host = host;
        }

        public int getPort() {
            return port;
        }

        public void setPort(int port) {
            this.port = port;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }
}
  • 修改SSHExec.class,添加代理实现
public Boolean connect() {
 ......
 if (this.conn.getProxyCredential() != null) {
     session.setProxy(getProxy());
 }
 .......
}
private ProxySOCKS5 getProxy() {
    String proxyHost = this.conn.getProxyCredential().getHost().trim();
    int proxyPort = this.conn.getProxyCredential().getPort();
    String proxyUsername = this.conn.getProxyCredential().getUsername().trim();
    String proxyPassword = this.conn.getProxyCredential().getPassword().trim();
    ProxySOCKS5 proxy = new ProxySOCKS5(proxyHost, proxyPort);
    proxy.setUserPasswd(proxyUsername, proxyPassword);
    return proxy;
}
  • 然后在初始化的时候时候ConnBean的时候添加代理的就可以了。

当然如果没有不是梨花

ConnBean.ProxyCredential proxyCredential = new ConnBean.ProxyCredential();
proxyCredential.setHost("host");
proxyCredential.setPort(port);
proxyCredential.setUsername("username");
proxyCredential.setPassword("password");
String host = "serverHost";
String username = "serverUsername";
String password = "serverPassword";
ConnBean cb = new ConnBean(host, username, password);
cb.setProxyCredential(proxyCredential); #如果不需要代理就直接不调用这行就可以了。
SSHExec ssh = SSHExec.getInstance(cb);

你可能感兴趣的:(拓展sshxcute使之支持SOCKS5代理)