在jmeter中通过beanshell执行shell命令

lmyy7pcs  于 2022-11-23  发布在  Shell
关注(0)|答案(2)|浏览(121)

在jmeter中通过beanshell执行shell命令。我想在jmeter的beanshell预处理器中执行shell命令
任何一个能告诉如何做到这一点。

x0fgdtte

x0fgdtte1#

Beanshell是JAVA(脚本语言)。下面的语句应该会有所帮助。

Runtime.getRuntime().exec("COMMAND");
tp5buhyn

tp5buhyn2#

基于@vins的回答,我创建了我的ping测试来验证我的电子邮件服务器是否可用。
另外,如果您想记录Runtime.getRuntime().exec("COMMAND");的输出,请在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());

相关问题