Created
March 18, 2019 08:58
-
-
Save liangzai-cool/c8755aae95b95adfff62e369de6cfe24 to your computer and use it in GitHub Desktop.
利用jsch连接跳板机,将本地和公司服务器内网建立连接
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.properties; | |
import com.google.common.net.HostAndPort; | |
import lombok.AllArgsConstructor; | |
import lombok.Getter; | |
import lombok.NoArgsConstructor; | |
import lombok.Setter; | |
import lombok.extern.slf4j.Slf4j; | |
import org.apache.commons.lang3.StringUtils; | |
import org.springframework.boot.context.properties.ConfigurationProperties; | |
import java.util.List; | |
import java.util.Map; | |
/** | |
* @author xueliang | |
* @since 2019-03-16 11:30 | |
*/ | |
@Slf4j | |
@Getter | |
@Setter | |
@AllArgsConstructor | |
public class DebugSSHTunnelProperties { | |
private String serverIp; | |
private Integer serverPort; | |
private String username; | |
private String privateKeyPath; | |
private Map<Integer, HostAndPort> portForwardingLMap; | |
public boolean validate() { | |
if (StringUtils.isEmpty(serverIp)) { | |
return false; | |
} | |
if (serverPort == null) { | |
return false; | |
} | |
if (StringUtils.isEmpty(username)) { | |
return false; | |
} | |
if (StringUtils.isEmpty(privateKeyPath)) { | |
return false; | |
} | |
if (portForwardingLMap.size() == 0) { | |
return false; | |
} | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import com.google.common.net.HostAndPort; | |
import com.hellobike.sw.data.config.properties.DebugSSHTunnelProperties; | |
import com.jcraft.jsch.Channel; | |
import com.jcraft.jsch.JSch; | |
import com.jcraft.jsch.Logger; | |
import com.jcraft.jsch.Session; | |
import lombok.extern.slf4j.Slf4j; | |
import java.util.Map; | |
/** | |
* 调试工具包 | |
* @author xueliang | |
* @since 2019-03-15 14:32 | |
*/ | |
@Slf4j | |
public class DebugUtils { | |
/** | |
* 利用 SSH Tunnel,将本地与内网建立链接 | |
* @param properties | |
*/ | |
public static void openSSHTunnelIfNecessary(DebugSSHTunnelProperties properties) { | |
try { | |
JSch.setLogger(new Logger() { | |
java.util.Hashtable<Integer, String> name = new java.util.Hashtable(); | |
{ | |
name.put(DEBUG, "DEBUG: "); | |
name.put(INFO, "INFO: "); | |
name.put(WARN, "WARN: "); | |
name.put(ERROR, "ERROR: "); | |
name.put(FATAL, "FATAL: "); | |
} | |
@Override | |
public boolean isEnabled(int level) { | |
return true; | |
} | |
@Override | |
public void log(int level, String message) { | |
System.err.print(name.get(level)); | |
System.err.println(message); | |
} | |
}); | |
JSch jsch = new JSch(); | |
jsch.addIdentity(properties.getPrivateKeyPath()); | |
Session session = jsch.getSession(properties.getUsername(), properties.getServerIp(), properties.getServerPort()); | |
session.setConfig("StrictHostKeyChecking", "no"); | |
for (Map.Entry<Integer, HostAndPort> entry : properties.getPortForwardingLMap().entrySet()) { | |
session.setPortForwardingL("*", entry.getKey(), entry.getValue().getHostText(), entry.getValue().getPort()); | |
} | |
// making a connection with timeout. | |
session.connect(30000); | |
Channel channel = session.openChannel("session"); | |
channel.connect(5 * 1000); | |
log.info("**********open SSH Tunnel successfully**********"); | |
} catch (Exception e) { | |
log.error("open SSH Tunnel error", e); | |
} | |
} | |
private DebugUtils() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment