Get IP Address
M. Gallant 10/31/2000
The ability to create objects from classes in the Java API
can be used to extend the capability of Windows scripts.
Since the MS Java API is resident on any PC with IE4+ installed,
developers can leverage the capability contained therein.
The following script instantiates a Java object which contains
a method IPInfo.getIPString(String hostname)
to return the IP address for a supplied host name.
To use the Java class, compile the Java source code, and place the
resultant IPInfo.class
class file in the classpath (for example: C:\Windows\Java\Classes)
getIP.vbs
'****************************************************************
' File: getIP.vbs (WSH for VBscript)
' Author: (c) M. Gallant 10/31/2000
'
' Displays IP address for array of host names provided
' Uses Java class "IPInfo" which must be in classpath.
'****************************************************************
Option Explicit
Dim oJIPinfo, oWshShell, ipaddress, hostname, hostnames
hostnames = Array("host1.some.com", "host2.someother.com", "host3.another.com")
set oWshShell = CreateObject("WScript.Shell")
set oJIPinfo = GetObject("java:IPInfo") 'Instantiate Java object via moniker.
For Each hostname In hostnames
ipaddress = oJIPinfo.getIPString(hostname)
WScript.Echo hostname & " " & ipaddress
Next
'----------------- End Script -------------------------------
IPInfo.java (Java class)
import java.net.* ;
public class IPInfo {
public String getIPString(String hostname) {
try {
String ipaddr = InetAddress.getByName(hostname).getHostAddress() ;
return ipaddr ;
}
catch(UnknownHostException uhe) {
return "Unknown host" ;
}
}
public static void main (String args[]) { // test method
IPInfo ipobj = new IPInfo() ;
System.out.println(ipobj.getIPString(args[0]) );
}
}