One Star

Java code to open SSH tunnel connection?

Hi, does anyone here have a recommended method to using java code to open a remote ssh tunnel connection to start out a job?
To at least start somewhere I downloaded and added the latest version of jsch jsch-0.1.50.jar to my user libraries
Also found a sample script to connect here: http://mws-mdm.blogspot.ca/2013/07/jdbc-over-ssh-tunnel.html
To get myself started I put that script verbatim into a tJava component; but it looks like the script is going to need to be Talend-ized in order to make it work. Tried it out of the box and got a number of errors:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Syntax error on token "import", assert expected
java.sql.Connection cannot be resolved to a variable
Syntax error on token "import", assert expected
java.sql.DriverManager cannot be resolved to a variable
Syntax error on token "import", assert expected
java.util.Properties cannot be resolved to a variable
Syntax error on token "import", assert expected
com.jcraft cannot be resolved to a variable
Syntax error on token "import", assert expected
com.jcraft cannot be resolved to a variable
Syntax error on token "import", assert expected
com.jcraft cannot be resolved to a variable
Illegal modifier for the local class CTestDriver; only abstract or final is permitted
The method doSshTunnel cannot be declared static; static methods can only be declared in a static or top level type
JSchException cannot be resolved to a type
JSch cannot be resolved to a type
JSch cannot be resolved to a type
Session cannot be resolved to a type
Properties cannot be resolved to a type
Properties cannot be resolved to a type
The method main cannot be declared static; static methods can only be declared in a static or top level type
The method doSshTunnel(String, String, String, int, String, int, int) from the type CTestDriver refers to the missing type JSchException
Connection cannot be resolved to a type
DriverManager cannot be resolved
at natcobidw.testingssh_0_1.TestingSSH.tJava_1Process(TestingSSH.java:341)
at natcobidw.testingssh_0_1.TestingSSH.runJobInTOS(TestingSSH.java:1308)
at natcobidw.testingssh_0_1.TestingSSH.main(TestingSSH.java:1173)

Perhaps someone could help me tweak my method to get it working?
Or, perhaps someone has already gone down this path and could lay out some steps for me to follow?
Thank You Smiley Happy
PS - I'm attempting to connect to a remote MySQL database
10 REPLIES
Seventeen Stars

Re: Java code to open SSH tunnel connection?

There is a user component in Talend Exchange called tSshTunnel. I would try this.
One Star

Re: Java code to open SSH tunnel connection?

Hi, I started working with this component and then hit a rather strange error that I've never seen before:
Exception in component tMysqlRow_1
com.mysql.jdbc.PacketTooBigException: Packet for query is too large (4739923 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:605)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1078)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2412)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2445)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2230)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
disconnected
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at ourworkspace.testingssh_0_1.TestingSSH.tMysqlRow_1Process(TestingSSH.java:703)
at ourworkspace.testingssh_0_1.TestingSSH.tSshTunnel_2Process(TestingSSH.java:375)
at ourworkspace.testingssh_0_1.TestingSSH.runJobInTOS(TestingSSH.java:1060)
at ourworkspace.testingssh_0_1.TestingSSH.main(TestingSSH.java:925)

The max_allowed_packet variable was adjusted (though it's never been a problem before); I feel like this error might be bogus and something else might be going on, but I'm not sure
Seventeen Stars

Re: Java code to open SSH tunnel connection?

Did you tested the communication with a hand made tunnel?
One Star

Re: Java code to open SSH tunnel connection?

Yes I can connect in terminal using ssh and can query successfully
Seventeen Stars

Re: Java code to open SSH tunnel connection?

As an alternative way you could call the necessary commands to the OS by the tSystem component.
One Star

Re: Java code to open SSH tunnel connection?

Hi thanks for the suggestion
Is this the right idea for tSystem commands?
1. Connect to remote server
2. Start MySQL
3. Enter my MySQL query
Then pass the resulting data on to my next component (e.g. tMap) via Main row connector?
Seventeen Stars

Re: Java code to open SSH tunnel connection?

No I mean something like this:
tSystem (call the necessary ssh command) --(OnSubjobOK)--> tMysqlConnection --(OnSubjobOK)--> ... any mysql comps in flows.
One Star

Re: Java code to open SSH tunnel connection?

Hi, Can you give more detail of how to use tSystem and tMysqlconnection or if anyone has used it can give snapshot of the settings?
One Star

Re: Java code to open SSH tunnel connection?

No I mean something like this:
tSystem (call the necessary ssh command) --(OnSubjobOK)--> tMysqlConnection --(OnSubjobOK)--> ... any mysql comps in flows.

your fiest component is System or there is a tSSH before it?
I have a problem to connect to Mysql with ssh.
One Star

Re: Java code to open SSH tunnel connection?

There is a user component in Talend Exchange called tSshTunnel. I would try this.

How to configure tSSHTunnel or tSSH to connect MySql with SSH?
I have a problem to connect to MySql with SSH, i dont know about the command on tSSH or tSystem
Hope somebody can help me.