Sometimes it is necessary for the load test to send a Linux command to verify the content/file exists in the remote Linux server. Every time it is not a fair idea to connect remote server by file server browsers like WinSCP, putty etc… as it is a load test. In such situations, JMETER offers a sampler called ‘SSH sampler’. This sampler is introduced to communicate with the Linux server over the ssh shell. It will be definitely useful for both Functional and Performance Testers.
Apache Jmeter’s SSH Sampler:
A sampler which will connect to a Linux server and execute Linux commands
Steps in detail:
1) Download SSH Sampler plugin from google’s website
2) Copy the jar to your local Apache Jmeter’s “lib/ext” folder.
3) Download “jsch-0.1.51.jar” and place it in your local Apache Jmeter’s “lib/” folder.
4) Open Jmeter-> ThreadGroup->Samplers-> SSH Command
5) Give hostname, username, password and the Linux command to be executed
6) Run Jmeter
7) Capture results in ‘View Results Tree’
Here is the image for your reference:
16 replies on “How to execute Linux command or shell script from APACHE JMETER”
2014/05/27 17:44:33 ERROR – jmeter.protocol.ssh.sampler.SSHSampler: Failed to connect to server with credentials email@example.com:22
It is working and I am using it frequently.You have problem with your server credentials. Please check it . Your error clearly shows that ‘Failed to connect to server with credentials’. Before giving it in a jmeter, open putty or winscp and give the same credentials and check it.
It worked, Thanks a lot
It worked for me and I was able to login to remote server with my user using SSH Sampler. However, I want to do few more steps. . I want to do the below now. Not sure how I can do
1. Sudo to a different user – ‘sudo su – report’
2. Change to some directory
3. Run some shell script which is already there
4. The Shell script will produce some output file. I need to SFTP the output file from there to local Jmeter directory
5. Write some tests to verify the output file
The main problem I am facing is that I am not able to understand how I can run series of commands in SSH Sampler. I tried using ; and it worked. However I am not able to do Sudo and run shell script from there.
Any help would be much appreciated.
Did you try keep those steps as shell script and run a shell script from jmeter using ‘sh shellscript_name’ command
Thanks @Suganthi Thangavel,
I have resolved that problem and am able to connect to the UNIX machine from SSH Sampler. However, I want to run a shell script on UNIX Box but when I am running it via Jmeter command its returning a return code of 127 ($?). Do you know what could be the reason.
If I have below commands in execute command of SSH Sampler Window I can see the present working directory, output of ls command and a value 0
– cd nexnet/bin/;pwd;ls -lrt;echo $?
However, when I try to run the shell script, I get value of 127. That means Shell script is not getting executed. Do you know what could be the remedy for this.
cd nexnet/bin/;pwd;ls -lrt;sh nexnet.sh;echo $?
I tried to search 127 and it says “command not found” or “illegal command”. However the shell script is their on UNIX box at the path and when I login directly to the box I can run it. However the same command is not working from SSH Sampler of JMETER
Can you please help if you have any ideas. I will keep on debugging myself as well.
I have managed to solve the above problem by adding below at the front of the command. So please ignore the above post.
However there is another issue now. I followed the below link to add SSH Sampler and SSH SFTP Sampler in Jmeter. SSH Sampler is working fine as I just connected to the machine and executed the shell script. However SSH SFTP Sampler is not visible in JMETER even after following all the steps like:
Any idea anybody, what could be the issue?
1. Downloaded “jmeter-ssh-sampler-0.1.0.jar” and copied to /lib/ext folder of JMeter installation.
2. Downloaded “jsch-0.1.51.jar” and copied to /lib/ folder of JMeter installation.
3. Restarted JMeter.
Manage to resolve this SFTP Sampler problem by following the below steps
Glad to hear that your issue is resolved.
How can i execute more than one command from the sampler? Should i add samplers for each command?
I have an issue here, am using SSH SFTP /SSH Command Sampler to transfer a file from Desktop to Server, but while executing am facing “Failed to Connect the Server”.
Used to access the server via Putty/Winscp manually. Using Jmeter am facing the above error message.
Also have written a code in JSR223 Sampler to connect, using “sftp.put” operation and able to transfer the file successfully.
Kindly provide any suggestions plz.
Failed to connect to server: Session.connect: java.net.SocketTimeoutException: Read timed out
The jmeter log shows below exception:
ERROR – jmeter.protocol.ssh.sampler.AbstractSSHSampler: SSH connexion error com.jcraft.jsch.JSchException: Session.connect: java.net.SocketTimeoutException: Read timed out
at java.lang.Thread.run(Unknown Source)
Thank you. It is helpfull and working nicely for me.
Hello I can login in my Unix server using host name, port, username and password using SSH Command sampler in JMeter. But in my test steps I need to switch user so in putty I am using su – namewheretoswitch followed by the password. But in JMeter SSH sampler su – namewheretoswitch is not working. Can you please help ? Also how to wright su command and password both in one line in JMeter SSH sampler ? Many thanks.
One Suggestion would be to create a script in the machine, and invoke that script from sampler.
[…] 16) How to execute the ssh command or shell script on a remote server using Jmeter? Using SSH sampler it can be done. For a detailed explanation see here https://www.technix.in/execute-linux-command-shell-script-apache-jmeter/ […]