Search code examples
jmeterbeanshell

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?


Solution

  • 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")