Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Machine IP Address using beanshell in JMeter?

I am trying to write a script in beanshell for jmeter through which I can get the IP address of a machine (using ipconfig command and extract only IP Address from the output)?

The following code is giving only the IP of the request of which needs to be passed to jmeter.

String IP = InetAddress.getByName(prev.getURL().getHost()).getHostAddress();
vars.put("IP", IP);

Can anyone guide me?

like image 781
Nikhil Avatar asked Nov 22 '16 13:11

Nikhil


2 Answers

Try this one:

vars.put("IP", org.apache.jmeter.util.JMeterUtils.getLocalHostIP());

Demo:

Beanshell IP

You can also use __machineIP() function in the "Parameters" section and refer the value as Parameters or bsh.args[0] in the script body

References:

  • JMeterUtils.getLocalHostIp()
  • How to Use BeanShell: JMeter's Favorite Built-in Component
like image 119
Dmitri T Avatar answered Nov 25 '22 12:11

Dmitri T


you can also use as follows:

log.info("IP " + InetAddress.getLocalHost().getHostAddress());
String IP = InetAddress.getLocalHost().getHostAddress();
vars.put("localIP", IP);

later, you can refer the IP using the following syntax:

${localIP} or vars.get("localIP")
like image 28
Naveen Kumar R B Avatar answered Nov 25 '22 14:11

Naveen Kumar R B