Bash JMeter Beanshell断言:如何保存Runtime.getRuntime().exec(“";ifconfig | grep\";${IpAddress}\”wc-l”的值?

Bash JMeter Beanshell断言:如何保存Runtime.getRuntime().exec(“";ifconfig | grep\";${IpAddress}\”wc-l”的值?,bash,jmeter,beanshell,Bash,Jmeter,Beanshell,当在终端内部运行时,我的shell命令ifconfig | grep\“${IpAddress}\”wc-l返回0或1。我需要使用JMeter执行同样的操作,并相应地断言(0为失败,1为通过)。但当我这么做的时候: Runtime.getRuntime().exec("ifconfig | grep \"${IpAddress}\" | wc -l"); 我没有得到任何回报!关于如何保存(以及以后检索)此命令输出的值,有什么想法吗 您基本上可以通过以下方式执行3个命令: ifconfig g

当在终端内部运行时,我的shell命令
ifconfig | grep\“${IpAddress}\”wc-l
返回
0
1
。我需要使用JMeter执行同样的操作,并相应地断言(
0
为失败,
1
为通过)。但当我这么做的时候:

Runtime.getRuntime().exec("ifconfig | grep \"${IpAddress}\" | wc -l");
我没有得到任何回报!关于如何保存(以及以后检索)此命令输出的值,有什么想法吗

  • 您基本上可以通过以下方式执行3个命令:

    • ifconfig
    • grep
    • wc

      它只在内部工作,因此您需要修改命令,使其看起来像:

      /bin/bash -c ifconfig | grep \"${IpAddress}\" | wc -l
      
  • 您将JMeter变量称为
    ${IpAddress}
    ,这不是很好的做法,因为它们可能会导致编译失败。考虑使用<代码> vARs类实例速记,如<代码> VARS。获取(“IPAdvess”)< /COD> < /P>
  • 对于任何形式的脚本,您都没有使用最好的测试元素

  • 假设以上所有情况,我建议使用以下代码:

    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
    }
    

    我甚至尝试将输出重定向到另一个文件,以便稍后读取该文件,但它没有创建任何文件Runtime.getRuntime().exec(“ifconfig | grep\”${IpAddress}\“| wc-l>log.txt”);指定该文件的路径,使其指向您知道的位置。很可能是创建了文件,但您不知道在哪里,或者您没有权限访问“当前”文件夹。所以像
    “ifconfig | grep\”${IpAddress}\“|wc-l>/tmp/log.txt”
    这样的东西应该可以工作,这解释了为什么这些命令不工作