通过jmeter中的beanshell执行shell命令

通过jmeter中的beanshell执行shell命令,jmeter,beanshell,Jmeter,Beanshell,通过jmeter中的beanshell执行shell命令。我想在jmeter的beanshell预处理器中执行shell命令 任何人都能告诉你怎么做。Beanshell是JAVA(脚本语言)。 下面的陈述应该会有所帮助 Runtime.getRuntime().exec("COMMAND"); 基于@vins answer,我云创建我的ping测试,以验证我的电子邮件服务器是否可用 另外,如果您想记录Runttime.getRuntime().exec(“命令”)的输出在jmeterBeanS

通过jmeter中的beanshell执行shell命令。我想在jmeter的beanshell预处理器中执行shell命令

任何人都能告诉你怎么做。

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

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

基于@vins answer,我云创建我的ping测试,以验证我的电子邮件服务器是否可用

另外,如果您想记录
Runttime.getRuntime().exec(“命令”)的输出在jmeter
BeanShell采样器中使用类似的东西:

// ********
// Ping the email server to verify it's accessable 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());

@Dev我在上面的答案中添加了注释,说明代码的作用。如果您需要某一行的更多具体信息,请告诉我。谢谢。我试图从
过程
对象部分理解。为什么我们必须把它看作一个过程,而不仅仅是一个字符串。还有
BufferReader
在这里是如何工作的。我们是否需要
StringBuilder,
我们可以在没有它的情况下打印输出吗?我采用的
过程能够在日志中显示输出。我在另一个环境中使用了这个脚本,我希望记录输出。
BufferReader
我们需要从流程中实际获取输出,
StringBuilder
非常方便。我相信整个事情可以用一种更简单的方式来完成。谢谢你提供的细节,布鲁诺!添加日志帮助了我!