0

My shell command ifconfig | grep \"${IpAddress}\" | wc -l returns either 0 or 1 when run inside terminal. I need to do the same using JMeter and assert accordingly (0 is fail, 1 is pass). But when I'm doing this:

Runtime.getRuntime().exec("ifconfig | grep \"${IpAddress}\" | wc -l");

I get nothing in return! Any ideas on how I can save (and later retrieve) the value of this command's output?

Joaquin
  • 2,013
  • 3
  • 14
  • 26
singh2005
  • 1,251
  • 12
  • 19
  • I even tried redirecting the output to another file so I could read that file later but it does not create any file Runtime.getRuntime().exec("ifconfig | grep \"${IpAddress}\" | wc -l > log.txt"); – singh2005 Jul 27 '18 at 18:22
  • Specify path for that file so it points to location you know. Most likely either file was created, but you don't know where, or you had no permissions to whatever "current" folder was. So something like `"ifconfig | grep \"${IpAddress}\" | wc -l > /tmp/log.txt"` should work – timbre timbre Jul 27 '18 at 18:31
  • 1
    https://stackoverflow.com/questions/31776546/why-does-runtime-execstring-work-for-some-but-not-all-commands/31776547 explains why these commands don't work – that other guy Jul 27 '18 at 19:03

1 Answers1

1
  1. You're basically execute 3 commands via pipe:

    • ifconfig
    • grep
    • wc

      It will work only inside a Linux SHELL so you need to amend your command to look like:

      /bin/bash -c ifconfig | grep \"${IpAddress}\" | wc -l
      
  2. You're referring a JMeter Variable as ${IpAddress} which is not very good practice as they can resolve into something causing compilation failure. Consider using vars shorthand for JMeterVariables class instance instead like vars.get("IpAddress")

  3. You're using not the best Test Element, starting JMeter 3.1 it is recommended to use JSR223 Elements and Groovy language for any form of scripting.

Assuming all above I would recommend using JSR223 Assertion and the code like:

String response = org.apache.commons.lang3.StringUtils.normalizeSpace(['/bin/bash', '-c', 'ifconfig | grep \"' + vars.get('IpAddress') + '\" | wc -l'].execute().text)
if (response.equals("1")) {
    //do what you need here
}
Dmitri T
  • 159,985
  • 5
  • 83
  • 133