How to execute Linux command or shell script from APACHE JMETER

linux1

linux1Sometimes 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 ssh shell . It will be definitely useful for both Functional and Performance Testers.

Apache Jmeter’s SSH Sampler:

Sampler which will connect to linux server and execute linux commands

Steps in detail:

1) Download SSH Sampler plugin from google’s website
https://code.google.com/p/jmeter-ssh-sampler/
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:

test

 

12 Comments

  1. don´t work:
    2014/05/27 17:44:33 ERROR – jmeter.protocol.ssh.sampler.SSHSampler: Failed to connect to server with credentials root@ftjportal.qa.is.hi.inet: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.

  2. It worked, Thanks a lot

  3. 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.

    Cheers!
    Vivek

    • Suganthi Thangavel

      January 5, 2016 at 11:01 am

      Hi Vivek,

      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.

        For e.g.

        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.

          source ~/.bash_profile;

          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.

          https://blazemeter.com/blog/how-run-external-commands-and-programs-locally-and-remotely-jmeter

  4. Manage to resolve this SFTP Sampler problem by following the below steps

    https://sqatech.wordpress.com/2014/02/13/how-to-build-sftp-sampler-for-jmeter/#comment-7

  5. How can i execute more than one command from the sampler? Should i add samplers for each command?

  6. Hi,

    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.

    Regards,
    Dinesh

  7. 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 com.jcraft.jsch.Session.connect(Session.java:557)
    at org.apache.jmeter.protocol.ssh.sampler.AbstractSSHSampler.connect(AbstractSSHSampler.java:72)
    at org.apache.jmeter.protocol.ssh.sampler.SSHCommandSampler.sample(SSHCommandSampler.java:64)
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:475)
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:418)
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:249)
    at java.lang.Thread.run(Unknown Source)

Leave a Reply

Your email address will not be published.

*

© 2017 My techbook

Theme by Anders NorénUp ↑