jmeter creates ssh connection to access database

The Internet industry pays more and more attention to the security mechanism. Many company database connections will add a layer of identity verification to the original basic connection information, such as ssh layer connection identity verification.

 

When we do interface testing, we often need to get some data from the database. For convenience, using jmeter's jdbc request and jdbc connection configuration module functions can meet the basic test data requirements.

 

However, in the function of the jdbc connection configuration module, only the basic database connection configuration is supported. If the database we want to access has ssh verification, it cannot meet our needs. Let's analyze the whole process, which is mainly divided into 4 steps, as follows:

 

1. Create an ssh connection .

Jmeter is a tool written in pure java, so as long as java can create ssh connections, it can also be done in jmeter.

The BeanShell Sampler module in jmeter can implement this function. The code is as follows:

import com.jcraft.jsch.*;


import java.sql.*;
JSch jsch = new JSch();  
Session session = jsch.getSession("ssh_username", "ssh_ip", ssh_prot);  
session.setPassword("ssh_password");  
session.setConfig("StrictHostKeyChecking", "no");  

session.connect();  
int assinged_port = session.setPortForwardingL(your_prot, "database connection address", database port);//Port mapping forwarding

            System.out.println("localhost:" + assinged_port);

      
           session.setPortForwardingR(your_prot, "Database connection address", port);
            System.out.println("localhost:  -> ");

System.out.println(session.getServerVersion());
//Class.forName("com.mysql.jdbc.Driver");
//Connection conn = null;
//conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_name", "username", "password");

 

2. Create a database connection

Use the jdbc connection configuration module function that comes with jmeter to create a connection.

Specific steps (omitted)

 

3. Create sql request

Use the jdbc request module function that comes with jmeter to create a connection.

Specific steps (omitted)

 

Fourth, regular extraction of the required data

Use the "Regular Expression Extractor" module function that comes with jmeter to create a connection.

Specific steps (omitted)

 

Attention to detail:

1. The dependency jar package is indispensable, jsch-0.1.54.jar and mysql-connector-java-5.1.39.jar

2. Creating an ssh connection only needs to be executed once, independent of the transaction thread

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326081936&siteId=291194637