Reputation: 103
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
Reputation: 1
Look here this answer about execute jar-file. But idea the same for other OS-command.
Upvotes: 0
Reputation: 27
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
Reputation: 168002
If you need to execute a command on remote system take the following steps:
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
Reputation: 34516
Have a look at OS Process Sampler which is done for this and available in core:
Upvotes: 0