user3381098
user3381098

Reputation: 103

How to execute Linux command or shell script from APACHE JMETER

How to execute Linux command or shell script from APACHE JMETER

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

Upvotes: 1

Views: 7009

Answers (4)

user2384349
user2384349

Reputation: 1

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

  1. Use OS_Process_Sampler
  2. Fill field command with OS shell, like /bin/bash or analogue.
  3. Set first argument with -c
  4. Set next argument with you command
  5. Sampler ready to execute :)

Upvotes: 0

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

    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());

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168002

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. Download groovy-all.jar - Groovy scripting engine support for Jmeter and drop it to the /lib folder as well
  3. Restart JMeter to pick the libraries up
  4. Add JSR223 Sampler to your Test Plan and choose "groovy" from "Language" drop-down
  5. Follow example code from Exec.java Jsch tutorial to implement your own logic.

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

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.

Upvotes: 1

UBIK LOAD PACK
UBIK LOAD PACK

Reputation: 34516

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

Upvotes: 0

Related Questions