如何通过ssh(端口22)然后端口转发3306来访问数据库 - SOAP UI groovy

时间:2016-11-08 00:31:19

标签: database groovy soapui

这是我到目前为止所拥有的。但问题是首先我需要通过ssh(端口22)进行隧道传输然后端口转发3306以通过ssh连接访问数据库。我能够做到这一点,但没有运气,有人可以调查一下吗?

import groovy.sql.Sql
package mypackage
import java.sql.*
import com.jcraft.jsch.JSch
import com.jcraft.jsch.Session


// ssh login
def sshHost = ''
def sshUser = ''
def sshPass = ''
def sshPort = 22

// database login
def targetHost = '127.0.0.1'
def targetUser = ''
def targetPass = ''
def targetPort = '3306'

JSch jsch = new JSch();
Session session = jsch.getSession(sshUser, sshHost, sshPort);
session.setPassword(sshPass);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
session.connect();
int assinged_port=session.setPortForwardingL(0, targetHost, targetPort);

Connection con = null;
def driver = 'org.mariadb.jdbc.Driver'
def connectionString = 'jdbc:mariadb://localhost:3306/db'
con = DriverManager.getConnection(connectionString, targetUser, targetPass);
Statement st = con.createStatement();
String sql = "select * company "
st.execute(sql);

0 个答案:

没有答案