如何从APACHE JMETER执行Linux命令或shell脚本

时间:2022-01-11 01:16:26

How to execute Linux command or shell script from APACHE JMETER

如何从APACHE JMETER执行Linux命令或shell脚本

Do anyone know how to execute linux commands from Jmeter? I found this link online http://www.technix.in/execute-linux-command-shell-script-apache-jmeter/ and I tried the steps, but is not working. I can't see the SSH Sampler. If anyone had any success with running shell scripts from Jmeter please share. Thanks in advance

有谁知道如何从Jmeter执行linux命令?我在网上找到了这个链接http://www.technix.in/execute-linux-command-shell-script-apache-jmeter/我尝试了这些步骤,但是没有用。我看不到SSH采样器。如果有人从Jmeter运行shell脚本取得任何成功,请分享。提前致谢

4 个解决方案

#1


Have a look at OS Process Sampler which is done for this and available in core:

看一下为此完成的OS Process Sampler并在核心中提供:

#2


If you need to execute a command on remote system take the following steps:

如果需要在远程系统上执行命令,请执行以下步骤:

  1. Download JSch.jar - the library which provides SSH and SCP protocols operations from Java language and place it to /lib folder of your JMeter installation
  2. 下载JSch.jar - 从Java语言提供SSH和SCP协议操作的库,并将其放到JMeter安装的/ lib文件夹中

  3. Download groovy-all.jar - Groovy scripting engine support for Jmeter and drop it to the /lib folder as well
  4. 下载groovy-all.jar - 对Jmeter的Groovy脚本引擎支持并将其放到/ lib文件夹中

  5. Restart JMeter to pick the libraries up
  6. 重新启动JMeter以选择库

  7. Add JSR223 Sampler to your Test Plan and choose "groovy" from "Language" drop-down
  8. 将JSR223 Sampler添加到您的测试计划中,然后从“语言”下拉菜单中选择“groovy”

  9. Follow example code from Exec.java Jsch tutorial to implement your own logic.
  10. 按照Exec.java Jsch教程中的示例代码实现您自己的逻辑。

You can also refer to below snippet which executes ls command on a remote *nix system and returns command execution result. Make sure that you provide valid username, hostname and password in order so sampler could work

您还可以参考下面的代码片段,它在远程* nix系统上执行ls命令并返回命令执行结果。确保您提供有效的用户名,主机名和密码,以便采样器可以正常工作

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

