Get localhost of client machine in Java WD

274 views Asked by At

I have created a Java Web Dynpro in the portal intranet. The localhost of the local machine is needed, but I'm only able to get the servername, serverlocalhost and the local machine ip-address.

When I'm running following code on a local java program, I'm getting:

  • Workstation: BEWSP
  • IP: 10.10.19.112

When I'm running following code on a SAP Portal program, I'm getting:

  • Workstation: SAPDEP
  • IP: 10.10.19.112

I need to get BEWSP in my SAP Portal application, any idea to do this?

           InetAddress ip = InetAddress.getLocalHost();
           String workstation = "";
           String currentip = "";
           //Workstation
           System.out.println("Workstation : " + ip.getHostName());
           workstation = "" + ip.getHostName();

           //Ip address
           System.out.println("Current IP address : " + ip.getHostAddress());
           currentip = "" +  ip.getHostAddress(); 

KR

1

There are 1 answers

0
Suncatcher On

Just to add the implementation of way chosen by OP:

import java.util.regex.Pattern;
import java.io.*;

public class nslookup
{
  public static void main(String[] args)
  {
      try {
                Process p = Runtime.getRuntime().exec("cmd /c nslookup 192.168.0.1");
                BufferedReader bi = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line = "";
                line = bi.readLine();
                while ( line != null ) {
                        if ( line.indexOf("Non-existent") != -1 ) {
                                System.out.println("The host/domain doesn't exist");
                                break;
                        }
                        if (line.matches("Server:\\s+.+$")) {
                            Matcher matcher = Pattern.compile("(Server:\\s+)([a-z0-9\\-]+[\\.]{1}[a-z0-9\\-]+([\\.]{1}[a-z0-9\\-]+)?([\\.]{1}[a-z0-9\\-]+)?)").matcher(line);
                            if (matcher.find()) {
                            String result = matcher.group(2);
                            System.out.println(result);
                            System.exit(42);
                            }
                        }
                        line = bi.readLine();
                }
                bi.close();
     } catch (IOException ioe) {
        ioe.printStackTrace(); }
  }
}