【问题标题】:set mysql connection behind ssh in groovy script SoapUI在 groovy 脚本 SoapUI 中设置 ssh 后面的 mysql 连接
【发布时间】:2014-05-22 08:17:43
【问题描述】:

通过 SoapUI 中的 groovy 脚本,我需要连接到 mysql 数据库来执行一些查询。问题是由于安全原因,无法进行外部访问。 因此需要获得 ssh 访问权限(如隧道)并在本地调用 mysql。

最初我是在阅读下面的项目属性,然后连接到 mysql:

ServerUrl=jdbc:mysql://10.255.255.122:3306/db
ServerDbUser=user
ServerDbPwd=password
ServerDriver=com.mysql.jdbc.Driver

def url=testRunner.testCase.testSuite.project.getPropertyValue("ServerUrl")
def usr=testRunner.testCase.testSuite.project.getPropertyValue("ServerDbUser")
def pwd=testRunner.testCase.testSuite.project.getPropertyValue("ServerDbPwd")
def driver=testRunner.testCase.testSuite.project.getPropertyValue("ServerDriver")

com.eviware.soapui.support.GroovyUtils.registerJdbcDriver(driver)
sqlServer = Sql.newInstance(url, usr, pwd, driver)`

但这不起作用,所以现在需要先与 IP 10.255.255.122 的服务器建立 ssh 连接,然后在本地打开 mysql 连接。所以我猜Server Url会变成:

ServerUrl=jdbc:mysql://127.0.0.1:3306/db

但我不知道如何首先设置与服务器的 ssh 连接。

有人可以帮我解决这个问题吗?

谢谢。

【问题讨论】:

    标签: mysql groovy ssh soapui


    【解决方案1】:

    看看http://forum.soapui.org/viewtopic.php?t=15400connect to remote mysql database through ssh using java

    它会给你一个关于在soapUI中实现它的想法。

    下面是 Ripon Al Wasim 的代码,可以在上面提到的 stackoverflow 链接中找到答案

    package mypackage;
    import java.sql.*;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.Session;
    
    public class UpdateMySqlDatabase {
        static int lport;
        static String rhost;
        static int rport;
        public static void go(){
            String user = "ripon";
            String password = "wasim";
            String host = "myhost.ripon.wasim";
            int port=22;
            try
                {
                JSch jsch = new JSch();
                Session session = jsch.getSession(user, host, port);
                lport = 4321;
                rhost = "localhost";
                rport = 3306;
                session.setPassword(password);
                session.setConfig("StrictHostKeyChecking", "no");
                System.out.println("Establishing Connection...");
                session.connect();
                int assinged_port=session.setPortForwardingL(lport, rhost, rport);
                System.out.println("localhost:"+assinged_port+" -> "+rhost+":"+rport);
                }
            catch(Exception e){System.err.print(e);}
        }
        public static void main(String[] args) {
            try{
                go();
            } catch(Exception ex){
                ex.printStackTrace();
            }
              System.out.println("An example for updating a Row from Mysql Database!");
              Connection con = null;
              String driver = "com.mysql.jdbc.Driver";
              String url = "jdbc:mysql://" + rhost +":" + lport + "/";
              String db = "testDB";
              String dbUser = "wasim";
              String dbPasswd = "riponalwasim123";
              try{
              Class.forName(driver);
              con = DriverManager.getConnection(url+db, dbUser, dbPasswd);
              try{
              Statement st = con.createStatement();
              String sql = "UPDATE MyTableName " +
                      "SET email = 'ripon.wasim@smile.com' WHERE email='peace@happy.com'";
    
              int update = st.executeUpdate(sql);
              if(update >= 1){
              System.out.println("Row is updated.");
              }
              else{
              System.out.println("Row is not updated.");
              }
              }
              catch (SQLException s){
              System.out.println("SQL statement is not executed!");
              }
              }
              catch (Exception e){
              e.printStackTrace();
              }
              }
            }
    

    【讨论】:

    • 有用的链接...我看看能不能把它翻译成 SoapUI groovy 脚本。谢谢
    • 您可能不必进行太多更改,只需能够重用大部分代码。让我知道这是否适合您。
    • 我以为这样可以更简单,不用导入java和java安全通道...
    • 你也可以使用 groovy 创建一个 SSH...查看这篇文章 randomactsofsentience.com/2012/02/…
    • 我尝试了您解决方案中的代码,但出现此错误:Thu Apr 10 12:03:38 EEST 2014:INFO:com.jcraft.jsch.JSchException: PortForwardingL: local port 127.0.0.1:4321 cannot be bound.
    猜你喜欢
    • 2017-12-07
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多