JSch jSch = new JSch();
Session session = jSch.getSession("username", "hostname", 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("password");
session.connect();

Channel channel = session.openChannel("exec");

String command = "ls";
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();

channel.connect();
StringBuilder rv = new StringBuilder();
rv.append("New system date: ");
byte[] tmp = new byte[1024];
while (true) {
  while (in.available() > 0) {
      int i = in.read(tmp, 0, 1024);
      if (i < 0) break;
      rv.append(new String(tmp, 0, i));
  }
  if (channel.isClosed()) {
      break;
  }
  try {
      Thread.sleep(100);
  } catch (Exception ee) {
      ee.printStackTrace();
  }
}
in.close();
channel.disconnect();
session.disconnect();        
SampleResult.setResponseData(rv.toString().getBytes());

See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! for details on Groovy scripting engine installation and best scripting practices.

请参阅Beanshell vs JSR223 vs Java JMeter Scripting:Performance-Off你一直在等待!有关Groovy脚本引擎安装和最佳脚本实践的详细信息。

#3


You can use the Beanshell scripting inside jmeter then you can have some thing like that:

你可以在jmeter里面使用Beanshell脚本,那么你可以有类似的东西:

    String command="your command here";
    StringBuffer output = new StringBuffer();

    Process p;
    try {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        BufferedReader reader = 
                        new BufferedReader(new InputStreamReader(p.getInputStream()));

                    String line = "";           
        while ((line = reader.readLine())!= null) {
            output.append(line + "\n");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    log.inf(output.toString());

#4


Look here this answer about execute jar-file. But idea the same for other OS-command.

看看这个关于执行jar文件的答案。但对其他OS命令也一样。

  1. Use OS_Process_Sampler
  2. Fill field command with OS shell, like /bin/bash or analogue.
  3. 使用OS shell填充字段命令,例如/ bin / bash或analogue。

  4. Set first argument with -c
  5. 用-c设置第一个参数

  6. Set next argument with you command
  7. 使用you命令设置下一个参数

  8. Sampler ready to execute :)
  9. 采样器准备执行:)

#1


Have a look at OS Process Sampler which is done for this and available in core:

看一下为此完成的OS Process Sampler并在核心中提供:

#2


If you need to execute a command on remote system take the following steps:

如果需要在远程系统上执行命令,请执行以下步骤:

  1. Download JSch.jar - the library which provides SSH and SCP protocols operations from Java language and place it to /lib folder of your JMeter installation
  2. 下载JSch.jar - 从Java语言提供SSH和SCP协议操作的库,并将其放到JMeter安装的/ lib文件夹中

  3. Download groovy-all.jar - Groovy scripting engine support for Jmeter and drop it to the /lib folder as well
  4. 下载groovy-all.jar - 对Jmeter的Groovy脚本引擎支持并将其放到/ lib文件夹中

  5. Restart JMeter to pick the libraries up
  6. 重新启动JMeter以选择库

  7. Add JSR223 Sampler to your Test Plan and choose "groovy" from "Language" drop-down
  8. 将JSR223 Sampler添加到您的测试计划中,然后从“语言”下拉菜单中选择“groovy”

  9. Follow example code from Exec.java Jsch tutorial to implement your own logic.
  10. 按照Exec.java Jsch教程中的示例代码实现您自己的逻辑。

You can also refer to below snippet which executes ls command on a remote *nix system and returns command execution result. Make sure that you provide valid username, hostname and password in order so sampler could work

您还可以参考下面的代码片段,它在远程* nix系统上执行ls命令并返回命令执行结果。确保您提供有效的用户名,主机名和密码,以便采样器可以正常工作

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

JSch jSch = new JSch();
Session session = jSch.getSession("username", "hostname", 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("password");
session.connect();

Channel channel = session.openChannel("exec");

String command = "ls";
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();

channel.connect();
StringBuilder rv = new StringBuilder();
rv.append("New system date: ");
byte[] tmp = new byte[1024];
while (true) {
  while (in.available() > 0) {
      int i = in.read(tmp, 0, 1024);
      if (i < 0) break;
      rv.append(new String(tmp, 0, i));
  }
  if (channel.isClosed()) {
      break;
  }
  try {
      Thread.sleep(100);
  } catch (Exception ee) {
      ee.printStackTrace();
  }
}
in.close();
channel.disconnect();
session.disconnect();        
SampleResult.setResponseData(rv.toString().getBytes());

See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! for details on Groovy scripting engine installation and best scripting practices.

请参阅Beanshell vs JSR223 vs Java JMeter Scripting:Performance-Off你一直在等待!有关Groovy脚本引擎安装和最佳脚本实践的详细信息。

#3


You can use the Beanshell scripting inside jmeter then you can have some thing like that:

你可以在jmeter里面使用Beanshell脚本,那么你可以有类似的东西:

    String command="your command here";
    StringBuffer output = new StringBuffer();

    Process p;
    try {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        BufferedReader reader = 
                        new BufferedReader(new InputStreamReader(p.getInputStream()));

                    String line = "";           
        while ((line = reader.readLine())!= null) {
            output.append(line + "\n");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    log.inf(output.toString());

#4


Look here this answer about execute jar-file. But idea the same for other OS-command.

看看这个关于执行jar文件的答案。但对其他OS命令也一样。

  1. Use OS_Process_Sampler
  2. Fill field command with OS shell, like /bin/bash or analogue.
  3. 使用OS shell填充字段命令,例如/ bin / bash或analogue。

  4. Set first argument with -c
  5. 用-c设置第一个参数

  6. Set next argument with you command
  7. 使用you命令设置下一个参数

  8. Sampler ready to execute :)
  9. 采样器准备执行:)