Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Determining computers on a network...

 
Bill White
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have posted this question in other topics here but have had no luck.
So, does anyone here know if its even possible for a Pure Java application, using TCP/IP to snoop out and find the other computers on a company LAN. I am not talking about anything fancy here, like finding computers on a company WAN, A company global wide netwok etc... I just want to be able to find the other machines on a local LAN.
Is there a limitation in JAVA for this? I was thinking about using JNDI but even that seems to fall short.
Please help. Thank You.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,

So, does anyone here know if its even possible for a Pure Java application, using TCP/IP to snoop out and find the other computers on a company LAN.

Well, that depends on several things. I'm not sure that there is a 100% guaranteed way to enumerate all the machines on a LAN, but there are methods you could try. The best approach is to utilize a ping sweep. Now, without native code or running an external process like fping and capturing and parsing the output, you can't do that in Java because ICMP echo packets have to be sent over raw sockets. Another approach is to scan known ports on the LAN's subnet, eg ports 137-139 for Windows netsesion or some flavor of Unix running Samba, any machine on which you get an exception on you assume does not exist.
The main problem with this is some or all machines on the LAN may have these ports closed for security reasons. Even the ping sweep may not work if there is packet filtering software like ZoneAlarm installed on some or all of the machines.
There is a Java extension named JPcap with which you could code your own Java ping sweeper. It is a small package and seems to work OK but the documentation is attrocious. So you'll need a fair understanding of network protocols to use it effectively. You can get it here:
JPCap
If your machine is a Windows box, then you will also need WinPcap. You can get that here:
WinPcap
Hope this helps,
Michael Morris
 
Bill White
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Micheal,
Thank you so VERY much for this response. It is very informative and I am sure will go a long way in helping me succeeding with this next project.
 
mahmoud adel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.Arrays;
import java.io.IOException;
import java.io.*;
import java.util.Scanner;
import jpcap.*;
import jpcap.packet.*;

public class ARP{
public static void main(String[] args) throws IOException{

//Obtain the list of network interfaces
NetworkInterface[] devices = JpcapCaptor.getDeviceList();

//for each network interface
for (int i = 0; i < devices.length; i++) {
//print out its name and description
System.out.println(i+": "+devices[i].name + "(" + devices[i].description+")");

//print out its datalink name and description
System.out.println(" datalink: "+devices[i].datalink_name + "(" + devices[i].datalink_description+")");

//print out its MAC address
System.out.print(" MAC address:");
for (byte b : devices[i].mac_address)
System.out.print(Integer.toHexString(b&0xff) + ":");
System.out.println();

//print out its IP address, subnet mask and broadcast address
for (NetworkInterfaceAddress a : devices[i].addresses)
System.out.println(" address:"+a.address + " " + a.subnet + " "+ a.broadcast);
}

//NetworkInterface[] devices = JpcapCaptor.getDeviceList();
int index =1; // set index of the interface that you want to open.

//Open an interface with openDevice(NetworkInterface intrface, int snaplen, boolean promics, int to_ms)
final JpcapCaptor captor=JpcapCaptor.openDevice(devices[index], 65535, false, 20);









//JpcapCaptor captor=JpcapCaptor.openDevice(device[1], 65535, false, 20);

//call processPacket() to let Jpcap call PacketPrinter.receivePacket() for every packet capture.
//captor.processPacket(10,new PacketPrinter());
//System.out.println(packet);
//captor.close();

}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic