Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing shell commands through beanshell in jmeter

Executing shell commands through beanshell in jmeter. I want to execute shell commands in beanshell preprocessor in jmeter

Can any one tell how to do so.

like image 859
user1788294 Avatar asked Jan 11 '23 07:01

user1788294


2 Answers

Beanshell is JAVA (scripting language). Below statement should help.

Runtime.getRuntime().exec("COMMAND");
like image 182
vins Avatar answered Jan 12 '23 20:01

vins


Based on @vins answer I cloud create my ping test to verify my email server is available.

Additionally if you want to log the output of the Runtime.getRuntime().exec("COMMAND"); use something similar to this in your jmeter BeanShell Sampler:

// ********
// Ping the email server to verify it's accessible from the execution server
//
// Preconditions:
// custom variable is available named "emailServer" which contains the IP-Adress of the machine to ping
//
// ********

log.info(Thread.currentThread().getName()+": "+SampleLabel+": Ping email server: " + vars.get("emailServer"));

// Select the ping command depending on your machine type windows or Unix machine. 
//String command = "ping -n 2 " + vars.get("emailServer");    // for windows
String command = "ping -c2 " + vars.get("emailServer");    // for Unix

// Print the generated ping command
log.info(command);

// Create a process object and let this object execute the ping command
Process p = Runtime.getRuntime().exec(command);
p.waitFor();

log.info("Execution complete.");

// Read the output of the ping command and log it
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder logCommandOutput = new StringBuilder();
String line;
while( (line = in.readLine()) != null) {
  logCommandOutput.append(line);
}
in.close();
log.info("Output: " + logCommandOutput.toString());
like image 23
Bruno Bieri Avatar answered Jan 12 '23 19:01

Bruno Bieri