-
Notifications
You must be signed in to change notification settings - Fork 2
/
SshTunnelConnection.java
106 lines (82 loc) 路 3.01 KB
/
SshTunnelConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.jayden.study.connection;
import com.jayden.study.utils.JdbcUtils;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.UUID;
/**
* Connecting to remote MySQL through SSH using Java
*
* @author jayden-lee
*/
public class SshTunnelConnection {
// db information
private static String host = "dbHost";
private static int port = 3306;
private static String databaseName = "mysql";
private static final String user = "user";
private static final String password = "password";
// ssh information
private static final String sshHost = "sshHost";
private static final int sshPort = 22;
private static final String sshUser = "user";
private static final String sshPassword = "password";
private static Connection connection;
// random number
private static int localPort = 12345;
private static Session session;
private static SshAuthType sshAuthType = SshAuthType.PASSWORD;
public enum SshAuthType {
PASSWORD,
PUBLIC_KEY
}
public static void main(String[] args) {
try {
// ssh connect
prepareSshTunnel();
connection = JdbcUtils.getConnection(getJdbcUrl(), user, password);
executeTestQuery();
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.closeConnection(connection);
if (session != null) {
// close ssh session
session.disconnect();
}
}
}
private static void prepareSshTunnel() throws Exception {
JSch jSch = new JSch();
if (SshAuthType.PUBLIC_KEY == sshAuthType) {
// key file byte data and passphrase
jSch.addIdentity(UUID.randomUUID().toString(), null, null, null);
}
session = jSch.getSession(sshUser, sshHost, sshPort);
session.setConfig("StrictHostKeyChecking", "no");
if (SshAuthType.PASSWORD == sshAuthType) {
session.setConfig("PreferredAuthentications", "password,keyboard-interactive");
session.setPassword(sshPassword);
} else {
session.setConfig("PreferredAuthentications", "publickey");
}
session.connect();
System.out.println("Connected");
int allocatedLocalPort = session.setPortForwardingL(localPort, host, port);
System.out.println("localhost:" + allocatedLocalPort + " -> " + host + ":" + port);
}
private static String getJdbcUrl() {
return "jdbc:mysql://localhost:" + localPort + "/" + databaseName;
}
private static void executeTestQuery() throws Exception {
String sql = "select * from sakila.address";
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql)) {
JdbcUtils.printResultData(resultSet);
} catch (Exception e) {
throw e;
}
}
}