There is no way "to know the IP addresses of all PCs in the LAN network".
No way using java, no way using C or C++ and no way using anything else.
Oops, there is way but this way is administrative.
1. Prevent any IP setting/changing activity on all LAN machines.
2. Lock all network cables to prevent adding new machine to network.
3. Set IP on every machine in the LAN and maintain list of IPs.
Refer to list in case of questions.
> ip addresses in my LAN
You are aware that IP's can be set statically or can be obtained dynamically? If a "sleeping" (shut down) PC has a static IP address set, is this address considered as an "ip address in my LAN"?
If some machines on the LAN are provided dynamical IP addresses (from a range defined with the DHCP server) when booting, which IP addresses do you consider as part of your LAN?
Or are you referring to the IP addresses active at a given moment?
java.net.InetAddress. isReachable since 1.5
> > java.net.InetAddress. isReachable since 1.5
>
> AFAIK this method just makes an attempt to connect to
> 7th port using TCP...
And, be warned, if this is true, then many security "hardened" systems, including those "hardened" using Sun's own Jass packages, have this echo server disabled and won't answer this request.
Well, there is a way to know if an IP is alive but its rigourous and time wasting.
You try to open all possible socket or atleast some very common ports on a range of IP of a LAN
Actually, if u wanna chk an IP on a network u most know atleast an IP address and subnet mask so that u can calculate the network range.
Knowing the gateway too is nice though u can guess it from the subnet calculation.
Just scan thru the ports, if any one is alive, then d machine is alive. if none is alive, d sys may b dead.
> Well, there is a way to know if an IP is alive but
> its rigourous and time wasting.
>
> You try to open all possible socket or atleast some
> very common ports on a range of IP of a LAN
>
> Actually, if u wanna chk an IP on a network u most
> know atleast an IP address and subnet mask so that u
> can calculate the network range.
>
> Knowing the gateway too is nice though u can guess
> it from the subnet calculation.
>
> Just scan thru the ports, if any one is alive, then d
> machine is alive. if none is alive, d sys may b dead.
Of course one should note that doing the above on a network that one doesn't actually own can lead to being expelled, fired, civilly or even criminally prosecuted.
So before doing it on a network that one doesn't own one should get permission in writing (not verbally) from a superior.
> Well, there is a way to know if an IP is alive but its rigourous and time wasting.
> You try to open all possible socket or atleast some very common ports on a range of IP of a LAN
And? What will happened if host tuned to accept connections only from IP other then your? How about hosts with all ports closed?
THERE IS NO 100% WAY!
// You can use this code
// It will provide u all running pc's info
import java.net.*;
import java.io.*;
import java.text.*;
public class WindowsNetworkScanner {
public static void main(String[] args) {
getAllHosts();
}
public static void getAllHosts() {
String str;
String input = "net view";
String ipConfigResponse = null ;
int count = 0;
try {
ipConfigResponse = runConsoleCommand(input);
} catch (IOException e) {
System.out.println("exception is" + e.toString());
e.printStackTrace();
}/* catch (ParseException e) {
System.out.println(e.toString());
e.printStackTrace();
} */
java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(ipConfigResponse, "\n");
String lastMacAddress;
while (tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim();
int len = line.length();
if (len > 2) {
str = line.substring(0, 2);
if (str.compareTo("\\") == 1) {
count++;
line = line.substring(2, len);
System.out.println(" ");
System.out.println("the host is: " + line);
try {
String hostaddr = java.net.InetAddress.getByName(line).getHostAddress();
System.out.println(" ");
System.out.println("The IP ADDRESS of"+line+"is : "+hostaddr);
}
catch (java.net.UnknownHostException e) {
System.out.println("exception is" + e.toString());
e.printStackTrace();
}
}
}
}
System.out.println(" ");
System.out.println("Total Computers: " + count);
}
protected static String runConsoleCommand(String command) throws IOException {
Process p = Runtime.getRuntime().exec(command);
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
int c = 0;
do {
c = stdoutStream.read();
buffer.append((char)c);
} while (c != -1);
String outputText = buffer.toString();
stdoutStream.close();
return outputText;
}
} // End of class SMBCIFSNetworkScanner