Can I connect to remote Mysql via Jumpbox/proxy from my local machine.
I have a java application deployed in AWS machine(A) which has access and can connect to remote Mysql server(B). Now when I need to test my Java application locally I can not connect to Remote Mysql server(B).
So basically can my Java application connect to remote server from my local machine via AWS machine.
This way
Local Machine(java Application) ===> AWS machine(A)==> Mysql Server(B)
AWS Machine(A) can only be connected through SSH. :(
Can tunneling help here?
you can use JSch
http://www.jcraft.com/jsch/
an example of how to use it
package com.journaldev.java.ssh
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.sql.Connection;
public class MySqlConnOverSSH {
/**
* Java Program to connect to the remote database through SSH using port forwarding
* #author Pankaj#JournalDev
* #throws SQLException
*/
public static void main(String[] args) throws SQLException {
int lport=5656;
String rhost="secure.journaldev.com";
String host="secure.journaldev.com";
int rport=3306;
String user="sshuser";
String password="sshpassword";
String dbuserName = "mysql";
String dbpassword = "mysql123";
String url = "jdbc:mysql://localhost:"+lport+"/mydb";
String driverName="com.mysql.jdbc.Driver";
Connection conn = null;
Session session= null;
try{
//Set StrictHostKeyChecking property to no to avoid UnknownHostKey issue
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
session=jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");
int assinged_port=session.setPortForwardingL(lport, rhost, rport);
System.out.println("localhost:"+assinged_port+" -> "+rhost+":"+rport);
System.out.println("Port Forwarded");
//mysql database connectivity
Class.forName(driverName).newInstance();
conn = DriverManager.getConnection (url, dbuserName, dbpassword);
System.out.println ("Database connection established");
System.out.println("DONE");
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn != null && !conn.isClosed()){
System.out.println("Closing Database Connection");
conn.close();
}
if(session !=null && session.isConnected()){
System.out.println("Closing SSH Connection");
session.disconnect();
}
}
}
}
Reference:
https://www.journaldev.com/235/java-mysql-ssh-jsch-jdbc
Related
My JDK is 1.7.
import java.util.Properties;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class SSHClientTest {
public static void main(String[] args){
try {
String host = "192.168.1.233";
int port = 23;
String username = "root";
String password = "htnice";
// create JSch
JSch jSch = new JSch();
// get session
Session session = jSch.getSession(username, host, port);
session.setPassword(password);
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
// start connect
session.connect();
session.disconnect();
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I'm getting the following exception:
Exception in thread "main" java.lang.NullPointerException
at com.jcraft.jsch.Session.start_discard(Session.java:1066)
at com.jcraft.jsch.Session.read(Session.java:937)
at com.jcraft.jsch.Session.connect(Session.java:309)
at com.jcraft.jsch.Session.connect(Session.java:183)
at SSHClientTest.main(SSHClientTest.java:26)
The same error with jsch-0.1.54 and jsch-0.1.55.
I can connect to the server by TTerm. The port is 23.
JSch is SSH client (port 22).
You cannot use it to connect to Telnet server (port 23).
I want to make a connection to the remote database ssh, in my java program, so I write a java code for this, I can ssh to the database server from my code but I can't access to the database, this is my out put which contains the error :
identity added
session created.
session connected.....
shell channel connected....
db Method...
try-catch
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at Main.connectToDB(Main.java:68)
at Main.main(Main.java:46)
Goodbye!
done
I run my .java class with this command ( I give both jsch and mysql-connector jar file, for running my class) and both jar files are in the folder that contains Main.java class
java -cp .:mysql-connector-java-5.0.7-bin.jar.:jsch-0.1.54.jar Main
and here is my code
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class Main {
public static void main(String[] arg) {
try {
JSch jsch = new JSch();
String user = "server-username";
String host = "server-Ip";
int port = 22;
String privateKey = "id_rsa";
jsch.addIdentity(privateKey);
System.out.println("identity added ");
Session session = jsch.getSession(user, host, port);
System.out.println("session created.");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
System.out.println("session connected.....");
Channel channel = session.openChannel("sftp");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect();
System.out.println("shell channel connected....");
connectToDB();
System.out.println("done");
} catch (Exception e) {
System.err.println(e);
}
}
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/DBname";
static final String USER = "username";
static final String PASS = "";
public static void connectToDB() {
System.out.println("db Method...");
Connection conn = null;
Statement stmt = null;
try{
System.out.println("try-catch");
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,"");
System.out.println("hey!");
stmt = conn.createStatement();
String sql;
sql="SELECT * from test";
ResultSet rs = stmt.executeQuery(sql);
try (BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"))) {
while(rs.next()){
String name = rs.getString("name");
bw.write(name);
bw.newLine();
System.out.println(" name: " + name);
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
I confused , I think maybe I should put both jar files in the db-server ?
( Now there are in the javacode-server in the folder of java code)
Where I should put this jars,if I have to put them in the Database Server?
have been troubled by this for quite sometime, with reference to the code from here: http://www.journaldev.com/235/java-program-to-connect-to-remote-database-through-ssh-using-port-forwarding
have been trying, but still unable to get the connection. Here's my modified code:
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.sql.Connection;
public class MySqlConnOverSSH {
/**
* Java Program to connect to remote database through SSH using port forwarding
* #throws SQLException
*/
public static void main(String[] args) throws SQLException {
int lport=5656;
String rhost="fictitious#germany.com";
String host="fictitious#germany.com";
int rport=3306;
String user="root";
String password="root";
String dbuserName = "root";
String dbpassword = "root";
String url = "jdbc:mysql://localhost:"+lport+"/test";
String driverName="com.mysql.jdbc.Driver";
Connection conn = null;
Session session= null;
try{
//Set StrictHostKeyChecking property to no to avoid UnknownHostKey issue
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
session=jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");
int assigned_port=session.setPortForwardingL(lport, rhost, rport);
System.out.println("localhost:"+assigned_port+" -> "+rhost+":"+rport);
System.out.println("Port Forwarded");
//mysql database connectivity
Class.forName(driverName).newInstance();
conn = DriverManager.getConnection (url, dbuserName, dbpassword);
System.out.println ("Database connection established");
System.out.println("DONE");
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn != null && !conn.isClosed()){
System.out.println("Closing Database Connection");
conn.close();
}
if(session !=null && session.isConnected()){
System.out.println("Closing SSH Connection");
session.disconnect();
}
}
}
and following is the error which i get:
Connected
localhost:5656 -> fictitious#germany.com:3306
Port Forwarded
java.sql.SQLInvalidAuthorizationSpecException: Could not connect: Access denied for user 'root'#'fictitious#germany.com' (using password: YES)
at org.mariadb.jdbc.internal.util.ExceptionMapper.get(ExceptionMapper.java:121)Closing SSH Connection
at org.mariadb.jdbc.internal.util.ExceptionMapper.throwException(ExceptionMapper.java:69)
at org.mariadb.jdbc.Driver.connect(Driver.java:110)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at myDB.MySqlConnOverSSH.main(MySqlConnOverSSH.java:50)
Caused by: org.mariadb.jdbc.internal.util.dao.QueryException: Could not connect: Access denied for user 'root'#'fictitious#germany.com' (using password: YES)
at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.authentication(AbstractConnectProtocol.java:433)
at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.handleConnectionPhases(AbstractConnectProtocol.java:397)
at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connect(AbstractConnectProtocol.java:318)
at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:630)
at org.mariadb.jdbc.internal.util.Utils.retrieveProxy(Utils.java:580)
at org.mariadb.jdbc.Driver.connect(Driver.java:105)
... 3 more}
I am at Java:beginner level and trying to explore different ways to get this connection going. By the way, I am able to connect to the database using the given Username and password on SecureCRT, so the authorization error is kind of confusing. It would be great if someone could guide me in what I'm doing wrong and how it could be corrected. Thanks a ton in advance !
I am using Java Eclipse on Windows, and have Mysql set up on Solaris 10. I have used JSch but it is not letting me to connect to Mysql database with root user.
I am getting the below error:
Connected
localhost:3306 -> xxx.xx.xxx.xxx:3306
Port Forwarded
java.sql.SQLException: Access denied for user: 'root#192.168.1.1' (Using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:894)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1309)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2032)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:729)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:283)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at ir.mnaeimabadi.hello.mysqltest.main(mysqltest.java:47)
Actually when in this line:
DriverManager.getConnection (url, dbuserName, dbpassword);
It is giving me the error.
This is overall code:
package ir.mnaeimabadi.hello;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.sql.Connection;
public class mysqltest {
public static void main(String[] args) {
// TODO Auto-generated method stub
int lport=3306;
String rhost="xxx.xx.xxx.xxx";
String host="xxx.xx.xxx.xxx";
int rport=3306;
String user="myusr";//ssh user
String password="mypass";//ssh pass
String dbuserName = "root";//mysql user
String dbpassword = "rootPass"; //mysql pass
String url = "jdbc:mysql://xxx.xx.xxx.xxx:";
String driverName="com.mysql.jdbc.Driver";
Connection conn = null;
Session session= null;
try{
//Set StrictHostKeyChecking property to no to avoid UnknownHostKey issue
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
session=jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");
int assinged_port=session.setPortForwardingL(lport, rhost, rport);
System.out.println("localhost:"+assinged_port+" -> "+rhost+":"+rport);
System.out.println("Port Forwarded");
url = url+assinged_port+"/myDB";
//mysql database connectivity
Class.forName(driverName).newInstance();
conn = DriverManager.getConnection (url, dbuserName, dbpassword);
System.out.println ("Database connection established");
System.out.println("DONE");
}catch(Exception e){
e.printStackTrace();
}finally{
try {
if(conn != null && !conn.isClosed()){
System.out.println("Closing Database Connection");
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(session !=null && session.isConnected()){
System.out.println("Closing SSH Connection");
session.disconnect();
}
}
}
}
i have tried create a project with library commons.net for send via ftp some files. But i created a connection with my server i have received this error.
org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
Server Reply: SSH-2.0-OpenSSH_5.3
i have followed this article for create my connection, and with official examples i've controlled article.
my java code here:
private void connect(String host, String user, String pwd) {
try{
ftp = new FTPSClient(false);
//ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
int reply;
ftp.connect(host,22);//error is here
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new Exception("Exception in connecting to FTP Server");
}
ftp.login(user, pwd);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
}catch(Exception ex){
ex.printStackTrace();
}
}
I do not understand where I went wrong.
The FTPS protocol does not run over SSH. What you need is SFTP. For this you could look at the Jsch library
JSch jsch = new JSch();
Session session = jsch.getSession( user, host, port );
session.setConfig( "PreferredAuthentications", "password" );
session.setPassword( pass );
session.connect( FTP_TIMEOUT );
Channel channel = session.openChannel( "sftp" );
ChannelSftp sftp = ( ChannelSftp ) channel;
sftp.connect( FTP_TIMEOUT );
SFTP (file transfer running as an SSH stream over an SSH connection) is not the same as FTPS (FTP using SSL/TLS).
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class FTPConnectAndLoginDemo {
public static void main(String[] args) throws Exception {
String user = "username";
String host = "hostadress";
int port = 22;
String pass = "password";
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(pass);
session.connect();
System.out.println("Connection established.");
System.out.println("Creating SFTP Channel.");
}
try this instead hope this can help.The Session.setConfig ommits the hashcode check and allows user to connect to host.
Change the port to 21 to connect to FTP server instead of SFTP(port 22)