Created
January 17, 2018 11:49
-
-
Save Ebazhanov/dbc164f61a94dc24cfcf858041c2d0e1 to your computer and use it in GitHub Desktop.
Establish connection by SSH to DataBase
This file contains 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 de.base.utils.SQL_connection | |
import com.jcraft.jsch.JSch | |
import com.jcraft.jsch.JSchException | |
import com.jcraft.jsch.Session | |
import com.jcraft.jsch.SftpException | |
import de.base.utils.AllOthersUtils.EnvProperties | |
import org.testng.annotations.Test | |
import ru.yandex.qatools.allure.annotations.Step | |
import static de.base.utils.AllOthersUtils.PropertiesReader.getConfigKey | |
/** | |
* Establish connection by SSH to DataBase | |
* | |
*/ | |
class SshConnection { | |
static JSch jsch = new JSch() | |
static Session session = null | |
static ssh = [ | |
UserName: "xxxxxxx", | |
Host : "xx.xx.xxx.xx", | |
Port : 22 | |
] | |
static mySql = [ | |
Iport : 3307, | |
Host : getConfigKey("DB_HOST_NAME"), | |
Rport : 3306, | |
DBname : EnvProperties.DATA_BASE_NAME, | |
DBpassword: getConfigKey("DB_PASSWORD") | |
] | |
@Step | |
static openSSH() { | |
try { | |
jsch.addIdentity("src/test/resources/rsa_key/my_rsa_key", "your_password") | |
session = jsch.getSession(ssh.UserName, ssh.Host, ssh.Port) | |
session.setConfig("StrictHostKeyChecking", "no") | |
session.setPortForwardingL(mySql.Iport, mySql.Host, mySql.Rport) | |
session.connect() | |
} catch (JSchException e) { | |
e.printStackTrace() | |
} catch (SftpException e) { | |
e.printStackTrace() | |
} | |
} | |
@Step | |
static closeSSH() { | |
try { | |
session.disconnect() | |
} catch (JSchException e) { | |
e.printStackTrace() | |
} catch (SftpException e) { | |
e.printStackTrace() | |
} | |
} | |
@Test | |
void testConnection() { | |
openSSH() | |
closeSSH() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment